Localization Tools
How can I access localized strings?
An application that is being built with the LocaleStation analyzer can access the LocalStrings class that contains the following properties:
Languages: The languages that have been processed during the generation time. It lists all supported languages in a single assembly.Localizations: A list of localization IDs that have been fetched for every language in a single assembly.
The following functions are dynamically generated to support AOT:
Translate(): Gives a translated version of a string using a language and a localized text ID.Exists(): Checks to see if a translated version of a string using a language and a localized text ID exists or not.CheckCulture(): Checks to see if the given culture in a specific language exists.ListLanguagesCulture(): Lists languages in a given culture.
You can use those functions to get localized strings, but you'll have to take the following points into consideration:
If the language doesn't exist, then it returns a localized string in this format:
{lang}_{id}For example, if we don't have
brzandLOCAL_STRING, you'll getbrz_LOCAL_STRING.
If the language exists, but the localization ID doesn't exist, it returns:
{id}For example, if we have
engbut notLOCAL_STRING, you'll getLOCAL_STRING.
Otherwise, it returns a localized string according to both the ID and the language.
For example, if we have
spaandTEXT_HI, you'll get¡Hola!.
Common localization tools
You can use the common localization tools on applications that support languages that are supported by LocaleStation. The LanguageCommon class has the necessary functions to perform the following operations:
Language(property): Sets and gets the current language for localized applications and librariesGetInferredLanguage(): Infers the languages and returns the first language from the listGetInferredLanguages(): Infers the languages according to the current UI cultureTranslate(): Translates a given string using the string ID, the language action type name, and the languageIsCustomActionDefined(): Checks to see if the language action type is defined or notAddCustomAction(): Adds the language actionRemoveCustomAction(): Removes the language actionGetAction(): Gets the language action by name
Here are some of the best design principles when creating applications and libraries:
All applications that use Localizer for localization are advised to use the
Languageproperty (getter and setter), while all libraries should only use the value ofLanguageto avoid wrong languages to be set.You can optionally infer the supported language from the current UI culture and set the above property to the resultant value.
Adding language actions requires a definition of the
LanguageLocalActionsinstance, which needs:A function delegate to the
Languagesproperty (usually() => LocalStrings.Languages)A function delegate to the
Localizationsproperty (usually() => LocalStrings.Localizations)A function delegate to the
Translate()function (usuallyLocalStrings.Translate)A function delegate to the
Exists()function (usuallyLocalStrings.Exists)A function delegate to the
CheckCulture()function (usuallyLocalStrings.CheckCulture)A function delegate to the
ListLanguagesCulture()function (usuallyLocalStrings.ListLanguagesCulture)
An example of defining the above actions class boils down to this:
After that, in your application, you can make a function that checks for your actions type, adds it if necessary, and translates your string using your actions type to reduce repetition. The simplest example for this kind of translation is this (assuming that the localType constant is LocalLocalization:
Call the above function on the text that you want translated, passing the string ID as the only parameter. This setup respects the chosen language managed by LocaleStation. Applications can set the current language as follows:
If you've set the language to an unsupported language, you'll see the string placeholders as mentioned in the beginning of this page.
Last updated