Skip to content

Commit

Permalink
Fix Faker localization for rake tasks
Browse files Browse the repository at this point in the history
Was breaking rake db:seed file
  • Loading branch information
Bodacious committed Sep 14, 2018
1 parent 753a2e9 commit 0ef9a80
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ addons:
- nodejs
- wkhtmltopdf

addons:
artifacts:
s3_region: "eu-west-2"

matrix:
fast_finish: true
include:
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/fast_gettext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand Down
18 changes: 10 additions & 8 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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) }

Expand Down
23 changes: 14 additions & 9 deletions lib/locale_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
63 changes: 63 additions & 0 deletions spec/lib/locale_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions spec/lib/locale_set_spec.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions spec/support/faker.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0ef9a80

Please sign in to comment.