-
Notifications
You must be signed in to change notification settings - Fork 0
3. Globalization
ROG edited this page Jun 3, 2022
·
6 revisions
Management strings in LanguageInfo<TLangCode, KTextCode>
.
public partial class Globalization<TLangCode, GCollectionCode, KTextCode>
The types used in
LanguageInfo<TLangCode, KTextCode>
must be the same here.
The following example shows how to instantiate Globalization
with LanguageInfo
defined:
public void InitGlobalization()
{
private static LanguageInfo<string, string, int> languagePtBr => new("pt_br", new(new()
{
{ "Home", new()
{
{0, "Olá"},
{1, "Seja Bem-Vindo"}
}}
}));
private static LanguageInfo<string, string, int> languageEn => new("en_us", new(new()
{
{"Home", new()
{
{ 0, "Hello" },
{ 1, "Wellcome" }
}}
}));
Globalization<string, string, int> globalization =
new(new() { languagePtBr, languageEn }, "pt_br");
}
The following example shows how to instantiate Globalization with the language information defined in the constructor (build-in):
public void InitGlobalization()
{
Globalization<string, string, int> globalization = new(new List<LanguageInfo<string, string, int>>
{
new("pt_br", new()
{
{"Home", new()
{
{ 0, "Olá" },
{ 1, "Seja Bem-Vindo" }
}}
}),
new("en_us", new(new()
{
{"Home", new()
{
{ 0, "Hello" },
{ 1, "Wellcome" }
}}
}))
}, "pt_br");
}
The following example shows how to initialize Globalization to watch for language changes and update the observed strings:
private void StartGlobalization()
{
// LanguageInfo already initialized before
Globalization<string, string, int> globalization =
new(new() { languagePtBr, languageEn }, "pt_br");
// Adds a method with signature defined by the delegate to the event
// containing all strings to be observed.
_globalization.LangTextObserver += Globalization_LangTextObserver;
}
// This method will be executed for the first time when calling StartGlobalization
// or every time the language is updated by UpdateLang or when calling SyncStrings
// to update all strings if necessary.
private void Globalization_LangTextObserver(object sender, UpdateModeEventArgs updateModeEventArgs)
{
// Assigns current language text based on GCollectionCode and KTextCode.
congrats = _globalization.SetText("Home", 0);
wellcome = _globalization.SetText("Home", 1);
}
This example shows how to format the string with the SetText
method to replace the {0} {1} {2}
points with corresponding strings.
Globalization<string, string, int> globalization =
new(new() { languagePtBr, languageEn }, "pt_br");
string text6 { get; set; }
public void ParamedSetText()
{
LanguageInfo<string, int, string> languageInfoEnUs = new("en_us", new()
{
{0, new()
{
{"text1", "Download on {0} and {1}"}
}}
});
Globalization<string, int, string> globalization =
new(new() { languageInfoEnUs }, "en_us");
globalization.LangTextObserver += Globalization_LangTextObserver;
globalization.StartGlobalization();
}
private void Globalization_LangTextObserver(object sender, UpdateModeEventArgs updateModeEventArgs)
{
text1 = _globalization.SetText(0, "text1", "NuGet", "GitHub");
//text1 = "Download on NuGet and GitHub";
}
Instance a new Globalization
.
public Globalization([NotNull] List<LanguageInfo<TLangCode, GCollectionCode, KTextCode>> languagesInfo, [NotNull] TLangCode langCodeNow)
Instance a new Globalization
and load the language strings in the JSON file. (More about JSON Serialization)
public Globalization([NotNull] string filepath, [NotNull] TLangCode langCodeNow)
Name | Description |
---|---|
hasStarted |
Get the state of the Globalization
|
Name | Description |
---|---|
SetText(GCollectionCode collectionCode, KTextCode key) |
Set the text according to the current language. Must be used in LangTextObserver event. |
SetText(KeyValuePair<GCollectionCode, KTextCode> textCode) |
Set the text according to the current language. Must be used in LangTextObserver event. |
SetText(GCollectionCode collectionCode, KTextCode key, params string[] formatArgs) |
Set the text according to the current language. Must be used in LangTextObserver event. |
SetText(KeyValuePair<GCollectionCode, KTextCode> textCode, params string[] formatArgs) |
Set the text according to the current language. Must be used in LangTextObserver event. |
UpdateLang(TLangCode newLangCode) |
Switch from one language to another and update all strings in LangTextObserver event. |
StartGlobalization() |
Set all strings in LangTextObserverCall for the first time. |
SyncStrings() |
Call LangTextObserver to synchronize all strings with current language. |
Name | Description |
---|---|
LangTextObserver |
This event is executed every time the language is changed with UpdateLang , StartGlobalization or SyncStrings is called. |