Git Product home page Git Product logo

translateme's Introduction

TranslateMe

Obsolete : replaced by CodingSeb.Localization

A C# and WPF apps translation library

Status

Branch Status
master Build Status
dev Dev Status
nuget NuGet Status

Quick installation

With Nuget

PM> Install-Package TranslateMe

or in Visual Studio Right-Click on your *.csproj --> "Manage nuget package..." --> Search for "TranslateMe" Install the "TranslateMe" package by Coding Seb.

To see how to use it

Simply clone this repository

Simple Use

In C# :

using TranslateMe;

// ...

// Text Id is the identifier of the text to translate
string translatedText = TM.Tr(textId);
// or -> defaultText is the text to show if no translations are defined for this textId in the current language.
string translatedText = TM.Tr(textId,defaultText);
// or -> languageId to force the language in which is show the text
// "en", "fr", "es" ...
string translatedText = TM.Tr(textId, defaultText, languageId);

In XAML (WPF) :

(no xmlns needed Tr Markup is available as soon as TranslateMe and TranslateMe.WPF are in project's references)

<!-- textId can be automatically calculate (with x:Name and the context of the element) -->
<Label x:Name="lbMyLabel" Content="{Tr }" />
<!-- or -> specify a custom textId -->
<Label x:Name="lblMyLabel" Content="{Tr 'textId'}" />
<!-- or -> specify a default text to show in designer and/or when the text is not translated in the current language-->
<!-- To escape special character you can use standard xml escapes like &gt; &quot; ... for single quote escape use [apos] -->
<Label x:Name="lblMyLabel" Content="{Tr 'textId', DefaultText='my default Text here'}" />
<!-- or -> force the translationLanguage -->
<Label x:Name="lblMyLabel" Content="{Tr 'textId', LanguageId='en'}" />

In general use XML escape to escape special characters. For single quote use [apos] to escape. XML escape does'nt work in this case for inline Tr markup. Or use the following format :

<!-- textId can be automatically calculate (with x:Name and the context of the element) -->
<Label x:Name="lbMyLabel" >
  <Label.Content>
    <Tr DefaultText="Text with a ' here" />
  </Label.Content>
</Label>

Language Selection

To select the language of the application.

TM.Instance.CurrentLanguage = "en";
TM.Instance.CurrentLanguage = "fr";
TM.Instance.CurrentLanguage = "es";
// ...
// To get availables languages
Collection<string> languages = TM.Instance.AvailableLanguages;

To see how to do it in XAML look at the "TranslateMe.Examples" project in the solution.

Remark : By default the translation made in the XAML are automatically updated when current language changed

OK But... ...How I define my translations ?

Translations are defined in JSON files with the extension "*.tm.json".

Here an example :

{
  "LanguageName": {
    "en": "English",
    "es": "Español",
    "fr": "Français"
  },
  "[TranslateMe.Examples.MainWindow].lblCurrentLanguage[Label].Content": {
    "en": "Current language",
    "es": "Lenguaje actual",
    "fr": "Langue courrante"
  },
  "[TranslateMe.Examples.MainWindow].lblHelloInCurrentLanguage[Label].Content": {
    "en": "Hello",
    "es": "Hola",
    "fr": "Bonjour"
  },
  "HelloInCurrentLanguage": {
    "en": "Hello in the current language",
    "es": "Hola en la lengua actual",
    "fr": "Bonjour dans la langue actuelle"
  },
  "[TranslateMe.Examples.MainWindow].lblHelloInCurrentLanguage[Label].ToolTip": {
    "en": "In english",
    "es": "En español",
    "fr": "En français"
  }
}

And to load it :

TMLanguagesLoader.Instance.AddFile(@"PathToTheFile\Example1.tm.json");
// or load directly a directory with multiple "*.tm.json" files.
TMLanguagesLoader.Instance.AddDirectory(@"PathToTheDirectory");

So you can change the text of your app or translate it in a new language without recompile all your application.

// or you can also load a translation by code (textId, languageId, value)
TMLanguagesLoader.Instance.AddTranslation("SayHello", "en", "Hello" );
TMLanguagesLoader.Instance.AddTranslation("SayHello", "es", "Hola" );
TMLanguagesLoader.Instance.AddTranslation("SayHello", "fr", "Bonjour" );

Implement your own file format

If you want to support an other format than json, you can create your custom FileLanguageLoader. Simply create a class that implement the ITMFileLanguageLoader interface and add an instance of your class in the FileLanguageLoaders :

TMLanguagesLoader.Instance.FileLanguageLoaders.Add(new YouCustomClassImplementingITMFileLanguageLoader());

Look at the TranslateMe/TMJsonFileLanguageLoader.cs file to see how it works.

Find Missing Translations

You can activate an option to generate a file with all missing translations.

// This will create a file named "TMMissingTranslations.json" in the directory of your assembly
// with all TextId and LanguageId that are missing when you trying to translate them.
TM.Instance.LogOutMissingTranslations = true;

translateme's People

Contributors

codingseb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

translateme's Issues

.NetStandard and Console demo

Hello, could you kindly migrate this project to .NetStandard and if possible provide us with a console demo of this library?

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.