From df84455b7b08eb93790ec344af4f89732623b7a6 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 31 May 2014 23:33:01 +0200 Subject: [PATCH] conservative router: fall back to pattern r. for new keys --- README.md | 30 +++++++++++++++++-- .../tasks/data/router/conservative_router.rb | 13 ++++---- lib/i18n/tasks/version.rb | 2 +- spec/conservative_router_spec.rb | 11 ++++++- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5626f1d7..8c440bd5 100644 --- a/README.md +++ b/README.md @@ -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 ` --help` for more information on a specific command. ``` @@ -151,12 +152,13 @@ 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' @@ -164,6 +166,18 @@ data: - "<%= %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 @@ -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 | diff --git a/lib/i18n/tasks/data/router/conservative_router.rb b/lib/i18n/tasks/data/router/conservative_router.rb index 06794a8f..59697e1a 100644 --- a/lib/i18n/tasks/data/router/conservative_router.rb +++ b/lib/i18n/tasks/data/router/conservative_router.rb @@ -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] @@ -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 diff --git a/lib/i18n/tasks/version.rb b/lib/i18n/tasks/version.rb index 32e4fe95..d46bfd13 100644 --- a/lib/i18n/tasks/version.rb +++ b/lib/i18n/tasks/version.rb @@ -1,5 +1,5 @@ module I18n module Tasks - VERSION = '0.4.0.rc1' + VERSION = '0.4.0.beta1' end end diff --git a/spec/conservative_router_spec.rb b/spec/conservative_router_spec.rb index 5c67288e..c41e3538 100644 --- a/spec/conservative_router_spec.rb +++ b/spec/conservative_router_spec.rb @@ -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'] ) } @@ -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