All Projects → xleon → I18N-Portable

xleon / I18N-Portable

Licence: MIT license
Simple and cross platform internationalization/translations for Xamarin and .NET

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to I18N-Portable

Flutter translate
Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter.
Stars: ✭ 245 (+140.2%)
Mutual labels:  translations, internationalization, localization
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (+150.98%)
Mutual labels:  translations, internationalization, localization
Django Translations
Django model translation for perfectionists with deadlines.
Stars: ✭ 109 (+6.86%)
Mutual labels:  translations, internationalization, localization
Attranslate
Semi-automated Text Translator for Websites and Apps
Stars: ✭ 178 (+74.51%)
Mutual labels:  translations, internationalization, localization
www.gafam.info
Sources of the gafam.info web page
Stars: ✭ 14 (-86.27%)
Mutual labels:  translations, localization
mini i18n
🌐 Minimalistic I18n library for Ruby
Stars: ✭ 93 (-8.82%)
Mutual labels:  translations, internationalization
django-parler-rest
Translatable model support for django-rest-framework
Stars: ✭ 48 (-52.94%)
Mutual labels:  translations, internationalization
arbify flutter
Flutter package providing Arbify support.
Stars: ✭ 18 (-82.35%)
Mutual labels:  internationalization, localization
Counterpart
A translation and localization library for Node.js and the browser.
Stars: ✭ 239 (+134.31%)
Mutual labels:  internationalization, localization
Localizer-Android
Gradle plugin which simplifies Android string resources & translations synchronization with POEditor.
Stars: ✭ 21 (-79.41%)
Mutual labels:  translations, localization
I18n Editor
GUI for editing your i18n translation files
Stars: ✭ 290 (+184.31%)
Mutual labels:  translations, internationalization
pH7-Internationalization
🎌 pH7CMS Internationalization (I18N) package 🙊 Get new languages for your pH7CMS website!
Stars: ✭ 17 (-83.33%)
Mutual labels:  translations, internationalization
Domino-English-Translation
🌏 Let's translate Domino, a Japanese MIDI editor!
Stars: ✭ 29 (-71.57%)
Mutual labels:  translations, localization
laravel-localized-routes
A convenient way to set up and use localized routes in a Laravel app.
Stars: ✭ 257 (+151.96%)
Mutual labels:  translations, localization
React Native Globalize
Internationalization (i18n) for React Native
Stars: ✭ 246 (+141.18%)
Mutual labels:  internationalization, localization
Django Parler
Easily translate "cheese omelet" into "omelette au fromage".
Stars: ✭ 459 (+350%)
Mutual labels:  translations, internationalization
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (-0.98%)
Mutual labels:  translations, internationalization
Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (+127.45%)
Mutual labels:  internationalization, localization
L10ns
Internationalization workflow and formatting
Stars: ✭ 234 (+129.41%)
Mutual labels:  internationalization, localization
Ava Docs
Localized docs for AVA
Stars: ✭ 455 (+346.08%)
Mutual labels:  translations, localization

I18N-Portable

Simple and cross platform internationalization/translations for Xamarin and .NET

NuGet NuGet AppVeyor Codecov

  • Cross platform
  • Simple to use: "key".Translate().
  • Simple and fluent initialization setup.
  • Readable locale files (.txt with key/value pairs).
  • Support for custom file formats (json, xml, etc)
  • Light weight
  • No dependencies.
  • Well tested

https://cloud.githubusercontent.com/assets/145087/24824462/c5a0ecce-1c0b-11e7-84d3-4f0fa815c9da.png

Install

Install it on your PCL and platform projects. From nuget package manager console:

PM> Install-Package I18NPortable

Setup locales

  • In your PCL/Core project, create a directory called "Locales".
  • Create a {languageCode}.txt file for each language you want to support. languageCode can be a two letter ISO code or a culture name like "en-US". See full list here.
  • Set "Build Action" to "Embedded Resource" on the properties of each file

Locale content sample

# key = value (the key will be the same across locales)
one = uno
two = dos
three = tres 
four = cuatro
five = cinco
  
# Enums are supported
Animals.Dog = Perro
Animals.Cat = Gato
Animals.Rat = Rata
Animals.Tiger = Tigre
Animals.Monkey = Mono
 
# Support for string.Format()
stars.count = Tienes {0} estrellas
 
TextWithLineBreakCharacters = Line One\nLine Two\r\nLine Three
 
Multiline = Line One
    Line Two
    Line Three

Other file formats (including custom) supported

Fluent initialization

I18N.Current
    .SetNotFoundSymbol("$") // Optional: when a key is not found, it will appear as $key$ (defaults to "$")
    .SetFallbackLocale("en") // Optional but recommended: locale to load in case the system locale is not supported
    .SetThrowWhenKeyNotFound(true) // Optional: Throw an exception when keys are not found (recommended only for debugging)
    .SetLogger(text => Debug.WriteLine(text)) // action to output traces
    .SetResourcesFolder("OtherLocales") // Optional: The directory containing the resource files (defaults to "Locales")
    .Init(GetType().GetTypeInfo().Assembly); // assembly where locales live

Usage

string one = "one".Translate();
string notification = "Mailbox.Notification".Translate("Diego", 3); // same as string.Format(params). Output: Hello Diego, you've got 3 emails
string missingKey = "missing".Translate(); // if the key is not found the output will be $key$. Output: $missing$
string giveMeNull = "missing".TranslateOrNull(); // Output: null

string dog = Animals.Dog.Translate(); // translate enum value (Animals is an Enum backed up in the locale file with "Animals.Dog = Perro")

List<string> animals = I18N.Current.TranslateEnumToList<Animals>(); 

List<Tuple<Animals, string>> animals = I18N.Current.TranslateEnumToTupleList<Animals>();
string dog = animals[0].Item2; // Perro

Dictionary<Animals, string> animals = I18N.Current.TranslateEnumToDictionary<Animals>();
string dog = animals[Animals.Dog]; // Perro

// List of supported languages (present in the "Locales" folder) in case you need to show a picker list
List<PortableLanguage> languages = I18N.Current.Languages; // Each `PortableLanguage` has 2 strings: Locale and DisplayName

// change language on runtime
I18N.Current.Language = language; // instance of PortableLanguage

// change language on runtime (option 2)
I18N.Current.Locale = "fr";

Data binding

I18N implements INotifyPropertyChanged and it has an indexer to translate keys. For instance, you could translate a key like:

string three = I18N.Current["three"]; 

With that said, the easiest way to bind your views to I18N translations is to use the built-in indexer by creating a proxy object in your ViewModel:

public abstract class BaseViewModel
{
    public II18N Strings => I18N.Current;
}

Xaml sample

<Button Content="{Binding Strings[key]}" />

Xamarin.Forms sample

<Button Text="{Binding Strings[key]}" />`

Android/MvvmCross sample

<TextView local:MvxBind="Text Strings[key]" />

iOS/MvvmCross sample

var set = this.CreateBindingSet<YourView, YourViewModel>();
set.Bind(anyUIText).To("Strings[key]");

Supported formats

The library ships with a single format reader/parser that is TextKvpReader. Any other reader will be isolated in a different nuget/plugin to keep the library as simple as possible.

Reader Format Source
TextKvpReader See sample I18NPortable
JsonKvpReader See sample I18NPortable.JsonReader I18NPortable.JsonReader
JsonListReader See sample I18NPortable.JsonReader I18NPortable.JsonReader

To use any non-default format, it needs to be added on initialization:

I18N.Current
    .AddLocaleReader(new JsonKvpReader(), ".json") // ILocaleReader, file extension
    // add more readers here if you need to
    .Init(GetType().Assembly);

Creating a custom reader for another file format:

It's very easy to create custom readers/parsers for any file format you wish. For instance, lets take a loot at the above mentioned JsonKvpReader:

Given this en.json file

{
  "one": "uno",
  "two": "dos",
  "three": "tres"
}

Creating a custom reader is as simple as implementing ILocaleReader:

public interface ILocaleReader
{
    Dictionary<string, string> Read(Stream stream);
}
public class JsonKvpReader : ILocaleReader
{
    public Dictionary<string, string> Read(Stream stream)
    {
        using (var streamReader = new StreamReader(stream))
        {
            var json = streamReader.ReadToEnd();

            return JsonConvert
                .DeserializeObject<Dictionary<string, string>>(json)
                .ToDictionary(x => x.Key.Trim(), x => x.Value.Trim().UnescapeLineBreaks());
        }
    }
}

Contributing new readers

If you implemented a new reader for another file format and you want to contribute, feel free to make a pull request. Any new reader will live in their own project in the solution and will produce a different nuget as a plugin to I18NPortable.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].