Skip to content

Commit

Permalink
Locale data from dependencies: data.external
Browse files Browse the repository at this point in the history
Adds a new configuration setting, `data.external`, for locale data from dependencies.

Locale data from dependencies is never considered unused and we never
modify it.

Fixes #264
  • Loading branch information
glebm committed Sep 24, 2017
1 parent bc84a3b commit c9b09b0
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## v0.9.19 (not yet released)

Adds a new configuration setting, `data.external`, for locale data from external dependencies (e.g. gems).
This locale data is never considered unused, and is never modified by i18n-tasks.
[#264](https://github.com/glebm/i18n-tasks/issues/264)

Fixes support for calls such as `t @instance_variable, scope: :static_scope` in the non-AST scanner.
[#1d2c6d0c](https://github.com/glebm/i18n-tasks/commit/1d2c6d0cb7ee20a8db68c52e33ec3c2a382633e6)

Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/tasks/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def key_value?(key, locale = base_locale)
!t(key, locale).nil?
end

def external_key?(key, locale = base_locale)
data.external(locale)[locale.to_s][key]
end

# Normalize all the locale data in the store (by writing to the store).
#
# @param [Array<String>] locales locales to normalize. Default: all.
Expand Down
25 changes: 16 additions & 9 deletions lib/i18n/tasks/data/file_system_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ def initialize(config = {})
end
end

# get locale tree
# @param [String, Symbol] locale
# @return [::I18n::Tasks::Data::Siblings]
def get(locale)
locale = locale.to_s
@trees ||= {}
@trees[locale] ||= Tree::Siblings[locale => {}].merge!(
read_locale(locale)
)
@trees ||= {}
@trees[locale] ||= read_locale(locale)
end

# @param [String, Symbol] locale
# @return [::I18n::Tasks::Data::Siblings]
def external(locale)
locale = locale.to_s
@external ||= {}
@external[locale] ||= read_locale(locale, paths: config[:external])
end

alias [] get
Expand Down Expand Up @@ -151,17 +158,17 @@ def router

protected

def read_locale(locale)
Array(config[:read]).map do |path|
def read_locale(locale, paths: config[:read])
Array(paths).flat_map do |path|
Dir.glob format(path, locale: locale)
end.reduce(:+).map do |path|
end.map do |path|
[path.freeze, load_file(path) || {}]
end.map do |path, data|
filter_nil_keys! path, data
Data::Tree::Siblings.from_nested_hash(data).tap do |s|
s.leaves { |x| x.data.update(path: path, locale: locale) }
end
end.reduce(:merge!) || Tree::Siblings.null
end.reduce(Tree::Siblings[locale => {}], :merge!)
end

def filter_nil_keys!(path, data, suffix = [])
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/missing_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def equal_values_tree(locale, compare_to = base_locale)
end

def locale_key_missing?(locale, key)
!key_value?(key, locale) && !ignore_key?(key, :missing)
!key_value?(key, locale) && !external_key?(key) && !ignore_key?(key, :missing)
end

# @param [::I18n::Tasks::Data::Tree::Siblings] forest
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/app/views/usages.html.slim
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
p = t 'used.a'
b = t 'used.a'

p = t 'external.used'
2 changes: 2 additions & 0 deletions spec/fixtures/config/i18n-tasks.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ data:
- ['devise.*', 'config/locales/devise.%{locale}.yml']
# default catch-all (same as ['*', 'config/locales/%{locale}.yml'])
- 'config/locales/%{locale}.yml'
external:
- 'config/locales/external/%{locale}.yml'
yaml:
write:
line_width: -1
Expand Down
4 changes: 4 additions & 0 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@
fs = fixtures_contents.merge(
'config/locales/en.yml' => { 'en' => en_data }.to_yaml,
'config/locales/es.yml' => { 'es' => es_data }.to_yaml,
'config/locales/external/en.yml' =>
{ 'en' => { 'external' => { 'used' => 'EN_TEXT', 'unused' => 'EN_TEXT' } } }.to_yaml,
'config/locales/external/es.yml' =>
{ 'es' => { 'external' => { 'used' => 'ES_TEXT', 'unused' => 'ES_TEXT' } } }.to_yaml,
'config/locales/old_devise.en.yml' => { 'en' => { 'devise' => { 'a' => 'EN_TEXT' } } }.to_yaml,
'config/locales/old_devise.es.yml' => { 'es' => { 'devise' => { 'a' => 'ES_TEXT' } } }.to_yaml,
'config/locales/unused.en.yml' => { 'en' => { 'unused' => { 'file' => 'EN_TEXT' } } }.to_yaml,
Expand Down
8 changes: 6 additions & 2 deletions templates/config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ data:
# - config/locales/%{locale}.yml
## More files:
# - config/locales/**/*.%{locale}.yml
## Another gem (replace %#= with %=):
# - "<%#= %x[bundle show vagrant].chomp %>/templates/locales/%{locale}.yml"

# Locale files to write new keys to, based on a list of key pattern => file rules. Matched from top to bottom:
# `i18n-tasks normalize -p` will force move the keys according to these rules
Expand All @@ -30,6 +28,12 @@ data:
## Catch-all default:
# - config/locales/%{locale}.yml

# External locale data (e.g. gems).
# This data is not considered unused and is never written to.
external:
## Example (replace %#= with %=):
# - "<%#= %x[bundle show vagrant].chomp %>/templates/locales/%{locale}.yml"

## Specify the router (see Readme for details). Valid values: conservative_router, pattern_router, or a custom class.
# router: conservative_router

Expand Down

0 comments on commit c9b09b0

Please sign in to comment.