From 89add4a4a2d52f8864d735f33a75a7bdb635ab01 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 5 Feb 2016 21:49:03 +0100 Subject: [PATCH] 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. --- lib/js_locale_helper.rb | 9 +++++++++ spec/components/js_locale_helper_spec.rb | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/js_locale_helper.rb b/lib/js_locale_helper.rb index af06c334092..1374235eb4f 100644 --- a/lib/js_locale_helper.rb +++ b/lib/js_locale_helper.rb @@ -123,7 +123,16 @@ module JsLocaleHelper end 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" + + 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) File.read(filename) << "\n" end || "" diff --git a/spec/components/js_locale_helper_spec.rb b/spec/components/js_locale_helper_spec.rb index ae0077f8bee..337553f7fac 100644 --- a/spec/components/js_locale_helper_spec.rb +++ b/spec/components/js_locale_helper_spec.rb @@ -173,6 +173,16 @@ describe JsLocaleHelper do ctx.load(Rails.root + 'app/assets/javascripts/locales/i18n.js') ctx.eval(js) 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