• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Internationalization support for ember-cli apps

Ember CLI i18n

Deprecated!

This library has been deprecated in favor of ember-i18n >= 4.0.0

Build

About

Simple Internationalization support for ember-cli apps.

Note: This release requires Ember 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'])

Overriding the Locale Lookup Handler

By default locales are attempted to be looked as modules in your project. However, you may wish to override how this is done. You can do that by overriding the locale lookup handler. Let's assume you have all of your locales stored in a single POJO.

You'll first need to create a new file: my-app/services/i18n.js

import service from 'ember-cli-i18n/services/i18n';

service.getLocalizedPath = function(locale, path) {
  return Locales[locale][path];    
}

export default service;

The default service object that was imported has three functions that can be overridden and customized:

resolveLocale

  • Paramaters: container, scope
  • Returns: locale code

getLocalizedPath

  • Paramaters: locale, path, container, scope
  • Returns: string or object

applyPluralizationRules

  • Paramaters: result, locale, path, container, values, scope
  • Returns: if result is a string, will skip rules and return result. If result is an Object, will assume pluralization needs to apply and formats result with proper pluralization rules based upon values[0]

fmt

  • Paramaters: result, values
  • Returns: formatted string

This function delegates to Ember.String.fmt by default. You can override

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

More Repositories

1

client_side_validations

Client Side Validations made easy for Ruby on Rails
Ruby
2,686
star
2

ember-validations

Validations for Ember Objects
JavaScript
832
star
3

postgres_ext

Adds support for missing PostgreSQL data types to ActiveRecord
Ruby
643
star
4

ember-easy-form

Easily build semantic forms in Ember
JavaScript
565
star
5

party_foul

Use GitHub to track your application errors!
Ruby
517
star
6

ruby-destroyed_at

ActiveRecord Mixin for Safe Destroys
Ruby
349
star
7

capybara-email

Test your ActionMailer and Mailer messages with Capybara
Ruby
339
star
8

postgres_ext-serializers

Ruby
324
star
9

client_side_validations-simple_form

Simple Form plugin for ClientSideValidations
JavaScript
254
star
10

ember-appkit-rails

Ember Appkit for Rails
Ruby
238
star
11

ember-suave

Make your Ember App Stylish
JavaScript
179
star
12

ember-one-way-controls

Native one way input
JavaScript
176
star
13

ember-data-route

Common teardown scenario for ember routes backed by a data model
JavaScript
120
star
14

dismissible_helpers

Ruby
96
star
15

es6_module_transpiler-rails

Transpile ES6 Modules in the Rails Asset Pipeline
JavaScript
87
star
16

capybara-extensions

Complements Capybara with additional finders and matchers.
Ruby
66
star
17

ruby-context_validations

Context Aware Validations for Rails
Ruby
65
star
18

pg_array_parser

Ruby
61
star
19

pages

Easy pages in Rails
Ruby
49
star
20

ember-skeleton

Show fast-loading temporary images in place of an eventual slow-loading image
JavaScript
41
star
21

ember-cli-proxy-fixtures

Ember CLI Proxy Fixtures
JavaScript
38
star
22

client_side_validations-mongoid

Mongoid plugin for ClientSideValidations
Ruby
28
star
23

ruby-easy_auth

Dead simple drop-in identity based Rails authentication
Ruby
28
star
24

ember-admin-bootstrap

Ember Admin with a Twitter Bootstrap Theme
CSS
25
star
25

client_side_validations-formtastic

Formtastic plugin for ClientSideValidations
Ruby
18
star
26

ember-wuphf

JavaScript
17
star
27

postgres_ext-postgis

Ruby
14
star
28

ember-cli-one-script

This addon combines your `vendor.js` and `<your-app-name>.js` into a single file called `app.js`
JavaScript
14
star
29

client_side_validations-turbolinks

Turbolinks Plugin for ClientSideValidations
Ruby
9
star
30

fixtory

Not quite fixtures, not quite factories
Ruby
9
star
31

ember-new-modules-shim

JavaScript
6
star
32

client_side_validations-backbone

Backbone plugin for ClientSideValidations
JavaScript
6
star
33

ember-cli-test-interactions

Ember acceptance test helpers.
JavaScript
3
star
34

minitest-moar

Moar Minitest Pluzsh!
Ruby
3
star
35

ruby-easy_auth-password

Password plugin for EasyAuth
Ruby
3
star
36

mail_congress

Ruby
2
star
37

ruby-easy_auth-oauth2

Ruby
1
star
38

ruby-easy_auth-twitter

Ruby
1
star
39

ruby-easy_auth-linked_in

Ruby
1
star
40

comet

Elixir
1
star