We use Microsoft.Extensions.Localization for any localization related work.
For more documentation on localization, read the references listed at the bottom.
-
We use
.resw
files located under the Strings folder to localize texts. -
An Uno implementation of
IStringLocalizer
(ResourceLoaderStringLocalizer
) is registered as service in the LocalizationConfiguration.cs file. -
For tests projects, a mock implementation of
IStringLocalizer
is used to avoid relying on a file. This improves performance and eases test parallelization. -
We use
IStringLocalizer
to resolve those localized textsvar stringLocalizer = serviceProvider.GetService<IStringLocalizer>(); // Using IStringLocalizer as a dictionary string myString = stringLocalizer["MyKey"]; // You can get a LocalizedString object too LocalizedString myString = stringLocalizer["MyKey"]; var isResourceNotFound = myString.ResourceNotFound;
-
In most cases, the system UI culture will define the displayed culture of the application; this is the default behaviour.
-
In some cases, we would like to allow the user to switch to another language based on a user preference (sandboxed to the application). This template offers this ability using a configuration setting (
ThreadCultureOverrideService
) configured in the LocalizationConfiguration.cs file.-
Much like the runtime environment, the preferred culture is saved in a file and processed during the startup. We use cultures instead of languages to support different versions of the same language (e.g. en-US vs en-UK or fr-CA vs fr-FR).
-
Once the culture is resolved, the thread culture is overwritten using the new culture. If the user culture is not supported by the application, the default culture will be used instead.
-
You can expose this feature in a settings page for example (toggle between English and French).
var threadCultureOverrideService = serviceProvider.GetService<ThreadCultureOverrideService>(); threadCultureOverrideService.SetCulture(new CultureInfo("fr-CA"));
-
Multiple localization features can be tested from the diagnostics screen. This is configured in SummaryDiagnosticsViewModel.
- You can see the current UI culture.
- You can set another UI culture (supported or not by the application); this is very useful to test how the application will behave in an unsupported culture.