ember-cli-i18n
Advanced tools
Comparing version 0.0.2 to 0.0.3
import Ember from 'ember'; | ||
var fmt = Ember.String.fmt; | ||
var get = Ember.get; | ||
var fmt = Ember.String.fmt; | ||
var bind = Ember.run.bind; | ||
export default function t() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var path = args.shift(); | ||
var options = args.pop(); | ||
import { read, readArray } from 'ember-cli-i18n/utils/stream'; | ||
var container = options.data.view.container; | ||
var countryCode = container.lookup('application:main').defaultLocale; | ||
function T(attributes) { | ||
for(var key in attributes) { | ||
this[key] = attributes[key]; | ||
} | ||
this.t = function(path, values) { | ||
var application = this.container.lookup('application:main'); | ||
var countryCode = application.localeStream.value(); | ||
var locale; | ||
var result; | ||
var locale = container.lookupFactory('locale:' + countryCode); | ||
if (!Ember.isArray(values)) { | ||
values = Array.prototype.slice.call(arguments, 1); | ||
} | ||
var result = get(locale, path); | ||
if (countryCode) { | ||
locale = this.lookupLocale(countryCode); | ||
} | ||
result = fmt(result, args); | ||
if (!locale) { | ||
countryCode = application.defaultLocale; | ||
locale = this.lookupLocale(countryCode); | ||
} | ||
return result; | ||
result = get(locale, read(path)); | ||
Ember.assert('Missing translation for key "' + path + '".', result); | ||
Ember.assert('Translation for key "' + path + '" is not a string.', Ember.typeOf(result) === 'string'); | ||
return fmt(result, readArray(values)); | ||
}; | ||
this.lookupLocale = function(countryCode) { | ||
return this.container.lookupFactory('locale:' + countryCode); | ||
}; | ||
} | ||
T.create = function(attributes) { | ||
var t = new T(attributes); | ||
var fn = bind(t, t.t); | ||
fn.destroy = function() {}; | ||
return fn; | ||
}; | ||
export default T; |
import Ember from 'ember'; | ||
import t from 'ember-cli-i18n/utils/t'; | ||
import Stream from 'ember-cli-i18n/utils/stream'; | ||
export default Ember.Handlebars.makeBoundHelper(t); | ||
// _SimpleHandlebarsView in 1.9 and _SimpleBoundView in 1.10 | ||
var SimpleBoundView = Ember._SimpleHandlebarsView || Ember._SimpleBoundView; | ||
export default function tHelper() { | ||
var args = Array.prototype.slice.call(arguments); | ||
var path = args.shift(); | ||
var options = args.pop(); | ||
var view = options.data.view; | ||
var container = view.container; | ||
var t = container.lookup('utils:t'); | ||
var application = container.lookup('application:main'); | ||
var types = options.types; | ||
// parse input params and streamify | ||
for (var i = 0, l = args.length; i < l; i++) { | ||
// (starting at 1 because we popped path off already | ||
if (types[i + 1] === 'ID') { | ||
args[i] = view.getStream(args[i]); | ||
} | ||
} | ||
// convert path into a stream | ||
if (types[0] === 'ID') { | ||
path = view.getStream(path); | ||
} | ||
var stream = new Stream(function() { | ||
return t(path, args); | ||
}); | ||
var childView = new SimpleBoundView(stream, options.escaped); | ||
stream.subscribe(view._wrapAsScheduled(function(){ | ||
Ember.run.scheduleOnce('render', childView, 'rerender'); | ||
})); | ||
application.localeStream.subscribe(stream.notify, stream); | ||
if (path.isStream) { | ||
path.subscribe(stream.notify, stream); | ||
} | ||
view.appendChild(childView); | ||
} |
@@ -0,5 +1,22 @@ | ||
import Ember from 'ember'; | ||
import T from 'ember-cli-i18n/utils/t'; | ||
import tHelper from '../helpers/t'; | ||
import Stream from 'ember-cli-i18n/utils/stream'; | ||
export function initialize(/* container, application */) { | ||
export function initialize(container, application) { | ||
Ember.Handlebars.registerHelper('t', tHelper); | ||
application.localeStream = new Stream(function() { | ||
return application.get('locale'); | ||
}); | ||
Ember.addObserver(application, 'locale', function() { | ||
application.localeStream.notify(); | ||
}); | ||
application.register('utils:t', T); | ||
application.inject('route', 't', 'utils:t'); | ||
application.inject('model', 't', 'utils:t'); | ||
application.inject('component', 't', 'utils:t'); | ||
application.inject('controller', 't', 'utils:t'); | ||
}; | ||
@@ -6,0 +23,0 @@ |
{ | ||
"name": "ember-cli-i18n", | ||
"dependencies": { | ||
"handlebars": "~1.3.0", | ||
"handlebars": "~2.0.0", | ||
"jquery": "^1.11.1", | ||
"ember": "1.7.0", | ||
"ember-data": "1.0.0-beta.10", | ||
"ember-resolver": "~0.1.7", | ||
"ember": "1.9.0", | ||
"ember-data": "1.0.0-beta.12", | ||
"ember-resolver": "~0.1.10", | ||
"loader.js": "stefanpenner/loader.js#1.0.1", | ||
@@ -17,2 +17,2 @@ "ember-cli-shims": "stefanpenner/ember-cli-shims#0.0.3", | ||
} | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
/* jshint node: true */ | ||
/* global require, module */ | ||
@@ -2,0 +3,0 @@ |
@@ -0,1 +1,2 @@ | ||
/* jshint node: true */ | ||
'use strict'; | ||
@@ -2,0 +3,0 @@ |
{ | ||
"name": "ember-cli-i18n", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Simple Internationalization support for ember-cli apps.", | ||
@@ -21,12 +21,14 @@ "directories": { | ||
"devDependencies": { | ||
"body-parser": "^1.2.0", | ||
"broccoli-asset-rev": "0.3.1", | ||
"broccoli-asset-rev": "^1.0.0", | ||
"broccoli-ember-hbs-template-compiler": "^1.6.1", | ||
"ember-cli": "0.1.2", | ||
"ember-cli": "0.1.4", | ||
"ember-cli-htmlbars": "^0.5.1", | ||
"ember-cli-content-security-policy": "0.3.0", | ||
"ember-export-application-global": "^1.0.0", | ||
"ember-cli-dependency-checker": "0.0.6", | ||
"ember-cli-esnext": "0.1.1", | ||
"ember-cli-ic-ajax": "0.1.1", | ||
"ember-cli-inject-live-reload": "^1.3.0", | ||
"ember-cli-qunit": "0.1.0", | ||
"ember-data": "1.0.0-beta.10", | ||
"ember-cli-qunit": "0.1.2", | ||
"ember-data": "1.0.0-beta.12", | ||
"ember-export-application-global": "^1.0.0", | ||
"express": "^4.8.5", | ||
@@ -33,0 +35,0 @@ "glob": "^4.0.5" |
@@ -32,2 +32,16 @@ # Ember CLI i18n | ||
`defaultLocale` is only the fallback. If you wanted to change the locale | ||
of the application you should modify your application's `locale`: | ||
```js | ||
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 | ||
@@ -65,3 +79,4 @@ | ||
export default { | ||
age: 'You are %@1 years old.' | ||
age: 'You are %@1 years old.', | ||
name: '%@, %@' | ||
}; | ||
@@ -99,6 +114,6 @@ ``` | ||
import Ember from 'ember'; | ||
import t from 'ember-cli-i18n'; | ||
export default Ember.Object.extend({ | ||
foo: function() { | ||
var t = container.lookup('utils:t'); | ||
return t('foo.bar'); | ||
@@ -109,2 +124,14 @@ } | ||
`t` is automatically injected into **Controllers**, **Components**, | ||
**Routes**, and **Models**: | ||
```javascript | ||
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 ## | ||
@@ -111,0 +138,0 @@ |
@@ -20,7 +20,7 @@ import Ember from 'ember'; | ||
var span = find('span.one'); | ||
equal('bar', span.text()); | ||
equal(span.text(), 'bar'); | ||
}); | ||
}); | ||
test('no bound arguments', function() { | ||
test('with bound arguments', function() { | ||
visit('/'); | ||
@@ -30,4 +30,25 @@ | ||
var span = find('span.two'); | ||
equal('You are 35 years old', span.text()); | ||
equal(span.text(), 'You are 35 years old'); | ||
}); | ||
}); | ||
test('changing application locale', function() { | ||
visit('/'); | ||
andThen(function() { | ||
var span = find('span.two'); | ||
equal(span.text(), 'You are 35 years old'); | ||
}); | ||
andThen(function() { | ||
App.set('locale', 'es'); | ||
}); | ||
andThen(function() { | ||
var spanOne = find('span.one'); | ||
equal(spanOne.text(), 'es_bar'); | ||
var spanTwo = find('span.two'); | ||
equal(spanTwo.text(), 'es_You are 35 years old'); | ||
}); | ||
}); |
@@ -34,3 +34,3 @@ /* jshint node: true */ | ||
ENV.baseURL = '/'; | ||
ENV.locationType = 'auto'; | ||
ENV.locationType = 'none'; | ||
@@ -37,0 +37,0 @@ // keep test console output quieter |
@@ -1,3 +0,2 @@ | ||
# robotstxt.org/ | ||
# http://www.robotstxt.org | ||
User-agent: * |
@@ -12,6 +12,2 @@ import Ember from 'ember'; | ||
Router.reopen({ | ||
location: 'none' | ||
}); | ||
Ember.run(function() { | ||
@@ -23,5 +19,3 @@ App = Application.create(attributes); | ||
App.reset(); // this shouldn't be needed, i want to be able to "start an app at a specific URL" | ||
return App; | ||
} |
@@ -6,8 +6,15 @@ import Ember from 'ember'; | ||
module('TInitializer', { | ||
module('T Initializer', { | ||
setup: function() { | ||
Ember.run(function() { | ||
container = new Ember.Container(); | ||
application = Ember.Application.create(); | ||
container = application.__container__; | ||
application.defaultLocale = 'en'; | ||
container.register('locale:en', { | ||
foo: 'bar' | ||
}); | ||
application.deferReadiness(); | ||
initialize(container, application); | ||
}); | ||
@@ -17,9 +24,50 @@ } | ||
// Replace this with your real tests. | ||
test('it works', function() { | ||
initialize(container, application); | ||
test('t gets injected into controllers', function() { | ||
var FooController = Ember.ObjectController.extend({ | ||
foo: Ember.computed(function() { | ||
return this.t('foo'); | ||
}) | ||
}); | ||
container.register('controller:foo', FooController); | ||
// you would normally confirm the results of the initializer here | ||
ok(true); | ||
var fooControllerInstance = container.lookup('controller:foo'); | ||
equal(fooControllerInstance.get('foo'), 'bar'); | ||
}); | ||
test('t gets injected into routes', function() { | ||
var FooRoute = Ember.Route.extend({ | ||
foo: Ember.computed(function() { | ||
return this.t('foo'); | ||
}) | ||
}); | ||
container.register('route:foo', FooRoute); | ||
var fooRouteInstance = container.lookup('route:foo'); | ||
equal(fooRouteInstance.get('foo'), 'bar'); | ||
}); | ||
test('t gets injected into models', function() { | ||
var FooModel = DS.Model.extend({ | ||
foo: Ember.computed(function() { | ||
return this.t('foo'); | ||
}) | ||
}); | ||
container.register('model:foo', FooModel); | ||
Ember.run(function() { | ||
var fooModelInstance = container.lookup('store:main').createRecord('foo'); | ||
equal(fooModelInstance.get('foo'), 'bar'); | ||
}); | ||
}); | ||
test('t gets injected into components', function() { | ||
var FooComponent = Ember.Component.extend({ | ||
foo: Ember.computed(function() { | ||
return this.t('foo'); | ||
}) | ||
}); | ||
container.register('component:foo', FooComponent); | ||
var fooComponentInstance = container.lookup('component:foo'); | ||
equal(fooComponentInstance.get('foo'), 'bar'); | ||
}); |
@@ -1,2 +0,2 @@ | ||
import t from 'ember-cli-i18n/utils/t'; | ||
import T from 'ember-cli-i18n/utils/t'; | ||
import Ember from 'ember'; | ||
@@ -6,3 +6,3 @@ | ||
var application; | ||
var options; | ||
var t; | ||
@@ -32,3 +32,4 @@ /*globals define, require, requirejs*/ | ||
}, | ||
number: 'Number: %@1' | ||
number: 'Number: %@1', | ||
name: '%@ %@' | ||
}; | ||
@@ -47,3 +48,3 @@ }); | ||
module('t Helper', { | ||
module('t utility function', { | ||
setup: function() { | ||
@@ -55,3 +56,10 @@ requirejs.backup(); | ||
application = {}; | ||
application = { | ||
localeStream: { | ||
value: function() { | ||
return application.locale; | ||
}, | ||
subscribe: function () {} | ||
} | ||
}; | ||
@@ -68,9 +76,3 @@ container = new Ember.Container(); | ||
options = { | ||
data: { | ||
view: { | ||
container: container | ||
} | ||
} | ||
}; | ||
t = T.create({container: container}); | ||
}, | ||
@@ -86,4 +88,3 @@ teardown: function() { | ||
var result = t('foo', options); | ||
equal(result, 'bar'); | ||
equal(t('foo'), 'bar'); | ||
}); | ||
@@ -94,4 +95,3 @@ | ||
var result = t('foo', options); | ||
equal(result, 'baz'); | ||
equal(t('foo'), 'baz'); | ||
}); | ||
@@ -102,4 +102,3 @@ | ||
var result = t('home.title', options); | ||
equal(result, 'Welcome'); | ||
equal(t('home.title'), 'Welcome'); | ||
}); | ||
@@ -110,4 +109,34 @@ | ||
var result = t('number', 5, options); | ||
equal(result, 'Number: 5'); | ||
equal(t('number', 5), 'Number: 5'); | ||
}); | ||
test('prefers locale to defaultLocale', function() { | ||
application.defaultLocale = 'en'; | ||
application.locale = 'fr'; | ||
equal(t('foo'), 'baz'); | ||
}); | ||
test('can take value arguments', function() { | ||
application.defaultLocale = 'en'; | ||
equal(t('name', 'John', 'Doe'), 'John Doe'); | ||
}); | ||
test('can take array arguments', function() { | ||
application.defaultLocale = 'en'; | ||
equal(t('name', ['John', 'Doe']), 'John Doe'); | ||
}); | ||
test('throws on missing keys', function() { | ||
application.defaultLocale = 'en'; | ||
throws(function() { t('missing'); }); | ||
}); | ||
test('throws on non-string values', function() { | ||
application.defaultLocale = 'en'; | ||
throws(function() { t('home'); }); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26894
55
508
156
14
3