SimplePCL for localization, can be used in any Xamarin app as well on WPF
Use nuget to import the reference in your project
PM> Install-Package XlocalizationService
After installation create a myVocabulary.csv
file inside the project. You can choose if save as content or resource.
The file have to follow these rules:
- Use standard csv formart: comma to separate columns, "\n" to separate rows
- First row start with "Key, " and then contain the Language Culture Names ("it-IT", "en-US")
- All the keys start with "#" and are lowercase
- After the Key, follow the list of localizations in the order defined by first row
- File contains also Language Culture Names localization
- If the localization contain a comma or special char you have to use the quotation mark
- Emplty row will be ignored
Example
Key,it-IT,en-US #it-it,Italiano,Italian
#en-en,Inglese,English
#,,
#set,Imposta,Set
#error,Errore,Error
#example, Questo e' un esempio di stringa, This is a sample
#example2, "Questo e' un esempio, con anche una virgola2", "This is a sample, with comma"
Load vocabulary as contet
StreamReader sr = new StreamReader("Vocabulary.csv");
LocalizationService.LS.ChangeVocabulary(sr.BaseStream);
Load vocabulary as embedded resource (Xamarin)
var assembly = typeof(anyClassOfYourDll).GetTypeInfo().Assembly;
Stream stream= assembly.GetManifestResourceStream("myNameSpace.Vocabulary.csv");
LocalizationService.LS.ChangeVocabulary(stream);
Example of use in code behind:
String localizedString = LocalizationService.LS["#example"];
or
String localizedString = LocalizationService.LS.GetValue("#example", "fallBackValue)";
Example of use in XAML (at the moment supported only on WPF):
xmlns:ls="clr-namespace:XLocalizationService;assembly=XLocalizationService.Win"
or
xmlns:ls="https://github.com/mvaloriani/XLocalizationService"
<Window.Resources>
<ls:LanguageConverter x:Key="LangConverter" />
</Window.Resources>
<TextBlock Text="{Binding Converter={StaticResource LangConverter}, ConverterParameter=#key}"/>
<TextBlock Text="{Binding Converter={StaticResource LangConverter}, ConverterParameter=#key|fallbackValue}"/>
<TextBlock Text="{Binding Converter={StaticResource LangConverter}, ConverterParameter=#key|fallbackValue| strinfToConcatenate}"/>
<TextBlock Text="{Binding Converter={StaticResource LangConverter}, ConverterParameter=#key|fallbackValue| strinfToConcatenate | stringToConcatenateBefore}"/>
If the fallBack value is not set and key is not found, the return value is String.Empty
To obtain the list of available languages
LocalizationService.LS.SupportedLanguages
To set a new language us the standard language convection
LocalizationService.LS.SetLanguage("en-US")
Written with StackEdit.