spec/i18n_js_spec.rb in i18n-js-3.0.0.rc9 vs spec/i18n_js_spec.rb in i18n-js-3.0.0.rc10

- old
+ new

@@ -1,8 +1,9 @@ require "spec_helper" describe I18n::JS do + describe '.config_file_path' do let(:default_path) { I18n::JS::DEFAULT_CONFIG_PATH } let(:new_path) { File.join("tmp", default_path) } subject { described_class.config_file_path } @@ -60,10 +61,23 @@ set_config "js_file_per_locale.yml" I18n::JS.export file_should_exist "en.js" file_should_exist "fr.js" + + en_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "en.js")) + expect(en_output).to eq(<<EOS +I18n.translations || (I18n.translations = {}); +I18n.translations["en"] = {"admin":{"edit":{"title":"Edit"},"show":{"note":"more details","title":"Show"}},"date":{"abbr_day_names":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbr_month_names":[null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"day_names":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"},"month_names":[null,"January","February","March","April","May","June","July","August","September","October","November","December"]}}; +EOS +) + fr_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "fr.js")) + expect(fr_output).to eq(<<EOS +I18n.translations || (I18n.translations = {}); +I18n.translations["fr"] = {"admin":{"edit":{"title":"Editer"},"show":{"note":"plus de détails","title":"Visualiser"}},"date":{"abbr_day_names":["dim","lun","mar","mer","jeu","ven","sam"],"abbr_month_names":[null,"jan.","fév.","mar.","avr.","mai","juin","juil.","août","sept.","oct.","nov.","déc."],"day_names":["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"],"formats":{"default":"%d/%m/%Y","long":"%e %B %Y","long_ordinal":"%e %B %Y","only_day":"%e","short":"%e %b"},"month_names":[null,"janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"]}}; +EOS +) end it "exports with multiple conditions" do set_config "multiple_conditions.yml" I18n::JS.export @@ -75,17 +89,26 @@ allow(::I18n).to receive(:available_locales){ [:en, :fr] } set_config "multiple_conditions_per_locale.yml" result = I18n::JS.translation_segments - result.map(&:file).should eql(["tmp/i18n-js/bits.en.js", "tmp/i18n-js/bits.fr.js"]) + result.map(&:file).should eql(["tmp/i18n-js/bits.%{locale}.js"]) - %w(en fr).each do |lang| - segment = result.select{|s| s.file == "tmp/i18n-js/bits.#{lang}.js"}.first - segment.translations.keys.should eql([lang.to_sym]) - segment.translations[lang.to_sym].keys.sort.should eql([:date, :number]) - end + result.map(&:save!) + + en_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "bits.en.js")) + expect(en_output).to eq(<<EOS +I18n.translations || (I18n.translations = {}); +I18n.translations["en"] = {"date":{"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"}},"number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}}}}; +EOS +) + fr_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "bits.fr.js")) + expect(fr_output).to eq(<<EOS +I18n.translations || (I18n.translations = {}); +I18n.translations["fr"] = {"date":{"formats":{"default":"%d/%m/%Y","long":"%e %B %Y","long_ordinal":"%e %B %Y","only_day":"%e","short":"%e %b"}},"number":{"currency":{"format":{"format":"%n %u","precision":2,"unit":"€"}}}}; +EOS +) end it "exports with :except condition" do set_config "except_condition.yml" I18n::JS.export @@ -136,73 +159,130 @@ result[:fr][:admin][:show][:title].should eql("Visualiser") result[:en][:admin][:edit][:title].should eql("Edit") result[:fr][:admin][:edit][:title].should eql("Editer") end + + describe ".filtered_translations" do + subject do + I18n::JS.filtered_translations + end + + let!(:old_sort_translation_keys) { I18n::JS.sort_translation_keys? } + before { I18n::JS.sort_translation_keys = sort_translation_keys_value } + after { I18n::JS.sort_translation_keys = old_sort_translation_keys } + before { expect(I18n::JS.sort_translation_keys?).to eq(sort_translation_keys_value) } + + let(:sorted_hash) do + {sorted: :hash} + end + before do + allow(I18n::JS::Utils). + to receive(:deep_key_sort). + and_return(sorted_hash) + end + + shared_examples_for ".filtered_translations" do + subject do + I18n::JS.filtered_translations + end + + # This example is to prevent the regression from + # PR https://github.com/fnando/i18n-js/pull/318 + it {should be_a(Hash)} + # Might need to test the keys... or not + end + + context "when translation keys SHOULD be sorted" do + let(:sort_translation_keys_value) { true } + + it_behaves_like ".filtered_translations" + it {should eq(sorted_hash)} + end + context "when translation keys should NOT be sorted" do + let(:sort_translation_keys_value) { false } + + it_behaves_like ".filtered_translations" + it {should_not eq(sorted_hash)} + end + end end context "exceptions" do - it "does not include a key listed in the exceptions list" do - result = I18n::JS.scoped_translations("*", ['admin']) + it "does not include scopes listed in the exceptions list" do + result = I18n::JS.scoped_translations("*", ['de.*', '*.admin', '*.*.currency']) + result[:de].should be_empty + result[:en][:admin].should be_nil result[:fr][:admin].should be_nil + result[:ja][:admin].should be_nil + + result[:en][:number][:currency].should be_nil + result[:fr][:number][:currency].should be_nil end - it "does not include multiple keys listed in the exceptions list" do - result = I18n::JS.scoped_translations("*", ['title', 'note']) + it "does not include scopes listed in the exceptions list and respects the 'only' option" do + result = I18n::JS.scoped_translations("fr.*", ['*.admin', '*.*.currency']) - result[:en][:admin][:show].should be_empty - result[:en][:admin][:edit].should be_empty + result[:en].should be_nil + result[:de].should be_nil + result[:ja].should be_nil - result[:fr][:admin][:show].should be_empty - result[:fr][:admin][:show].should be_empty - result[:fr][:admin][:edit].should be_empty + result[:fr][:admin].should be_nil + result[:fr][:number][:currency].should be_nil + + result[:fr][:time][:am].should be_a(String) end - it "does not include a key listed in the exceptions list and respecs the 'only' option" do - result = I18n::JS.scoped_translations("fr.*", ['date', 'time', 'number', 'show']) + it "does exclude absolute scopes listed in the exceptions list" do + result = I18n::JS.scoped_translations("*", ['de', 'en.admin', 'fr.number.currency']) - result[:en].should be_nil result[:de].should be_nil - result[:ja].should be_nil - result[:fr][:date].should be_nil - result[:fr][:time].should be_nil - result[:fr][:number].should be_nil - result[:fr][:admin][:show].should be_nil + result[:en].should be_a(Hash) + result[:en][:admin].should be_nil - result[:fr][:admin][:edit][:title].should be_a(String) + result[:fr][:number].should be_a(Hash) + result[:fr][:number][:currency].should be_nil end + + it "does not exclude non-absolute scopes listed in the exceptions list" do + result = I18n::JS.scoped_translations("*", ['admin', 'currency']) + + result[:en][:admin].should be_a(Hash) + result[:fr][:admin].should be_a(Hash) + result[:ja][:admin].should be_a(Hash) + + result[:en][:number][:currency].should be_a(Hash) + result[:fr][:number][:currency].should be_a(Hash) + end end context "fallbacks" do subject do - I18n::JS.translation_segments.inject({}) do |hash, segment| - hash[segment.file] = segment.translations - hash - end + I18n::JS.translation_segments.first.translations end it "exports without fallback when disabled" do set_config "js_file_per_locale_without_fallbacks.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql(nil) + subject[:fr][:fallback_test].should eql(nil) end it "exports with default_locale as fallback when enabled" do set_config "js_file_per_locale_with_fallbacks_enabled.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql("Success") + subject[:fr][:fallback_test].should eql("Success") end it "exports with default_locale as fallback when enabled with :default_locale" do set_config "js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql("Success") + subject[:fr][:fallback_test].should eql("Success") end it "exports with given locale as fallback" do set_config "js_file_per_locale_with_fallbacks_as_locale.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql("Erfolg") + subject[:fr][:fallback_test].should eql("Erfolg") end context "with I18n::Fallbacks enabled" do let(:backend_with_fallbacks) { backend_class_with_fallbacks.new } let!(:old_backebad) { I18n.backend } @@ -213,21 +293,21 @@ end after { I18n.backend = old_backebad } it "exports with defined locale as fallback when enabled" do set_config "js_file_per_locale_with_fallbacks_enabled.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql("Erfolg") + subject[:fr][:fallback_test].should eql("Erfolg") end it "exports with defined locale as fallback when enabled with :default_locale" do set_config "js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql("Success") + subject[:fr][:fallback_test].should eql("Success") end it "exports with Fallbacks as Hash" do set_config "js_file_per_locale_with_fallbacks_as_hash.yml" - subject["tmp/i18n-js/fr.js"][:fr][:fallback_test].should eql("Erfolg") + subject[:fr][:fallback_test].should eql("Erfolg") end end end context "namespace and pretty_print options" do @@ -261,10 +341,11 @@ }$/) end end context "I18n.available_locales" do + context "when I18n.available_locales is not set" do it "should allow all locales" do result = I18n::JS.scoped_translations("*.admin.*.title") result[:en][:admin][:show][:title].should eql("Show") @@ -398,7 +479,89 @@ before { described_class.export_i18n_js_dir_path = nil } it { should eq :none } end end + end + + describe "translation key sorting" do + + describe ".sort_translation_keys?" do + after { described_class.send(:remove_instance_variable, :@sort_translation_keys) } + subject { described_class.sort_translation_keys? } + + + context "set with config" do + + context 'when :sort_translation_keys is not set in config' do + before :each do + set_config "default.yml" + end + + it { should eq true } + end + + context 'when :sort_translation_keys set to true in config' do + before :each do + set_config "js_sort_translation_keys_true.yml" + end + + it { should eq true } + end + + context 'when :sort_translation_keys set to false in config' do + before :each do + set_config "js_sort_translation_keys_false.yml" + end + + it { should eq false } + end + end + + context 'set by .sort_translation_keys' do + + context "when it is not set" do + it { should eq true } + end + + context "when it is set to true" do + before { described_class.sort_translation_keys = true } + + it { should eq true } + end + + context "when it is set to false" do + before { described_class.sort_translation_keys = false } + + it { should eq false } + end + end + end + + context "exporting" do + subject do + I18n::JS.export + file_should_exist "en.js" + File.read(File.join(I18n::JS.export_i18n_js_dir_path, "en.js")) + end + + before do + stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path) + end + + context 'sort_translation_keys is true' do + before :each do + set_config "js_sort_translation_keys_true.yml" + end + + it "exports with the keys sorted" do + expect(subject).to eq(<<EOS +I18n.translations || (I18n.translations = {}); +I18n.translations["en"] = {"admin":{"edit":{"title":"Edit"},"show":{"note":"more details","title":"Show"}},"date":{"abbr_day_names":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbr_month_names":[null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"day_names":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"},"month_names":[null,"January","February","March","April","May","June","July","August","September","October","November","December"]},"fallback_test":"Success","foo":"Foo","number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}},"format":{"delimiter":",","precision":3,"separator":"."}},"time":{"am":"am","formats":{"default":"%a, %d %b %Y %H:%M:%S %z","long":"%B %d, %Y %H:%M","short":"%d %b %H:%M"},"pm":"pm"}}; +EOS +) + end + end + end + end end