Skip to content

Commit

Permalink
conservative router: fall back to pattern r. for new keys
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed May 31, 2014
1 parent 920714f commit df84455
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Available commands:
remove-unused remove unused keys
config display i18n-tasks configuration
xlsx-report save missing and unused translations to an Excel file
irb irb session within i18n-tasks context

See `<command> --help` for more information on a specific command.
```
Expand Down Expand Up @@ -151,19 +152,32 @@ data:
#### Multiple locale files
Use `data.read` and `data.write` options to work with locale data spread over multiple files.
Use `data` options to work with locale data spread over multiple files.

`data.read` accepts a list of file globs to read from per-locale:

```
# config/i18n-tasks.yml
data:
# a list of file globs to read from per-locale
read:
# read from namespaced files, e.g. simple_form.en.yml
- 'config/locales/*.%{locale}.yml'
# read from a gem (config is parsed with ERB first, then YAML)
- "<%= %x[bundle show vagrant].chomp %>/templates/locales/%{locale}.yml"
# default
- 'config/locales/%{locale}.yml'
```

For writing to locale files i18n-tasks provides 2 options.

##### Pattern router

Pattern router organizes keys based on a list of key patterns, as in the example below:

```
data:
# pattern_router is default
router: pattern_router
# a list of {key pattern => file} routes, matched top to bottom
write:
# write models.* and views.* keys to the respective files
Expand All @@ -174,6 +188,18 @@ data:
- 'config/locales/%{locale}.yml'
```

##### Conservative router (v0.4.0+)

Conservative router keeps the keys where they are found, or infers the path from base locale.
If the key is completely new, conservative router will fall back to the pattern router behaviour.

```
data:
router: conservative_router
write:
- 'config/locales/%{locale}.yml'
```
#### Key pattern syntax
| syntax | description |
Expand Down
13 changes: 8 additions & 5 deletions lib/i18n/tasks/data/router/conservative_router.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require 'i18n/tasks/key_pattern_matching'
require 'i18n/tasks/data/tree/node'
require 'i18n/tasks/data/router/pattern_router'

module I18n::Tasks
module Data::Router
# Keep the path, or infer from base locale
class ConservativeRouter
class ConservativeRouter < PatternRouter
def initialize(adapter, config)
@adapter = adapter
@base_locale = config[:base_locale]
@routes_config = config[:write]
super
end

def route(locale, forest, &block)
return to_enum(:route, locale, forest) unless block
out = {}
not_found = Set.new
forest.keys(root: false) do |key, node|
locale_key = "#{locale}.#{key}"
path = adapter[locale][locale_key].data[:path]
Expand All @@ -27,12 +27,15 @@ def route(locale, forest, &block)
if path
(out[path] ||= Set.new) << locale_key
else
raise "could not find path for #{locale_key}"
not_found << locale_key
end
end
out.each do |dest, keys|
block.yield dest, forest.select_keys { |key, _| keys.include?(key) }
end
if not_found.present?
super(locale, forest.select_keys { |key, _| not_found.include?(key) }, &block)
end
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module I18n
module Tasks
VERSION = '0.4.0.rc1'
VERSION = '0.4.0.beta1'
end
end
11 changes: 10 additions & 1 deletion spec/conservative_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
I18n::Tasks::Data::FileSystem.new(
router: 'conservative_router',
base_locale: 'en',
read: 'config/locales/*%{locale}.yml'
read: 'config/locales/*%{locale}.yml',
write: ['config/locales/not_found.%{locale}.yml']
)
}

Expand All @@ -37,5 +38,13 @@
data['es']['es.b'].data[:path].should == 'config/locales/other.es.yml'
end
end

it 'falls back to pattern_router when the key is new' do
TestCodebase.in_test_app_dir do
data['es'] = data['es'].merge!(build_tree(es: {z: 2}))
data.reload
data['es']['es.z'].data[:path].should == 'config/locales/not_found.es.yml'
end
end
end
end

0 comments on commit df84455

Please sign in to comment.