GithubHelp home page GithubHelp logo

wagenet / ember-cli-i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davyjoneslocker/ember-cli-i18n

0.0 2.0 0.0 154 KB

Simple Internationalization support for ember-cli apps

License: MIT License

JavaScript 99.94% CSS 0.06%

ember-cli-i18n's Introduction

Ember CLI i18n

Build

About

Simple Internationalization support for ember-cli apps.

Note: This release requires Ember 1.9. Will likely break with 1.10

Install

npm install ember-cli-i18n --save-dev

Usage

Translate

Configuration

In your app's config/environment.js you'll need to set ENV.APP.defaultLocale to a country code:

var ENV = {
  APP: {
    defaultLocale: 'en'
  }
};

defaultLocale is only the fallback. If you wanted to change the locale of the application you should modify your application's locale:

var set = Ember.set;
var application = container.lookup('application:main');
set(application, 'locale', 'fr');

You can can trigger this after authentication, or if the user modifies a language setting in the app. Of course when this state is removed you should clear locale so that internationalization fallback to defaultLocale.

Locale Files

Generate a new locale file:

ember g locale en

The file will be added to app/locales

app
└── locales
    └── en.js

Then export a single POJO:

export default {
  home: {
    title: 'Welcome'
  }
};
Interpolation

You can add keys for interpolation

export default {
  age: 'You are %@1 years old.',
  name: '%@, %@'
};

The rules for interpolation follow the same from Ember.String.fmt

Pluralization

Pluralization keys follow the format from CLDR. For example, for en it expects only the keys one and other:

export default {
  friend: {
    one: 'only one friend, %@2',
    other: '%@ friends, %@'
  }
};

The first value passed will be considered the count for determining how to pluralize.

t('friend', 0, 'Brian');
// 0 friends, Brian

t('friend', 1, 'Brian');
// only one friend, Brian

t('friend', 10, 'Brian');
// 10 friends, Brian

Hyphenated languages will be split and the first half will be used to determine the pluralization rules. So both en-us and en-gb will follow the en rules.

View the currently supported set of pluralization rules.

Helper

You can access the translations in your app with the t helper:

{{t 'home.title'}}

Computed properties for the path are also supported:

{{t age}}

If the value has interpolation keys you can pass those values:

{{t colors colorOne colorTwo}}

Utility

The t function can be used outside of templates as a utility function:

import Ember from 'ember';

export default Ember.Object.extend({
  foo: function() {
    var t = container.lookup('utils:t');
    return t('foo.bar');
  }
});

t is automatically injected into Controllers, Components, Routes, and Models:

export default DS.Model.extend({
  name: function() {
    return this.t('name', 'John', 'Doe');
  }
});

Note that interpolation values can also be passed as an array if you prefer this style. this.t('name', ['John', 'Doe'])

Authors

We are very thankful for the many contributors

Versioning

This library follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this gem. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard, Inc © 2014

@dockyard

Licensed under the MIT license

ember-cli-i18n's People

Contributors

bcardarella avatar rwjblue avatar marcoow avatar dlangevin avatar ember-tomster avatar

Watchers

Peter Wagenet avatar 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.