Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Contributor

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's available_locales doesn't include locale X, then it passes it through?

```

### How to run tests

```bash
Expand Down
11 changes: 9 additions & 2 deletions lib/pseudolocalization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Copy link
Contributor

Choose a reason for hiding this comment

The 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 I18n.available_locales? Doing so would allow us to transform if locale_ignored?(locale) info if pseudolocalized_locale?(locale), and simply have it check if the locale is part of the array. An added benefit would be the ability to set that array to empty, and completely bypass pseudolocalization for all locales.

yield self if block_given?
end

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

Expand Down
7 changes: 7 additions & 0 deletions test/pseudolocalization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_it_allows_only_cetain_locales
def test_it_allows_only_certain_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: {
Expand Down