Skip to content

Commit

Permalink
Follow-up to #304
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Oct 23, 2018
1 parent 0c5f8bc commit 7a00e6d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lib/i18n/tasks/base_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require 'i18n/tasks/used_keys'
require 'i18n/tasks/ignore_keys'
require 'i18n/tasks/missing_keys'
require 'i18n/tasks/inconsistent_interpolation'
require 'i18n/tasks/interpolations'
require 'i18n/tasks/unused_keys'
require 'i18n/tasks/translation'
require 'i18n/tasks/locale_pathname'
Expand All @@ -31,7 +31,7 @@ class BaseTask
include UsedKeys
include IgnoreKeys
include MissingKeys
include InconsistentInterpolation
include Interpolations
include UnusedKeys
include Translation
include Logging
Expand Down
34 changes: 0 additions & 34 deletions lib/i18n/tasks/inconsistent_interpolation.rb

This file was deleted.

29 changes: 29 additions & 0 deletions lib/i18n/tasks/interpolations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module I18n::Tasks
module Interpolations
VARIABLE_REGEX = /%{[^}]+}/

def inconsistent_interpolation(locales: nil, base_locale: nil) # rubocop:disable Metrics/AbcSize
locales ||= self.locales
base_locale ||= self.base_locale
result = empty_forest

data[base_locale].key_values.each do |key, value|
next if !value.is_a?(String) || ignore_key?(key, :inconsistent)
base_vars = Set.new(value.scan(VARIABLE_REGEX))
(locales - [base_locale]).each do |current_locale|
node = data[current_locale].first.children[key]
next unless node&.value&.is_a?(String)
vars = node.value.scan(VARIABLE_REGEX)
unless vars.size == base_vars.size && vars.all? { |v| base_vars.include?(v) }
result.merge!(node.walk_to_root.reduce(nil) { |c, p| [p.derive(children: c)] })
end
end
end

result.each { |root| root.data[:type] = :inconsistent_interpolation }
result
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'spec_helper'

RSpec.describe 'InconsistentInterpolation' do
RSpec.describe 'Interpolations' do
let!(:task) { I18n::Tasks::BaseTask.new }

let(:base_keys) { { 'a' => 'hello %{world}', 'b' => 'foo', 'c' => { 'd' => 'hello %{name}' }, 'e' => 'ok' } }
Expand Down

0 comments on commit 7a00e6d

Please sign in to comment.