Languages and String Resources

About

Data Descriptions store descriptions of attributes. This includes the translated names of these attributes in various languages. Using maxon::Resource and maxon::LanguageInterface it is easily possible to access such translated strings.

Note
maxon::Resource::LoadResourceString() and maxon::LanguageInterface::LoadResourceString() access the same data as maxon::DataDescriptionDatabaseInterface::LoadDescription().

Resources

The maxon::Resource class gives access to the supported languages:

// This example loops through all known languages and prints their names.
// get current language for later comparison
const maxon::LanguageRef currentLanguage = maxon::Resource::GetCurrentLanguage();
// get all languages
// loop through all languages
for (const maxon::LanguageRef& language : languages)
{
// print language name
DiagnosticOutput("Language: @", language.GetName());
// check if language is the current language
if (currentLanguage == language)
DiagnosticOutput("--> Current Language");
}
static MAXON_METHOD Result< BaseArray< LanguageRef > > GetAllLanguages()
static MAXON_METHOD LanguageRef GetCurrentLanguage()
for(i=0;i< length;i++)
Definition: unicodeobject.h:61
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
The maxon namespace contains all declarations of the MAXON API.
Definition: autoweight.h:14
#define iferr_return
Definition: resultbase.h:1524

A localized string of the current language can be obtained with:

// This example loads the string for the given attribute in the current language.
// prepare database and attribute IDs
const maxon::Id databaseID { "net.maxonexample.description" };
maxon::InternedId attributeID;
attributeID.Init("net.maxonexample.description.option") iferr_return;
// get string
const maxon::String localizedString = maxon::Resource::LoadResourceString(databaseID, attributeID);
DiagnosticOutput("Localized String: \"@\"", localizedString);
Definition: apibaseid.h:243
Definition: datatypelib.h:32
Result< void > Init(const Id &i)
static MAXON_METHOD String LoadResourceString(const Id &scope, const InternedId &keyValue)
Definition: string.h:1237

Language

The properties of a language object are accessed with:

// This example gets the current language and prints its settings to the console.
// get current language
const maxon::LanguageRef language = maxon::Resource::GetCurrentLanguage();
// get language settings
const maxon::String languageName = language.GetName();
const maxon::Id languageId = language.GetIdentifier();
DiagnosticOutput("Language: \"@\" (@)", languageName, languageId);

A localized string of a given language can be obtained with:

// This example loads the string for the given attribute in the current language.
// prepare database and attribute IDs
const maxon::Id database("net.maxonexample.description");
maxon::InternedId attribute;
attribute.Init("net.maxonexample.description.option") iferr_return;
// get string
const maxon::LanguageRef language = maxon::Resource::GetCurrentLanguage();
const maxon::String localizedString = language.LoadResourceString(database, attribute);
DiagnosticOutput("Localized String: \"@\"", localizedString);

Further Reading