Localization JSON File
In the MauiProgram.cs file,
builder
.UseMauiApp<App>()
.UseMauiMarkupLocalization();
should be added.
In your main project you should have a language file of type json. The translation will be read from this file and imported. If you do not specify the path to the file(s) in the parameter (
builder
.UseMauiApp<App>()
.UseMauiMarkupLocalization();
//.UseMauiMarkupLocalization(defaultLang:"en-US"); // or
//.UseMauiMarkupLocalization(defaultLang:"en-US", "Loc1.json", "Loc2.json"); // or
), will look for a json file named Localization.json
in the home directory.
if you give one or more parameters like
builder
.UseMauiApp<App>()
.UseMauiMarkupLocalization(filePaths: "Localization1.json", "Localization2.json", "/Languages/Temp1.json");
it will read json files in given file paths.
The critical point here is to select Build Action: MauiAsset
for json files.
Proper json format:
{
"Hello": {
"tr-TR": "Merhaba Dünya!",
"en-US": "Hello World!"
},
"Msg": {
"tr-TR": "Deneme amaçlı yapılmıştır.",
"en-US": "It was made for testing purposes."
}
}
Instead of 'keyWord' keywords, you can use any word or phrase(s) you want. You don't have any Regex limitations.
You can also change the 'tr-TR' and 'en-US' language keys with words or sentences as you wish. But it is recommended to use expressions such as 'en-US', 'tr-TR', 'fr-FR'.
You can change the language of the system with the following statement: Translator.Instance.ChangeCulture(CultureInfo.GetCultureInfo("en-US"));
You can simply use
new Label()
.Text(e => e.Translate("Hello"))
.FontSize(32)
.CenterHorizontal()
.SemanticHeadingLevel(SemanticHeadingLevel.Level1),
new Label()
.Text(e => e.Translate("Msg"))
.FontSize(18)
.CenterHorizontal()
.SemanticDescription(e => e.Translate("Msg"))
.SemanticHeadingLevel(SemanticHeadingLevel.Level1),
new VerticalStackLayout()
.Center()
.Children(
new RadioButton()
.IsChecked(Translator.Instance.CurrentCulture.Name == "tr-TR")
.Content("tr-TR")
.OnCheckedChanged((sender, e) =>
{
Translator.Instance.ChangeCulture(CultureInfo.GetCultureInfo("tr-TR"));
}),
new RadioButton()
.IsChecked(Translator.Instance.CurrentCulture.Name == "en-US")
.Content("en-US")
.OnCheckedChanged((sender, e) =>
{
Translator.Instance.ChangeCulture(CultureInfo.GetCultureInfo("en-US"));
})
)
in the code.