-
Notifications
You must be signed in to change notification settings - Fork 13
feat: allow for restricting the locales affected #91
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of defaulting to an empty array, and having custom logic to bypass the feature if the array is empty, would it be possible to instantiate this array to |
||
| 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 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| @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: { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it wouldn't be clearer if we were to reuse the naming convention of
available_locales. If the pseudolocalization backend'savailable_localesdoesn't include locale X, then it passes it through?