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.
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
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
:
internal static class LanguageTools
{
private const string localType = "LocalLocalization";
internal static string GetLocalized(string id)
{
if (!LanguageCommon.IsCustomActionDefined(localType))
LanguageCommon.AddCustomAction(localType, new(() => LocalStrings.Languages, () => LocalStrings.Localizations, LocalStrings.Translate, LocalStrings.CheckCulture, LocalStrings.ListLanguagesCulture, LocalStrings.Exists));
return LanguageCommon.Translate(id, localType);
}
}
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:
LanguageCommon.Language = "ger";
Last updated