Skip to content

mvaloriani/XLocalizationService

Repository files navigation

XLocalizationService

SimplePCL for localization, can be used in any Xamarin app as well on WPF

Installation

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:

  1. Use standard csv formart: comma to separate columns, "\n" to separate rows
  2. First row start with "Key, " and then contain the Language Culture Names ("it-IT", "en-US")
  3. All the keys start with "#" and are lowercase
  4. After the Key, follow the list of localizations in the order defined by first row
  5. File contains also Language Culture Names localization
  6. If the localization contain a comma or special char you have to use the quotation mark
  7. 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"

How to use

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

Language Management

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.

About

SimplePCL for lacalization

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages