Git Product home page Git Product logo

localizy's Introduction

Localizy

Strongly typed localization. Based on the foundations of Fubu-Localizations but with an easy setup and good feature set.

Getting Started

Defining a translation is done by creating a public static field. The generic paramter type should be the parent class type.

public class L 
{
    public static StringToken Default = new StringToken<L>("Default Value"); 	
}

Then in your Application_Start or static Main method, you need to initialize the Localization provider. This should only be called once. LocalizationManager is a static class helper. This uses the generic parameter to determine the assembly in which your localizations are defined. You can pass the assembly in when not using the generic version of the Init method.

var provider = LocalizationManager.Init<L>();

You can then use the provider to resolve the translations. It can be resolved in 1 of 3 ways.

  1. By Key
    This returns null if the key could not be found. The key is defaulted to the class hierachy.
  2. By StringToken
  3. Using the StringToken directly
// 1.
provider.TryGetText("L.Default");
// 2.
provider.GetText(L.Default);
// 3.
L.Default

Localization Sources

You can define multiple localization sources for the transated values. These need to implement ILocalizationStorageProvider.

public interface ILocalizationStorageProvider
{
    string Name { get; }
    IEnumerable<LocalString> Provide(CultureInfo culture);
}

These can then be passed to the Init method. Multiple can be defined and they will get overlayed in the order they are passed in.
For instance you may have google localizations and custom localizations.

var provider = LocalizationManager
    .Init<L>(new XmlDirectoryStorageProvider("Google", @"c:\mylocalizations"), 
             new CustomLocalizationProvider());

The above example is using the default XmlDirectoryStorageProvider which loads translations formatted as xml. For instance the google translations could be stored in XML files like so. The files must start with the culture and end in .locale.config. eg. de.local.config. This can be overridden.

<localizations>
  <string key="L.Default">The Google Default</string>
</localizations>

Current Culture

The current culture used to resolve the translations is determined (by default) to be the Current Thread Culture but this can be overriden in a couple different ways.

1 - CurrentCultureFactory

provider.CurrentCultureFactory = () => {
   //return my fancy way to resolve the current culture  
};

2 - Passing it in

provider.TryGetText("L.Default", new Culture("de"));
// or
provider.GetText(L.Default, new Culture("de"));
// or 
L.Default.ToString(new Culture("de"));

localizy's People

Contributors

schotime avatar

Watchers

James Cloos 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.