Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display language name or language key #2949

Merged
merged 7 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Display language name or language key
There where two issues with the current implementation:

- There was a possible duplication between looking up the language name in key "locale" and in key "i18n.language.name"

- The "default" option was not being picked up, as the fallback always returned the default locale's translation, "English"

With this implementation there is only a single place to put the language name: i18n.language.name. I think this place is easier to find and understand for Crowdin translators than a "locale" key hidden in general.yml

If the translation is not found we display the language key, instead of English, which makes more sense to me too 😌

Solution based on recent comments[1] on a related I18n issue

[1] ruby-i18n/i18n#365 (comment)
  • Loading branch information
voodoorai2000 committed Oct 5, 2018
commit 0a0261900c40a31fd87d0d8c765fdd8f1bc62ad0
5 changes: 1 addition & 4 deletions app/helpers/locales_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module LocalesHelper

def name_for_locale(locale)
default = I18n.t("locale", locale: locale)
I18n.backend.translate(locale, "i18n.language.name", default: default)
rescue
nil
I18n.t("i18n.language.name", locale: locale, fallback: false, default: locale.to_s)
end

end
26 changes: 26 additions & 0 deletions spec/features/localization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,30 @@
expect(page).not_to have_content('Language')
expect(page).not_to have_css('div.locale')
end

context "Missing language names" do

let!(:default_enforce) { I18n.enforce_available_locales }
let!(:default_locales) { I18n.available_locales }

before do
I18n.enforce_available_locales = false
I18n.available_locales << :wl
I18n.locale = :wl
end

after do
I18n.enforce_available_locales = default_enforce
I18n.available_locales = default_locales
end

scenario 'Available locales without language translation display locale key' do
visit '/'

within('.locale-form .js-location-changer') do
expect(page).to have_content 'wl'
end
end

end
end
32 changes: 32 additions & 0 deletions spec/helpers/locales_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

describe LocalesHelper do

context "Language names" do

let!(:default_enforce) { I18n.enforce_available_locales }

before do
I18n.enforce_available_locales = false
end

after do
I18n.backend.reload!
I18n.enforce_available_locales = default_enforce
end

it "returns the language name in i18n.language.name translation" do
keys = { language: {
name: "World Language" }}

I18n.backend.store_translations(:wl, { i18n: keys })

expect(name_for_locale(:wl)).to eq("World Language")
end

it "retuns the locale key if i18n.language.name translation is not found" do
expect(name_for_locale(:wl)).to eq("wl")
end

end
end