From 2c79bbfbf2cc4c934251532dd68d541063a9e644 Mon Sep 17 00:00:00 2001 From: Olafur Arason Date: Sun, 16 Mar 2025 12:47:37 +0100 Subject: [PATCH 1/2] feat: allow for restricting the locales affected It's often useful to limit where the pseudolocalization gets applied. This allows for checking the difference between applied and unapplied locales. It also allows for using a strategy where only locales such as XX or ZZ are used. --- lib/pseudolocalization.rb | 11 +++++++++-- test/pseudolocalization_test.rb | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/pseudolocalization.rb b/lib/pseudolocalization.rb index bc8f8af..4d29901 100644 --- a/lib/pseudolocalization.rb +++ b/lib/pseudolocalization.rb @@ -5,11 +5,12 @@ module Pseudolocalization module I18n class Backend attr_reader :original_backend - attr_accessor :ignores + attr_accessor :ignores, :only_locales def initialize(original_backend) @original_backend = original_backend @ignores = [] + @only_locales = [] yield self if block_given? end @@ -26,7 +27,7 @@ def respond_to_missing?(name, include_private = false) end def translate(locale, key, options) - return original_backend.translate(locale, key, options) if key_ignored?(key) + return original_backend.translate(locale, key, options) if key_ignored?(key) || locale_ignored?(locale) ::Pseudolocalization::I18n::Pseudolocalizer.pseudolocalize(original_backend.translate(locale, key, options)) end @@ -40,6 +41,12 @@ def translations private + def locale_ignored?(locale) + return false if only_locales.empty? + + !only_locales.include?(locale) + end + def key_ignored?(key) return false unless ignores diff --git a/test/pseudolocalization_test.rb b/test/pseudolocalization_test.rb index 19045c9..1df52a4 100644 --- a/test/pseudolocalization_test.rb +++ b/test/pseudolocalization_test.rb @@ -74,6 +74,13 @@ def test_it_allows_ignoring_cetain_keys assert_equal(['Ḥḛḛḽḽṓṓ, ẁṓṓṛḽḍ!'], @backend.translate(:en, ['Hello, world!'], {})) end + def test_it_allows_only_cetain_locales + @backend.only_locales = [:en] + + assert_equal('Ignore me, World!', @backend.translate(:fr, 'Ignore me, World!', {})) + assert_equal(['Ḥḛḛḽḽṓṓ, ẁṓṓṛḽḍ!'], @backend.translate(:en, ['Hello, world!'], {})) + end + def test_it_exposes_pseudo_localized_translations translations = { en: { From 58f1de6b6be2dfe74cdff766966ed2d240cff938 Mon Sep 17 00:00:00 2001 From: Olafur Arason Date: Sun, 16 Mar 2025 12:59:53 +0100 Subject: [PATCH 2/2] chore: add documentation --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 504f12e..a2afe03 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,14 @@ I18n.backend = Pseudolocalization::I18n::Backend.new(I18n.backend) I18n.backend.ignores = ['ignored*', /Waldo.$/] ``` +### Apply only to specific locales +You may wish to have the backend only apply to specific locales. These may be configured via an array, `only_locales`, on the backend. + +```ruby +I18n.backend = Pseudolocalization::I18n::Backend.new(I18n.backend) +I18n.backend.only_locales = [:en, :xx, :zz] +``` + ### How to run tests ```bash