diff --git a/.travis.yml b/.travis.yml index e16eac779..7f367db23 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,10 @@ addons: - nodejs - wkhtmltopdf + addons: + artifacts: + s3_region: "eu-west-2" + matrix: fast_finish: true include: diff --git a/config/initializers/fast_gettext.rb b/config/initializers/fast_gettext.rb index 06d97c30c..25dc19aaf 100644 --- a/config/initializers/fast_gettext.rb +++ b/config/initializers/fast_gettext.rb @@ -15,7 +15,7 @@ def default_locale end def available_locales - Rails.application.config.i18n.available_locales = LocaleSet.new(['en-GB']) + Rails.application.config.i18n.available_locales = LocaleSet.new(['en-GB', 'en']) end end @@ -26,7 +26,7 @@ def available_locales report_warning: false, }) -I18n.available_locales = available_locales.for(:i18n) +I18n.available_locales += available_locales.for(:i18n) FastGettext.default_available_locales = available_locales.for(:fast_gettext) FastGettext.default_text_domain = 'app' diff --git a/db/seeds.rb b/db/seeds.rb index 4295d5a70..3987a81f8 100755 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -12,9 +12,11 @@ include FactoryBot::Syntax::Methods -I18n.locale = LocaleFormatter.new("en_GB", format: :i18n).to_s -Faker::Config.locale = LocaleFormatter.new("en_GB", format: :i18n).to_s -FastGettext.default_locale = LocaleFormatter.new("en_GB", format: :fast_gettext).to_s +I18n.available_locales = ['en', 'en-GB', 'de', 'fr'] +I18n.locale = LocaleFormatter.new(:en, format: :i18n).to_s +# Keep this as :en. Faker doesn't have :en-GB +Faker::Config.locale = LocaleFormatter.new(:en, format: :i18n).to_s +FastGettext.default_locale = LocaleFormatter.new(:en, format: :fast_gettext).to_s require 'factory_bot' @@ -84,11 +86,11 @@ # Languages (check config/locales for any ones not defined here) # ------------------------------------------------------- languages = [ - {abbreviation: 'en_GB', + {abbreviation: 'en-GB', description: '', name: 'English (GB)', default_language: true}, - {abbreviation: 'en_US', + {abbreviation: 'en-US', description: '', name: 'English (US)', default_language: false}, @@ -221,16 +223,16 @@ {name: Branding.fetch(:organisation, :name), abbreviation: Branding.fetch(:organisation, :abbreviation), org_type: 4, links: {"org":[]}, - language: Language.find_by(abbreviation: 'en_GB'), + language: Language.find_by(abbreviation: 'en-GB'), token_permission_types: TokenPermissionType.all}, {name: 'Government Agency', abbreviation: 'GA', org_type: 2, links: {"org":[]}, - language: Language.find_by(abbreviation: 'en_GB')}, + language: Language.find_by(abbreviation: 'en-GB')}, {name: 'University of Exampleland', abbreviation: 'UOS', org_type: 1, links: {"org":[]}, - language: Language.find_by(abbreviation: 'en_GB')} + language: Language.find_by(abbreviation: 'en-GB')} ] orgs.map{ |o| create(:org, o) } diff --git a/lib/locale_formatter.rb b/lib/locale_formatter.rb index 6416a089a..e2590c46e 100644 --- a/lib/locale_formatter.rb +++ b/lib/locale_formatter.rb @@ -19,12 +19,26 @@ class LocaleFormatter # Regex to extract the components (language and region) from a locale String COMPONENT_FORMAT = /[a-z]{2}/i + # The format to modify the String in + # + # Returns Symbol + attr_reader :format + + # The formatted locale as a string + # + # Returns String + attr_reader :string + + alias to_s string + # Takes a given locale string and formats it properly for the desired framework # # string - A locale String (e.g. "en_GB", "en-GB", "en", :en) # format - A Symbol representing the desired translation framework (defaults: :i18n) # def initialize(string, format: :i18n) + @format = format + language, region = string.to_s.scan(COMPONENT_FORMAT) join_char = format.to_sym == :fast_gettext ? FAST_GETTEXT_JOIN : I18N_JOIN @@ -38,13 +52,4 @@ def initialize(string, format: :i18n) end end - # The formatted locale as a string - # - # Returns String - def string - @string - end - - alias to_s string - end diff --git a/spec/lib/locale_formatter_spec.rb b/spec/lib/locale_formatter_spec.rb new file mode 100644 index 000000000..c8da5b51b --- /dev/null +++ b/spec/lib/locale_formatter_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +RSpec.describe LocaleFormatter do + + context "#format" do + + subject { LocaleFormatter.new('en_GB').format } + + it "defaults to :i18n" do + expect(subject).to eql(:i18n) + end + + end + + describe "#string" do + + context "when format is :i18n" do + + subject { LocaleFormatter.new(locale_string, format: format).string } + + let(:locale_string) { 'HH_xx' } + + let!(:format) { :i18n } + + it "forces the hyphenated format" do + expect(subject).to eql("hh-XX") + end + + it "downcases the language component" do + expect(subject).to start_with('hh') + end + + it "upcases the region" do + expect(subject).to end_with('XX') + end + + end + + context "when format is :fast_gettext" do + + subject { LocaleFormatter.new(locale_string, format: format).string } + + let(:locale_string) { 'HH-xx' } + + let!(:format) { :fast_gettext } + + it "forces the underescore format" do + expect(subject).to eql("hh_XX") + end + + it "downcases the language component" do + expect(subject).to start_with('hh') + end + + it "upcases the region" do + expect(subject).to end_with('XX') + end + + end + + end + +end diff --git a/spec/lib/locale_set_spec.rb b/spec/lib/locale_set_spec.rb new file mode 100644 index 000000000..946c9368d --- /dev/null +++ b/spec/lib/locale_set_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +RSpec.describe LocaleSet do + + describe "#for" do + + subject { LocaleSet.new(['en_gb', 'EN-US', :es, :fr]).for(format) } + + let!(:format) { :i18n } + + it "converts each item to a string" do + subject.each do |item| + expect(item).to be_a(String) + end + end + + it "removes duplicate items" do + @locale_set = LocaleSet.new([:es, :es]) + expect(@locale_set).to have(1).item + end + + context "when format is :i18n" do + + let!(:format) { :i18n } + + it "returns each item in i18n format" do + expect(subject).to eql(['en-GB', 'en-US', 'es', 'fr']) + end + + end + + context "when format is :fast_gettext" do + + let!(:format) { :fast_gettext } + + it "returns each item in fast_gettext format" do + expect(subject).to eql(['en_GB', 'en_US', 'es', 'fr']) + end + + end + + end + +end diff --git a/spec/support/faker.rb b/spec/support/faker.rb index 2604000e5..3f35c1f8b 100644 --- a/spec/support/faker.rb +++ b/spec/support/faker.rb @@ -1,11 +1,12 @@ require 'faker' -LOCALE = 'en-GB' +# Keep this as :en. Faker doesn't have :en-GB +LOCALE = 'en' RSpec.configure do |config| config.before(:each) do - I18n.locale = LOCALE - Faker::Config.locale = LOCALE - FastGettext.default_locale = LOCALE + I18n.locale = LocaleFormatter.new(LOCALE, format: :i18n).to_s + Faker::Config.locale = LocaleFormatter.new(LOCALE, format: :i18n).to_s + FastGettext.default_locale = LocaleFormatter.new(LOCALE, format: :fast_gettext).to_s end end