Git Product home page Git Product logo

easylocalization's Introduction

EasyLocalization

A simple library that makes WPF Localization easier.

Features

  • An easy extension to use {l:Localize Title1}
  • Handles bindable Key {l:Localize Key={Binding TitleKey}}
  • Handles alternative keys when the Key is not provided or not found (ControlName_PropertyName, e.g. Button1_Content)
  • Handles singular, zero and plural values {l:Localize Key=Sentence1, CountSource={Binding Value}}
    • The zero form is automatically used when the Count value is equal to 0
    • The plural form is automatically used when the Count value is greater than 0
  • Ability to change the current culture without having to restart the application.

Demo

Demo GIF

How to use

Things that you should know

  • EasyLocation doesn't use resources.resx files (at least for now).
  • EasyLocation works with cultures to identify languages (CultureInfo class)
  • There are only 3 types of readers for now:
    • CharSeperatedFileReader: Each line is considered to have at least a key and a value seperated by a char (e.g. en-US.txt)
    • JsonFileReader: A json file of a key-object format (e.g. fr.json)
    • XmlFileReader: A xml file with an <Entries> root and <Entry> elements, each must have a key attribute (e.g. es-ES.xml)

Adding cultures (languages)

On startup (for example in App.xaml.cs) you'll have to register the cultures you want your applications to have:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    LocalizationManager.Instance.AddCulture(
        CultureInfo.GetCultureInfo("en-US"),
        new CharSeperatedFileReader("Resources/en-US.txt"),
        true);
    LocalizationManager.Instance.AddCulture(
        CultureInfo.GetCultureInfo("es-ES"),
        new XmlFileReader("Resources/es-ES.xml"));
    LocalizationManager.Instance.AddCulture(
        CultureInfo.GetCultureInfo("fr"),
        new JsonFileReader("Resources/fr.json"));
}

The true in the first AddCulture call tells the LocalizationManager to choose this language for now.

Simple localization

<TextBlock Margin="4" Text="{_:Localize Key1}" />
<TextBlock Margin="4" Text="{_:Localize Key=Key1_1}" />

Bindable Key

<TextBlock Margin="4" Text="{localization:Localize KeySource={Binding Key} />

Alterternative Key

<TextBlock Margin="4" Name="LblTitle" Text="{localization:Localize}" />

Since the Key is not provided, the LocalizationManager will use the alternative key, in this case it's LblTitle_Text.

Handling singular, zero and plural

<TextBlock Margin="4"
           Text="{localization:Localize KeySource={Binding Key}, 
                                        CountSource={Binding Value}}" />

The LocalizationManager will adapt the Text property whenever the Key or Count change:

  • If the Count value is 0, the ZeroValue is chosen.
  • If the Count value is 1, the Value is chosen.
  • Otherwise the PluralValue is chosen string.Format(PluralValue, Count)

Changing the culture (language)

LocalizationManager.Instance.CurrentCulture = CultureInfo.GetCultureInfo("fr");

Whenever the CurrentCulture changes, the whole application is automatically updated without the need to restart it.

For a list of the available/added cultures:

LocalizationManager.Instance.AvailableCultures

easylocalization's People

Contributors

zhaytam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.