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.
This commit is contained in:
Randy Morgan 2012-12-11 08:54:43 +09:00
parent fa5de8ba66
commit e4819d8e0b
5 changed files with 38 additions and 8 deletions

View File

@ -1,3 +1,4 @@
require 'helpers/handlebars'
require 'helpers/helpers'
require 'helpers/urls'
require 'helpers/i18n_handlebars'

View File

@ -6,9 +6,6 @@ safe = (string) ->
Handlebars.registerHelper 'tipsy', (text, tip) ->
safe '<span class="tool-tip" original-title="' + tip + '">' + text + '</span>'
Handlebars.registerHelper 't', (key) ->
safe I18n.t(key)
Ember.registerBoundHelper 'capitalize', (value, options) ->
if value?
safe $.capitalize(value)

View File

@ -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

View File

@ -32,6 +32,7 @@ require 'travis/model'
).property()
updateLocale: (locale) ->
Travis.set 'locale', locale
@setWithSession('locale', locale)
transaction = @get('transaction')

View File

@ -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 || {})