From e4819d8e0ba160e24c232841ee022b60fe2b8d1d Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Tue, 11 Dec 2012 08:54:43 +0900 Subject: [PATCH] Fixed I18n handlebars helpers to update when locale changes Via the console, you can Travis.set('locale', 'ja') and all the labels will properly update. This should work on User#updateLocale as well but I am having a devil of a time testing it locally as I cannot sign in. --- assets/scripts/app/helpers.coffee | 1 + assets/scripts/app/helpers/handlebars.coffee | 3 -- .../app/helpers/i18n_handlebars.coffee | 34 +++++++++++++++++++ assets/scripts/app/models/user.coffee | 1 + assets/scripts/travis.coffee | 7 ++-- 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 assets/scripts/app/helpers/i18n_handlebars.coffee diff --git a/assets/scripts/app/helpers.coffee b/assets/scripts/app/helpers.coffee index bcbd98c1..ac77424a 100644 --- a/assets/scripts/app/helpers.coffee +++ b/assets/scripts/app/helpers.coffee @@ -1,3 +1,4 @@ require 'helpers/handlebars' require 'helpers/helpers' require 'helpers/urls' +require 'helpers/i18n_handlebars' diff --git a/assets/scripts/app/helpers/handlebars.coffee b/assets/scripts/app/helpers/handlebars.coffee index 29037a28..d86aa80d 100644 --- a/assets/scripts/app/helpers/handlebars.coffee +++ b/assets/scripts/app/helpers/handlebars.coffee @@ -6,9 +6,6 @@ safe = (string) -> Handlebars.registerHelper 'tipsy', (text, tip) -> safe '' + text + '' -Handlebars.registerHelper 't', (key) -> - safe I18n.t(key) - Ember.registerBoundHelper 'capitalize', (value, options) -> if value? safe $.capitalize(value) diff --git a/assets/scripts/app/helpers/i18n_handlebars.coffee b/assets/scripts/app/helpers/i18n_handlebars.coffee new file mode 100644 index 00000000..38e6bfbc --- /dev/null +++ b/assets/scripts/app/helpers/i18n_handlebars.coffee @@ -0,0 +1,34 @@ +I18nBoundView = Ember.View.extend Ember._Metamorph, { + + key: null, + + valueDidChange: -> + return if this.morph.isRemoved() + this.morph.html(this.valueForRender()) + + valueForRender: -> + new Handlebars.SafeString I18n.t(this.key) + + init: -> + this._super() + Travis.addObserver('locale', this, 'valueDidChange') + + didInsertElement: -> + this.valueDidChange() + + destroy: -> + Travis.removeObserver('locale', this, 'valueDidChange') + this._super() + + render: (buffer) -> + buffer.push(this.valueForRender()) +} + +Ember.Handlebars.registerHelper 't', (key, options) -> + view = options.data.view + bindView = view.createChildView(I18nBoundView, { key: key }) + view.appendChild(bindView) + # dont write any content from this helper, let the child view + # take care of itself. + false + diff --git a/assets/scripts/app/models/user.coffee b/assets/scripts/app/models/user.coffee index 4194dee7..1a226d9c 100644 --- a/assets/scripts/app/models/user.coffee +++ b/assets/scripts/app/models/user.coffee @@ -32,6 +32,7 @@ require 'travis/model' ).property() updateLocale: (locale) -> + Travis.set 'locale', locale @setWithSession('locale', locale) transaction = @get('transaction') diff --git a/assets/scripts/travis.coffee b/assets/scripts/travis.coffee index 3a977000..9f244785 100644 --- a/assets/scripts/travis.coffee +++ b/assets/scripts/travis.coffee @@ -64,10 +64,7 @@ Storage = Em.Object.extend setLocale: (locale) -> return unless locale I18n.locale = locale - @storage.setItem('travis.locale', locale) - - needsLocaleChange: (locale) -> - I18n.locale != locale + Travis.set('locale', locale) storage: (-> storage = null @@ -96,7 +93,7 @@ Storage = Em.Object.extend location.href = location.href.replace('#!/', '') if location.hash.slice(0, 2) == '#!' I18n.fallbacks = true - @setLocale @storage.getItem('travis.locale') || 'en' + Travis.set 'locale', 'en' Ember.run.next this, -> app = Travis.App.create(attrs || {})