Merge pull request #929 from Retrospring/locale-gen

re-enable locale switching, add rake task to generate testing locales
This commit is contained in:
Georg Gadinger 2023-01-06 12:47:49 +00:00 committed by GitHub
commit b6121b16f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 165 additions and 7 deletions

3
.gitignore vendored
View File

@ -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

View File

@ -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

View File

@ -27,9 +27,9 @@ en:
header: "Share your answers"
body_html: |
<p>Want your followers on another platform to see your %{app_name} answers?
You can configure automatic sharing to your favorite platforms easily.</p>
You can configure automatic sharing to your favourite platforms easily.</p>
<p class="text-muted">Not sure if it's a favorite, but at the moment only
<p class="text-muted">Not sure if it's a favourite, but at the moment only
<b>Twitter</b> is supported.</p>
customize:
header: "Customise your experience"

66
lib/tasks/locale.rake Normal file
View File

@ -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 <html tags>
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

View File

@ -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