JsLocaleHelper should search for moment.js locale files

moment.js uses a different naming conventions for locale files.
E.g. "zh-zn" instead of "zh_ZN" and "nb" instead of "nb_NO"

This change allows us to use the locale files without renaming which
makes future upgrades of moment.js a lot easier.
This commit is contained in:
Gerhard Schlager 2016-02-05 21:49:03 +01:00
parent 8ced8350ba
commit 89add4a4a2
2 changed files with 19 additions and 0 deletions

View File

@ -123,7 +123,16 @@ module JsLocaleHelper
end end
def self.moment_locale(locale_str) def self.moment_locale(locale_str)
# moment.js uses a different naming scheme for locale files
locale_str = locale_str.tr('_', '-').downcase
filename = Rails.root + "lib/javascripts/moment_locale/#{locale_str}.js" filename = Rails.root + "lib/javascripts/moment_locale/#{locale_str}.js"
unless File.exists?(filename)
# try the language without the territory
locale_str = locale_str.partition('-').first
filename = Rails.root + "lib/javascripts/moment_locale/#{locale_str}.js"
end
if File.exists?(filename) if File.exists?(filename)
File.read(filename) << "\n" File.read(filename) << "\n"
end || "" end || ""

View File

@ -173,6 +173,16 @@ describe JsLocaleHelper do
ctx.load(Rails.root + 'app/assets/javascripts/locales/i18n.js') ctx.load(Rails.root + 'app/assets/javascripts/locales/i18n.js')
ctx.eval(js) ctx.eval(js)
end end
it "finds moment.js locale file for #{locale[:value]}" do
content = JsLocaleHelper.moment_locale(locale[:value])
if (locale[:value] == 'en')
expect(content).to eq('')
else
expect(content).to_not eq('')
end
end
end end
end end