diff --git a/.gitignore b/.gitignore index 90e50453..b71b2d63 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,9 @@ yarn-debug.log* # site configuration /config/justask.yml +# locales generated with `bin/rails locale:generate` +/config/locales/*.en-xx.yml + # local storage /public/export /public/system diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 530d7b94..05b7ad28 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,17 +6,21 @@ class ApplicationController < ActionController::Base before_action :sentry_user_context before_action :configure_permitted_parameters, if: :devise_controller? - before_action :check_locale + around_action :switch_locale before_action :banned? before_action :find_active_announcements # check if user wants to read - def check_locale - return I18n.locale = "en" if Rails.env.test? + def switch_locale(&) + locale = params[:lang] || current_user&.locale || cookies[:lang] || "en" + if params[:lang] && current_user.present? + current_user.locale = locale + current_user.save + end - I18n.locale = "en" + cookies[:lang] = locale - cookies[:lang] = I18n.locale + I18n.with_locale(locale, &) end # check if user got hit by the banhammer of doom diff --git a/config/locales/views.en.yml b/config/locales/views.en.yml index b498e58e..a631a7c3 100644 --- a/config/locales/views.en.yml +++ b/config/locales/views.en.yml @@ -27,9 +27,9 @@ en: header: "Share your answers" body_html: |

Want your followers on another platform to see your %{app_name} answers? - You can configure automatic sharing to your favorite platforms easily.

+ You can configure automatic sharing to your favourite platforms easily.

-

Not sure if it's a favorite, but at the moment only +

Not sure if it's a favourite, but at the moment only Twitter is supported.

customize: header: "Customise your experience" diff --git a/lib/tasks/locale.rake b/lib/tasks/locale.rake new file mode 100644 index 00000000..fa58f5c0 --- /dev/null +++ b/lib/tasks/locale.rake @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +module TestLocaleTransformer + SUBSTITUTIONS = { + "a" => "åä", + "e" => "éê", + "i" => "ïí", + "n" => "ñ", + "o" => "öø", + "r" => "ř", + "u" => "üǔ", + "y" => "ÿ", + "z" => "ż" + }.freeze + + refine String do + def test_locale_destroy + SUBSTITUTIONS.inject(self) do |val, (from, to)| + val.gsub(from, to).gsub(from.upcase, to.upcase) + end + end + + def test_locale_repair + SUBSTITUTIONS.inject(self) do |val, (from, to)| + val.gsub(to, from).gsub(to.upcase, from.upcase) + end + end + end +end + +using TestLocaleTransformer + +namespace :locale do + desc "Generate en-xx locale" + task generate: :environment do + def transform_locale(hash) + hash.transform_values do |val| + next transform_locale(val) if val.is_a? Hash + next val if val.is_a? Symbol + + val = val.test_locale_destroy + + # undo damage in %{variables} + val = val.gsub(/%{([^}]+)}/, &:test_locale_repair) + + # undo damage in + val = val.gsub(/<([^>]+)>/, &:test_locale_repair) + + "[#{val}]" + end + end + + en_locales = Dir[Rails.root.join("config/locales/*.en.yml")] + + en_locales.each do |locale_path| + destination = locale_path.sub(/\.en\.yml$/, ".en-xx.yml") + puts "* generating #{File.basename(destination)}" + + locale = YAML.load_file(locale_path)["en"] + new_locale = { "en-xx" => transform_locale(locale) } + File.open(destination, "w") do |f| + f.puts new_locale.to_yaml + end + end + end +end diff --git a/spec/integration/locale_switch_spec.rb b/spec/integration/locale_switch_spec.rb new file mode 100644 index 00000000..47ea9dfa --- /dev/null +++ b/spec/integration/locale_switch_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require "rails_helper" +require "nokogiri" + +describe "locale switching", type: :request do + matcher :have_html_lang do |expected_lang| + description { %(have the HTML "lang" attribute set to #{expected_lang.inspect}) } + + match do |result| + Nokogiri::HTML.parse(result).css("html[lang=#{expected_lang}]").size == 1 + end + end + + context "when user not signed in" do + it "uses the default :en locale" do + get "/" + expect(response.body).to have_html_lang "en" + expect(response.cookies["lang"]).to eq "en" + end + + context "when ?lang param is given" do + it "changes the locale" do + # 1. ensure we start with the default :en locale + get "/" + expect(response.body).to have_html_lang "en" + expect(response.cookies["lang"]).to eq "en" + + # 2. switch the language to en-xx + get "/?lang=en-xx" + expect(response.body).to have_html_lang "en-xx" + expect(response.cookies["lang"]).to eq "en-xx" + + # 3. remove the language parameter again + get "/" + expect(response.body).to have_html_lang "en-xx" + expect(response.cookies["lang"]).to be_nil # no new cookie here, it's already en-xx + end + end + end + + context "when user is signed in" do + let(:user) { FactoryBot.create(:user, password: "test1234", locale:) } + let(:locale) { "en" } + + before do + post "/sign_in", params: { user: { login: user.email, password: user.password } } + end + + it "uses the en locale" do + get "/" + expect(response.body).to have_html_lang "en" + expect(response.cookies["lang"]).to be_nil # no new cookie here, already set by the sign in + end + + context "when ?lang param is given" do + it "changes the locale" do + # 1. ensure we start with the :en locale + get "/" + expect(response.body).to have_html_lang "en" + expect(response.cookies["lang"]).to be_nil # no new cookie here, already set by the sign in + + # 2. switch the language to en-xx + expect { get "/?lang=en-xx" }.to change { user.reload.locale }.from("en").to("en-xx") + expect(response.body).to have_html_lang "en-xx" + expect(response.cookies["lang"]).to eq "en-xx" + + # 3. remove the language parameter again + get "/" + expect(response.body).to have_html_lang "en-xx" + expect(response.cookies["lang"]).to be_nil # no new cookie here, it's already en-xx + end + end + + context "when user has a different locale set" do + let(:locale) { "fi" } + + it "uses the different locale" do + get "/" + expect(response.body).to have_html_lang "fi" + expect(response.cookies["lang"]).to be_nil # no new cookie here, already set by the sign in + end + end + end +end