From 0a0261900c40a31fd87d0d8c765fdd8f1bc62ad0 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Thu, 4 Oct 2018 14:25:17 +0200 Subject: [PATCH 1/7] 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 :relieved: Solution based on recent comments[1] on a related I18n issue [1] https://github.com/svenfuchs/i18n/issues/365#issuecomment-419263847 --- app/helpers/locales_helper.rb | 5 +---- spec/features/localization_spec.rb | 26 +++++++++++++++++++++++ spec/helpers/locales_helper_spec.rb | 32 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 spec/helpers/locales_helper_spec.rb diff --git a/app/helpers/locales_helper.rb b/app/helpers/locales_helper.rb index 78ab25189..3ae6bb5ef 100644 --- a/app/helpers/locales_helper.rb +++ b/app/helpers/locales_helper.rb @@ -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 diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb index eb345cb7c..255847991 100644 --- a/spec/features/localization_spec.rb +++ b/spec/features/localization_spec.rb @@ -48,4 +48,30 @@ feature 'Localization' do 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 diff --git a/spec/helpers/locales_helper_spec.rb b/spec/helpers/locales_helper_spec.rb new file mode 100644 index 000000000..2eb10be05 --- /dev/null +++ b/spec/helpers/locales_helper_spec.rb @@ -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 \ No newline at end of file From 66c9f6711bc05b3c29a9cf5b14a9fc8e7ce86ef1 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Thu, 4 Oct 2018 16:35:33 +0200 Subject: [PATCH 2/7] Removes obsolete locale keys for language names --- config/locales/en/general.yml | 1 - config/locales/es/general.yml | 1 - config/locales/fr/general.yml | 1 - config/locales/he/general.yml | 1 - config/locales/it/general.yml | 1 - config/locales/nl/general.yml | 1 - config/locales/pt-BR/general.yml | 1 - config/locales/val/general.yml | 1 - 8 files changed, 8 deletions(-) diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 5afd4a9cd..5de10978f 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -249,7 +249,6 @@ en: text_sign_in: login text_sign_up: sign up title: How I can comment this document? - locale: English notifications: index: empty_notifications: You don't have new notifications. diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 02f60f8e6..ddab82f57 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -249,7 +249,6 @@ es: text_sign_in: iniciar sesión text_sign_up: registrarte title: '¿Cómo puedo comentar este documento?' - locale: Español notifications: index: empty_notifications: No tienes notificaciones nuevas. diff --git a/config/locales/fr/general.yml b/config/locales/fr/general.yml index cf1488375..e0c603a51 100644 --- a/config/locales/fr/general.yml +++ b/config/locales/fr/general.yml @@ -892,7 +892,6 @@ fr: open_gov: Gouvernement ouvert proposals: Propositions spending_proposals: Propositions de dépense - locale: Français mailers: comment: hi: Bonjour diff --git a/config/locales/he/general.yml b/config/locales/he/general.yml index 96b7702d1..115100058 100644 --- a/config/locales/he/general.yml +++ b/config/locales/he/general.yml @@ -192,7 +192,6 @@ he: poll_questions: הצבעות budgets: מימון השתתפותי spending_proposals: הצעות להוצאת כספים - locale: עברית notifications: index: empty_notifications: אין לך תגובות חדשות diff --git a/config/locales/it/general.yml b/config/locales/it/general.yml index c0309e872..d2c01bb15 100644 --- a/config/locales/it/general.yml +++ b/config/locales/it/general.yml @@ -244,7 +244,6 @@ it: text_sign_in: "" text_sign_up: "" title: "" - locale: "" notifications: index: comments_on: diff --git a/config/locales/nl/general.yml b/config/locales/nl/general.yml index 62221c280..e6cb63558 100644 --- a/config/locales/nl/general.yml +++ b/config/locales/nl/general.yml @@ -232,7 +232,6 @@ nl: text_sign_in: log in text_sign_up: registreer title: Hoe kan ik reageren? - locale: Nederlands notifications: index: comments_on: diff --git a/config/locales/pt-BR/general.yml b/config/locales/pt-BR/general.yml index 0bc8ff9fc..fe0f22b42 100644 --- a/config/locales/pt-BR/general.yml +++ b/config/locales/pt-BR/general.yml @@ -879,7 +879,6 @@ pt-BR: open_gov: Governo aberto proposals: Propostas spending_proposals: Propostas de despesas - locale: Português brasileiro mailers: comment: hi: Olá diff --git a/config/locales/val/general.yml b/config/locales/val/general.yml index 7a01cfbb9..d44478714 100644 --- a/config/locales/val/general.yml +++ b/config/locales/val/general.yml @@ -250,7 +250,6 @@ val: text_sign_in: iniciar sessió text_sign_up: registrar-te title: Com puc comentar aquest document? - locale: Valencià notifications: index: empty_notifications: No tens noves notificacions. From 9d7042c5aea81f34f48aa8660cbcafb7e4dfc450 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Thu, 4 Oct 2018 16:35:54 +0200 Subject: [PATCH 3/7] Adds missing language name translations --- config/locales/en/i18n.yml | 4 ++++ config/locales/es/i18n.yml | 4 ++++ config/locales/fr/i18n.yml | 4 ++++ config/locales/he/i18n.yml | 4 ++++ config/locales/nl/i18n.yml | 4 ++++ config/locales/pt-BR/i18n.yml | 4 ++++ config/locales/val/i18n.yml | 4 ++++ spec/features/localization_spec.rb | 3 ++- 8 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 config/locales/en/i18n.yml create mode 100644 config/locales/es/i18n.yml create mode 100644 config/locales/fr/i18n.yml create mode 100644 config/locales/he/i18n.yml create mode 100644 config/locales/nl/i18n.yml create mode 100644 config/locales/pt-BR/i18n.yml create mode 100644 config/locales/val/i18n.yml diff --git a/config/locales/en/i18n.yml b/config/locales/en/i18n.yml new file mode 100644 index 000000000..65fdd3ed5 --- /dev/null +++ b/config/locales/en/i18n.yml @@ -0,0 +1,4 @@ +en: + i18n: + language: + name: "English" \ No newline at end of file diff --git a/config/locales/es/i18n.yml b/config/locales/es/i18n.yml new file mode 100644 index 000000000..8df5d7ec1 --- /dev/null +++ b/config/locales/es/i18n.yml @@ -0,0 +1,4 @@ +es: + i18n: + language: + name: "Español" \ No newline at end of file diff --git a/config/locales/fr/i18n.yml b/config/locales/fr/i18n.yml new file mode 100644 index 000000000..911aa3352 --- /dev/null +++ b/config/locales/fr/i18n.yml @@ -0,0 +1,4 @@ +fr: + i18n: + language: + name: "Français" \ No newline at end of file diff --git a/config/locales/he/i18n.yml b/config/locales/he/i18n.yml new file mode 100644 index 000000000..003edcd71 --- /dev/null +++ b/config/locales/he/i18n.yml @@ -0,0 +1,4 @@ +he: + i18n: + language: + name: "עברית" \ No newline at end of file diff --git a/config/locales/nl/i18n.yml b/config/locales/nl/i18n.yml new file mode 100644 index 000000000..c85ec1294 --- /dev/null +++ b/config/locales/nl/i18n.yml @@ -0,0 +1,4 @@ +nl: + i18n: + language: + name: "Nederlands" \ No newline at end of file diff --git a/config/locales/pt-BR/i18n.yml b/config/locales/pt-BR/i18n.yml new file mode 100644 index 000000000..a9a624a68 --- /dev/null +++ b/config/locales/pt-BR/i18n.yml @@ -0,0 +1,4 @@ +pt-BR: + i18n: + language: + name: "Português brasileiro" \ No newline at end of file diff --git a/config/locales/val/i18n.yml b/config/locales/val/i18n.yml new file mode 100644 index 000000000..59e13344b --- /dev/null +++ b/config/locales/val/i18n.yml @@ -0,0 +1,4 @@ +val: + i18n: + language: + name: "Valencià" \ No newline at end of file diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb index 255847991..edda6f693 100644 --- a/spec/features/localization_spec.rb +++ b/spec/features/localization_spec.rb @@ -52,7 +52,7 @@ feature 'Localization' do context "Missing language names" do let!(:default_enforce) { I18n.enforce_available_locales } - let!(:default_locales) { I18n.available_locales } + let!(:default_locales) { I18n.available_locales.dup } before do I18n.enforce_available_locales = false @@ -63,6 +63,7 @@ feature 'Localization' do after do I18n.enforce_available_locales = default_enforce I18n.available_locales = default_locales + I18n.locale = I18n.default_locale end scenario 'Available locales without language translation display locale key' do From 168ebe3c8bab83ec5f9d9db6ea8de165fafacae4 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Thu, 4 Oct 2018 18:43:49 +0200 Subject: [PATCH 4/7] Fix i18n task' specs Add i18n.yml to i18n task's available translation files --- config/i18n-tasks.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index b673c2110..d7ae6c72b 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -43,6 +43,7 @@ data: - config/locales/%{locale}/images.yml - config/locales/%{locale}/guides.yml - config/locales/%{locale}/user_groups.yml + - config/locales/%{locale}/i18n.yml # Locale files to write new keys to, based on a list of key pattern => file rules. Matched from top to bottom: # `i18n-tasks normalize -p` will force move the keys according to these rules From ad511091499d3c873d3fae3d02ec59d2307754a0 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Fri, 5 Oct 2018 18:07:41 +0200 Subject: [PATCH 5/7] Remove duplicate language translation --- config/locales/pt-BR/general.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/locales/pt-BR/general.yml b/config/locales/pt-BR/general.yml index fe0f22b42..ef797dac5 100644 --- a/config/locales/pt-BR/general.yml +++ b/config/locales/pt-BR/general.yml @@ -1,8 +1,5 @@ --- pt-BR: - i18n: - language: - name: Português account: show: change_credentials_link: Alterar meus dados pessoais From 6f978b9f4ed253d28de59114e43c18516c26fd77 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Tue, 9 Oct 2018 18:32:59 +0200 Subject: [PATCH 6/7] Fix portuguese specs --- .../site_customization/custom_pages_spec.rb | 20 +++++++++---------- .../poll_question_answers_spec.rb | 4 ++-- spec/shared/features/translatable.rb | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/features/site_customization/custom_pages_spec.rb b/spec/features/site_customization/custom_pages_spec.rb index 74148b577..d9bf01acb 100644 --- a/spec/features/site_customization/custom_pages_spec.rb +++ b/spec/features/site_customization/custom_pages_spec.rb @@ -107,7 +107,7 @@ feature "Custom Pages" do scenario "Listed in more information page" do custom_page = create(:site_customization_page, :published, - slug: "another-slug", + slug: "another-slug", title_en: "Another custom page", subtitle_en: "Subtitle for custom page", more_info_flag: true @@ -136,10 +136,10 @@ feature "Custom Pages" do expect(page).to have_content("Subtitle for custom page") end end - end - + end + context "Translation" do - + let(:custom_page) { create(:site_customization_page, :published, slug: "example-page", title_en: "Title in English", @@ -148,7 +148,7 @@ feature "Custom Pages" do subtitle_es: "Subtitulo en Español", content_en: "Content in English", content_es: "Contenido en Español" - ) } + ) } background do admin = create(:administrator) @@ -162,19 +162,19 @@ feature "Custom Pages" do scenario "Add a translation in Português", :js do visit @edit_page_url - select "Português", from: "translation_locale" + select "Português brasileiro", from: "translation_locale" fill_in 'site_customization_page_title_pt_br', with: 'Titulo em Português' click_button 'Update Custom page' expect(page).to have_content "Page updated successfully" - + visit @edit_page_url expect(page).to have_field('site_customization_page_title_en', with: 'Title in English') click_link "Español" expect(page).to have_field('site_customization_page_title_es', with: 'Titulo en Español') - click_link "Português" + click_link "Português brasileiro" expect(page).to have_field('site_customization_page_title_pt_br', with: 'Titulo em Português') end @@ -190,11 +190,11 @@ feature "Custom Pages" do visit custom_page.url select('English', from: 'locale-switcher') - + expect(page).to have_content("Title in English") select('Español', from: 'locale-switcher') - + expect(page).to have_content("Titulo correcta en Español") end diff --git a/spec/features/translations/poll_question_answers_spec.rb b/spec/features/translations/poll_question_answers_spec.rb index cd33106c1..fded75cfd 100644 --- a/spec/features/translations/poll_question_answers_spec.rb +++ b/spec/features/translations/poll_question_answers_spec.rb @@ -91,11 +91,11 @@ feature "Translations" do scenario "Add a translation for a locale with non-underscored name", :js do visit @edit_answer_url - select('Português', from: 'translation_locale') + select('Português brasileiro', from: 'translation_locale') fill_in_ckeditor 'poll_question_answer_description_pt_br', with: 'resposta em Português' click_button 'Save' - select('Português', from: 'locale-switcher') + select('Português brasileiro', from: 'locale-switcher') expect(page).to have_content("resposta em Português") end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 843df2c09..e3abf5f84 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -107,14 +107,14 @@ shared_examples "translatable" do |factory_name, path_name, fields| visit path field = fields.sample - select "Português", from: "translation_locale" + select "Português brasileiro", from: "translation_locale" fill_in field_for(field, :pt_br), with: text_for(field, :"pt-BR") click_button update_button_text visit path - select('Português', from: 'locale-switcher') + select('Português brasileiro', from: 'locale-switcher') expect(page).to have_field(field_for(field, :pt_br), with: text_for(field, :"pt-BR")) end From 59b35156b1f488b0e81e4112dfa4613c897b51de Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Tue, 9 Oct 2018 22:15:05 +0200 Subject: [PATCH 7/7] Fix flaky spec `I18n.available_locales=` does a little magic[1] and was causing some flakieness along the way, as the `:wl` locale persisted in future specs Thanks for the heads up @javierm :ok_hand: [1] https://www.rubydoc.info/github/svenfuchs/i18n/I18n%2FConfig:available_locales= --- spec/features/localization_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb index edda6f693..fc9cfb1e6 100644 --- a/spec/features/localization_spec.rb +++ b/spec/features/localization_spec.rb @@ -56,7 +56,7 @@ feature 'Localization' do before do I18n.enforce_available_locales = false - I18n.available_locales << :wl + I18n.available_locales = default_locales + [:wl] I18n.locale = :wl end