Skip to content
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

isolating router: only match files with configured locales #605

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions lib/i18n/tasks/data/router/isolating_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ module Data::Router
class IsolatingRouter
include ::I18n::Tasks::KeyPatternMatching

attr_reader :config_read_patterns, :base_locale
attr_reader :config_read_patterns, :base_locale, :locales

def initialize(_adapter, data_config)
@base_locale = data_config[:base_locale]
@config_read_patterns = Array.wrap(data_config[:read])
@locales = Array.wrap(data_config[:locales] || @base_locale)
end

# Route keys to destinations
Expand Down Expand Up @@ -51,8 +52,9 @@ def route(locale, forest, &block)
def alternate_path_for(source_path, locale)
source_path = source_path.dup

locale_pattern = "{#{locales.join(',')}}"
config_read_patterns.each do |pattern|
regexp = Glob.new(format(pattern, locale: '(*)')).to_regexp
regexp = Glob.new(format(pattern, locale: locale_pattern)).to_regexp
next unless source_path.match?(regexp)

source_path.match(regexp) do |match_data|
Expand Down
4 changes: 3 additions & 1 deletion spec/isolating_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
I18n::Tasks::Data::FileSystem.new(
router: 'isolating_router',
base_locale: 'en',
locales: %w[en fr],
read: ['app/components/*.%{locale}.yml']
)
end
Expand Down Expand Up @@ -56,7 +57,8 @@

describe 'alternate_path_for(source_path, locale)' do
let(:read_config_patterns) { ['config/locales/**/*.%{locale}.yml'] }
let(:router) { I18n::Tasks::Data::Router::IsolatingRouter.new(nil, { read: read_config_patterns }) }
let(:base_locale) { :en }
let(:router) { I18n::Tasks::Data::Router::IsolatingRouter.new(nil, { base_locale: base_locale, read: read_config_patterns }) }

Check failure on line 61 in spec/isolating_router_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/LineLength: Line is too long. [130/120]

context 'when `source_path` matches a pattern of the `read` configuration' do
it 'changes only the `%{locale}` part of `source_path`' do
Expand Down
Loading