i18nliner 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +8 -6
  3. data/lib/i18nliner/base.rb +26 -27
  4. data/lib/i18nliner/call_helpers.rb +42 -34
  5. data/lib/i18nliner/commands/basic_formatter.rb +2 -0
  6. data/lib/i18nliner/commands/check.rb +14 -14
  7. data/lib/i18nliner/commands/color_formatter.rb +2 -0
  8. data/lib/i18nliner/commands/dump.rb +6 -6
  9. data/lib/i18nliner/commands/generic_command.rb +4 -2
  10. data/lib/i18nliner/controller_scope.rb +2 -0
  11. data/lib/i18nliner/errors.rb +5 -4
  12. data/lib/i18nliner/erubi.rb +5 -4
  13. data/lib/i18nliner/erubis.rb +2 -0
  14. data/lib/i18nliner/extensions/controller.rb +8 -8
  15. data/lib/i18nliner/extensions/core.rb +16 -18
  16. data/lib/i18nliner/extensions/inferpolation.rb +11 -7
  17. data/lib/i18nliner/extensions/model.rb +11 -10
  18. data/lib/i18nliner/extensions/view.rb +13 -9
  19. data/lib/i18nliner/extractors/ruby_extractor.rb +18 -16
  20. data/lib/i18nliner/extractors/sexp_helper.rb +8 -5
  21. data/lib/i18nliner/extractors/translate_call.rb +31 -26
  22. data/lib/i18nliner/extractors/translation_hash.rb +10 -10
  23. data/lib/i18nliner/pre_processors/erb_pre_processor.rb +54 -44
  24. data/lib/i18nliner/processors/abstract_processor.rb +9 -4
  25. data/lib/i18nliner/processors/erb_processor.rb +10 -8
  26. data/lib/i18nliner/processors/ruby_processor.rb +13 -10
  27. data/lib/i18nliner/processors.rb +2 -0
  28. data/lib/i18nliner/railtie.rb +10 -10
  29. data/lib/i18nliner/reserved_keys.rb +2 -0
  30. data/lib/i18nliner/scope.rb +7 -9
  31. data/lib/i18nliner.rb +8 -6
  32. data/lib/tasks/i18nliner.rake +8 -7
  33. data/spec/fixtures/app/models/invalid.rb +2 -0
  34. data/spec/fixtures/app/models/valid.rb +2 -0
  35. data/spec/{commands → i18nliner/commands}/check_spec.rb +5 -6
  36. data/spec/{commands → i18nliner/commands}/dump_spec.rb +9 -10
  37. data/spec/{extensions → i18nliner/extensions}/controller_spec.rb +9 -8
  38. data/spec/{extensions → i18nliner/extensions}/core_spec.rb +45 -34
  39. data/spec/i18nliner/extensions/inferpolation_spec.rb +51 -0
  40. data/spec/i18nliner/extensions/model_spec.rb +31 -0
  41. data/spec/{extensions → i18nliner/extensions}/view_spec.rb +15 -13
  42. data/spec/{extractors → i18nliner/extractors}/ruby_extractor_spec.rb +24 -15
  43. data/spec/{extractors → i18nliner/extractors}/translate_call_spec.rb +73 -65
  44. data/spec/{extractors → i18nliner/extractors}/translation_hash_spec.rb +13 -12
  45. data/spec/{reserved_keys_spec.rb → i18nliner/i18n_spec.rb} +3 -1
  46. data/spec/{pre_processors → i18nliner/pre_processors}/erb_pre_processor_spec.rb +80 -77
  47. data/spec/i18nliner/processors/erb_processor_spec.rb +45 -0
  48. data/spec/i18nliner/processors/ruby_processor_spec.rb +27 -0
  49. metadata +56 -83
  50. data/spec/extensions/inferpolation_spec.rb +0 -49
  51. data/spec/extensions/model_spec.rb +0 -30
  52. data/spec/processors/erb_processor_spec.rb +0 -45
  53. data/spec/processors/ruby_processor_spec.rb +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c65c8bd881edfdc231120af22f283b85770bfa2befdf3495d273848101393062
4
- data.tar.gz: ce466b775c3e449efc57ba5d6083f59c8c5a0a63243a0301be8515dedca6c9f0
3
+ metadata.gz: 15cfd420213769dbeb319a5527cf831c0d8a26a9293862cf65f1ae41d1971006
4
+ data.tar.gz: 4dcbbefb5e7659feccbbf268fa7ee9e9cdf920bf8a2a61b37a87074ff5c86b5f
5
5
  SHA512:
6
- metadata.gz: b4e7c18f79f693a815ea3f6b6b84f46be8b56d8e739cdf7c064e0d2632cef23138cfb1e75c344077c44ace0f9c63d4b79cce32f19c8df9a79a28f7a07a71e92d
7
- data.tar.gz: 8455844299eeca26b8d017e17ccfca90172d597f942b5331069e2b07e409167e425c16ceb8d59511066b5035c96bdee587f1c2fa0abe6793987fc4d40a4cc3be
6
+ metadata.gz: 63d0eb0cdb5fe84bb5d702c38bc52c2cf615c566e095c28a1a56a54d54ca77fc5b7f8276c2493a0ba68d644639fdd96939ec7dc4cc69f15e489c99a27cdba7ae
7
+ data.tar.gz: 851f0e3161a6242e063c36b88e558c52f8bb9f74c79433248f6af4fea0f513893c1129d3c96898b39656fd4e7e4c7573bb96ab5ea166c87833f12a9723d6625b
data/Rakefile CHANGED
@@ -1,10 +1,12 @@
1
- require 'rake'
2
- require 'bundler/gem_tasks'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'rspec/core'
5
- require 'rspec/core/rake_task'
3
+ require "rake"
4
+ require "bundler/gem_tasks"
5
+
6
+ require "rspec/core"
7
+ require "rspec/core/rake_task"
6
8
  RSpec::Core::RakeTask.new(:spec) do |spec|
7
- spec.pattern = FileList['spec/**/*_spec.rb']
9
+ spec.pattern = FileList["spec/**/*_spec.rb"]
8
10
  end
9
11
 
10
- task :default => :spec
12
+ task default: :spec
@@ -1,17 +1,16 @@
1
- require 'i18n'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18n"
2
4
 
3
5
  module I18nliner
4
6
  def self.ignore
5
7
  @ignore ||= begin
6
8
  path = File.join(base_path, ".i18nignore")
7
- File.exist?(path) ?
8
- File.read(path).split(/\r?\n|\r/) :
9
- []
9
+ File.exist?(path) ? File.read(path).split(/\r?\n|\r/) : []
10
10
  end
11
11
  end
12
12
 
13
- def self.translations
14
- end
13
+ def self.translations; end
15
14
 
16
15
  def self.manual_translations
17
16
  I18n.t(:foo) # ensure backend is initialized
@@ -19,31 +18,31 @@ module I18nliner
19
18
  I18n.backend.send(:translations)[I18n.default_locale]
20
19
  end
21
20
 
22
- def self.look_up(key)
21
+ def self.look_up(key) # rubocop:disable Naming/PredicateMethod
23
22
  I18n.exists?(I18n.locale, key)
24
23
  end
25
24
 
26
25
  def self.setting(key, value)
27
- instance_eval <<-CODE
28
- def #{key}=(value)
29
- @#{key} = value
30
- end
31
-
32
- def #{key}(value = nil)
33
- if value && block_given?
34
- begin
35
- value_was = @#{key}
36
- @#{key} = value
37
- yield
38
- ensure
39
- @#{key} = value_was
40
- end
41
- else
42
- @#{key} = #{value.inspect} if @#{key}.nil?
43
- @#{key}
44
- end
45
- end
46
- CODE
26
+ instance_eval <<~RUBY, __FILE__, __LINE__ + 1
27
+ def #{key}=(value) # def base_path=(value)
28
+ @#{key} = value # @base_path = value
29
+ end # end
30
+ #
31
+ def #{key}(value = nil) # def base_path(value = nil)
32
+ if value && block_given? # if value && block_given?
33
+ begin # begin
34
+ value_was = @#{key} # value_was = @base_path
35
+ @#{key} = value # @base_path = value
36
+ yield # yield
37
+ ensure # ensure
38
+ @#{key} = value_was # @base_path = value_was
39
+ end # end
40
+ else # else
41
+ @#{key} = #{value.inspect} if @#{key}.nil? # @base_path = "./" if @base_path.nil?
42
+ @#{key} # @base_path
43
+ end # end
44
+ end # end
45
+ RUBY
47
46
  end
48
47
 
49
48
  setting :inferred_key_format, :underscored_crc32
@@ -1,12 +1,16 @@
1
- require 'zlib'
2
- require 'i18n'
3
- require 'active_support/core_ext/string/inflections'
4
- require 'i18nliner/base'
1
+ # frozen_string_literal: true
2
+
3
+ require "zlib"
4
+ require "i18n"
5
+ require "active_support/core_ext/string/inflections"
6
+ require "i18nliner/base"
5
7
 
6
8
  module I18nliner
7
9
  module CallHelpers
8
- ALLOWED_PLURALIZATION_KEYS = [:zero, :one, :few, :many, :other]
9
- REQUIRED_PLURALIZATION_KEYS = [:one, :other]
10
+ ALLOWED_PLURALIZATION_KEYS = %i[zero one few many other].freeze
11
+ REQUIRED_PLURALIZATION_KEYS = %i[one other].freeze
12
+
13
+ module_function
10
14
 
11
15
  def normalize_key(key, scope, inferred, i18n_scope)
12
16
  scope.normalize_key(key, inferred, i18n_scope)
@@ -14,8 +18,7 @@ module I18nliner
14
18
 
15
19
  def normalize_default(default, translate_options = {}, options = {})
16
20
  default = infer_pluralization_hash(default, translate_options)
17
- default = normalize_whitespace(default, options)
18
- default
21
+ normalize_whitespace(default, options)
19
22
  end
20
23
 
21
24
  def normalize_whitespace(default, options)
@@ -27,14 +30,14 @@ module I18nliner
27
30
  default.each { |key, value| default[key] = normalize_whitespace(value, options) }
28
31
  return default
29
32
  elsif default.is_a?(Array)
30
- return default.map{|value| value.is_a?(String) ? normalize_whitespace(value, options) : value}
33
+ return default.map { |value| value.is_a?(String) ? normalize_whitespace(value, options) : value }
31
34
  end
32
35
 
33
36
  if options[:remove_whitespace]
34
- default.gsub!(/\s+/, ' ')
37
+ default.gsub!(/\s+/, " ")
35
38
  default.strip!
36
39
  else
37
- default.sub!(/\s*\n\z/, '')
40
+ default.sub!(/\s*\n\z/, "")
38
41
  default.lstrip!
39
42
  end
40
43
  default
@@ -43,28 +46,30 @@ module I18nliner
43
46
  def infer_pluralization_hash(default, translate_options)
44
47
  str = (default.is_a?(Array) && default.size == 1) ? default.first : default
45
48
  return default unless str.is_a?(String) &&
46
- str =~ /\A[\w\-]+\z/ &&
49
+ str =~ /\A[\w-]+\z/ &&
47
50
  translate_options.include?(:count)
48
- {:one => "1 #{str}", :other => "%{count} #{str.pluralize}"}
51
+
52
+ { one: "1 #{str}", other: "%{count} #{str.pluralize}" }
49
53
  end
50
54
 
51
55
  def infer_key(default, translate_options = {})
52
56
  return unless default && (default.is_a?(String) || default.is_a?(Hash))
53
- default = normalize_default(default, translate_options, :remove_whitespace => true)
57
+
58
+ default = normalize_default(default, translate_options, remove_whitespace: true)
54
59
  default = default[:other].to_s if default.is_a?(Hash)
55
60
  keyify(default)
56
61
  end
57
62
 
58
63
  def keyify_underscored(string)
59
- key = I18n.transliterate(string, :locale => I18n.default_locale).to_s
64
+ key = I18n.transliterate(string, locale: I18n.default_locale).to_s
60
65
  key.downcase!
61
- key.gsub!(/[^a-z0-9_]+/, '_')
62
- key.gsub!(/\A_|_\z/, '')
66
+ key.gsub!(/[^a-z0-9_]+/, "_")
67
+ key.gsub!(/\A_|_\z/, "")
63
68
  key[0...I18nliner.underscored_key_length]
64
69
  end
65
70
 
66
71
  def keyify_underscored_crc32(string)
67
- checksum = Zlib.crc32("#{string.size.to_s}:#{string}").to_s(16)
72
+ checksum = Zlib.crc32("#{string.size}:#{string}").to_s(16)
68
73
  "#{keyify_underscored(string)}_#{checksum}"
69
74
  end
70
75
 
@@ -83,27 +88,30 @@ module I18nliner
83
88
  # key, default_hash, options
84
89
  # default_string [, options]
85
90
  # default_hash, options
86
- def key_provided?(key_or_default = nil, default_or_options = nil, maybe_options = nil, *others)
91
+ def key_provided?(key_or_default = nil, default_or_options = nil, maybe_options = nil, *)
87
92
  return false if key_or_default.is_a?(Hash)
88
93
  return true if key_or_default.is_a?(Symbol) || key_or_default.nil? || key_or_default.is_a?(Array)
89
- raise ArgumentError.new("invalid key_or_default argument. expected String, Symbol, Array, Hash or nil, got #{key_or_default.class}") unless key_or_default.is_a?(String)
94
+
95
+ unless key_or_default.is_a?(String)
96
+ raise ArgumentError, "invalid key_or_default argument. " \
97
+ "expected String, Symbol, Array, Hash or nil, got #{key_or_default.class}"
98
+ end
90
99
  return true if default_or_options.is_a?(String)
91
100
  return true if maybe_options
92
- return true if key_or_default =~ /\A\.?(\w+\.)+\w+\z/
101
+ return true if /\A\.?(\w+\.)+\w+\z/.match?(key_or_default)
102
+
93
103
  false
94
104
  end
95
105
 
96
106
  def pluralization_hash?(hash)
97
107
  hash.is_a?(Hash) &&
98
- hash.size > 0 &&
99
- (hash.keys - ALLOWED_PLURALIZATION_KEYS).size == 0
108
+ !hash.empty? &&
109
+ (hash.keys - ALLOWED_PLURALIZATION_KEYS).empty?
100
110
  end
101
111
 
102
112
  def infer_arguments(args)
103
- raise ArgumentError.new("wrong number of arguments (#{args.size} for 1..3)") if args.empty? || args.size > 3
104
- if args.size == 2 && args[1].is_a?(Hash) && args[1][:default]
105
- return args
106
- end
113
+ raise ArgumentError, "wrong number of arguments (#{args.size} for 1..3)" if args.empty? || args.size > 3
114
+ return args if args.size == 2 && args[1].is_a?(Hash) && args[1][:default]
107
115
 
108
116
  has_key = key_provided?(*args)
109
117
  args.unshift nil unless has_key
@@ -115,16 +123,16 @@ module I18nliner
115
123
  end
116
124
  args << {} if args.size == 1
117
125
  options = args[1]
118
- raise ArgumentError.new("invalid default translation. expected Hash or String, got #{default.class}") unless default.nil? || default.is_a?(String) || default.is_a?(Hash)
119
- raise ArgumentError.new("invalid options argument. expected Hash, got #{options.class}") unless options.is_a?(Hash)
126
+ unless default.nil? || default.is_a?(String) || default.is_a?(Hash)
127
+ raise ArgumentError,
128
+ "invalid default translation. expected Hash or String, got #{default.class}"
129
+ end
130
+ raise ArgumentError, "invalid options argument. expected Hash, got #{options.class}" unless options.is_a?(Hash)
131
+
120
132
  options[:default] = default if default
121
133
  options[:i18nliner_inferred_key] = true unless has_key
122
- unless has_key
123
- args[0] = infer_key(default, options)
124
- end
134
+ args[0] = infer_key(default, options) unless has_key
125
135
  args
126
136
  end
127
-
128
- extend self
129
137
  end
130
138
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18nliner
2
4
  module Commands
3
5
  module BasicFormatter
@@ -1,6 +1,8 @@
1
- require 'i18nliner/commands/generic_command'
2
- require 'i18nliner/extractors/translation_hash'
3
- require 'active_support/core_ext/enumerable'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18nliner/commands/generic_command"
4
+ require "i18nliner/extractors/translation_hash"
5
+ require "active_support/core_ext/enumerable"
4
6
  Dir[File.join(File.dirname(__FILE__), "../processors/*.rb")].each do |file|
5
7
  require "i18nliner/processors/#{File.basename(file)}"
6
8
  end
@@ -19,22 +21,20 @@ module I18nliner
19
21
  def processors
20
22
  @processors ||= I18nliner::Processors.all.map do |klass|
21
23
  klass.new @translations,
22
- :only => @options[:only],
23
- :translations => @translations,
24
- :checker => method(:check_file)
24
+ only: @options[:only],
25
+ translations: @translations,
26
+ checker: method(:check_file)
25
27
  end
26
28
  end
27
29
 
28
30
  def check_files
29
- processors.each &:check_files
31
+ processors.each(&:check_files)
30
32
  end
31
33
 
32
34
  def check_file(file)
33
- if yield file
34
- print green(".") unless @options[:silent]
35
- end
36
- rescue SyntaxError, StandardError, ExtractionError
37
- @errors << "#{$!}\n#{file}"
35
+ print green(".") if yield(file) && !@options[:silent]
36
+ rescue SyntaxError, StandardError, ExtractionError => e
37
+ @errors << "#{e}\n#{file}"
38
38
  print red("F") unless @options[:silent]
39
39
  end
40
40
 
@@ -49,7 +49,7 @@ module I18nliner
49
49
  print "\n\n"
50
50
 
51
51
  @errors.each_with_index do |error, i|
52
- puts "#{i+1})"
52
+ puts "#{i + 1})"
53
53
  puts red(error)
54
54
  print "\n"
55
55
  end
@@ -59,7 +59,7 @@ module I18nliner
59
59
  puts success? ? green(summary) : red(summary)
60
60
  end
61
61
 
62
- def run
62
+ def run # rubocop:disable Naming/PredicateMethod
63
63
  check_files
64
64
  print_summary unless @options[:silent]
65
65
  success?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18nliner
2
4
  module Commands
3
5
  module ColorFormatter
@@ -1,5 +1,7 @@
1
- require 'ya2yaml'
2
- require 'fileutils'
1
+ # frozen_string_literal: true
2
+
3
+ require "ya2yaml"
4
+ require "fileutils"
3
5
 
4
6
  module I18nliner
5
7
  module Commands
@@ -10,14 +12,12 @@ module I18nliner
10
12
  super
11
13
  @translations = @options[:translations]
12
14
  @yml_file = @options[:file] ||
13
- File.join(I18nliner.base_path, "config", "locales", "generated", "#{I18n.default_locale}.yml")
15
+ File.join(I18nliner.base_path, "config", "locales", "generated", "#{I18n.default_locale}.yml")
14
16
  end
15
17
 
16
18
  def run
17
19
  FileUtils.mkdir_p File.dirname(yml_file)
18
- File.open(yml_file, "w") do |file|
19
- file.write({I18n.default_locale.to_s => @translations}.ya2yaml(:syck_compatible => true))
20
- end
20
+ File.write(yml_file, { I18n.default_locale.to_s => @translations }.ya2yaml(syck_compatible: true))
21
21
  puts "Wrote default translations to #{yml_file}" unless @options[:silent]
22
22
  end
23
23
  end
@@ -1,5 +1,7 @@
1
- require 'i18nliner/commands/basic_formatter'
2
- require 'i18nliner/commands/color_formatter'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18nliner/commands/basic_formatter"
4
+ require "i18nliner/commands/color_formatter"
3
5
 
4
6
  module I18nliner
5
7
  module Commands
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "i18nliner/scope"
2
4
 
3
5
  module I18nliner
@@ -1,17 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18nliner
2
4
  class ExtractionError < StandardError
3
5
  def initialize(line, detail = nil)
6
+ super()
4
7
  @line = line
5
8
  @detail = detail
6
9
  end
7
10
 
8
11
  def to_s
9
12
  error = self.class.name.underscore.humanize
10
- error.gsub!(/\AI18nliner\/| error\z/, '')
13
+ error.gsub!(%r{\AI18nliner/| error\z}, "")
11
14
  error = "#{error} on line #{@line}"
12
- @detail ?
13
- error + " (got #{@detail.inspect})" :
14
- error
15
+ @detail ? error + " (got #{@detail.inspect})" : error
15
16
  end
16
17
  end
17
18
 
@@ -1,12 +1,13 @@
1
- require 'action_view/template'
2
- require 'i18nliner/pre_processors/erb_pre_processor'
1
+ # frozen_string_literal: true
2
+
3
+ require "action_view/template"
4
+ require "i18nliner/pre_processors/erb_pre_processor"
3
5
 
4
6
  module I18nliner
5
7
  class Erubi < ::ActionView::Template::Handlers::ERB::Erubi
6
8
  def initialize(source, options = {})
7
9
  source = I18nliner::PreProcessors::ErbPreProcessor.new(source).result
8
- super(source, options)
10
+ super
9
11
  end
10
12
  end
11
13
  end
12
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "i18nliner/erubi"
2
4
 
3
5
  module I18nliner
@@ -1,6 +1,8 @@
1
- require 'i18nliner/base'
2
- require 'i18nliner/call_helpers'
3
- require 'i18nliner/extensions/inferpolation'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18nliner/base"
4
+ require "i18nliner/call_helpers"
5
+ require "i18nliner/extensions/inferpolation"
4
6
 
5
7
  module I18nliner
6
8
  module Extensions
@@ -15,11 +17,9 @@ module I18nliner
15
17
  options[:i18nliner_scope] = i18nliner_scope
16
18
  super(key, **options)
17
19
  end
18
- alias :t :translate
19
- alias :t! :translate
20
- alias :translate! :translate
20
+ alias_method :t, :translate
21
+ alias_method :t!, :translate
22
+ alias_method :translate!, :translate
21
23
  end
22
24
  end
23
25
  end
24
-
25
-
@@ -1,6 +1,8 @@
1
- require 'i18nliner/scope'
2
- require 'i18nliner/call_helpers'
3
- require 'active_support/core_ext/string/output_safety'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18nliner/scope"
4
+ require "i18nliner/call_helpers"
5
+ require "active_support/core_ext/string/output_safety"
4
6
 
5
7
  module I18nliner
6
8
  module Extensions
@@ -12,7 +14,7 @@ module I18nliner
12
14
  inferred_key = options.delete(:i18nliner_inferred_key)
13
15
  key = CallHelpers.normalize_key(key, scope, inferred_key, options[:scope])
14
16
 
15
- if default = options[:default]
17
+ if (default = options[:default])
16
18
  options[:default] = CallHelpers.normalize_default(default, options)
17
19
  end
18
20
 
@@ -26,24 +28,20 @@ module I18nliner
26
28
  # If you are actually using nonprintable characters in your source string, you should feel ashamed
27
29
  result = result.gsub("\\\\", "\uE124").gsub("\\*", "\uE123")
28
30
  result = result.html_safe if was_html_safe
29
- if wrappers
30
- result = apply_wrappers(result, wrappers)
31
- end
31
+ result = apply_wrappers(result, wrappers) if wrappers
32
32
  was_html_safe = result.html_safe?
33
- result = result.gsub("\uE123", '*').gsub("\uE124", "\\")
33
+ result = result.tr("\uE123\uE124", "*\\")
34
34
  result = result.html_safe if was_html_safe
35
35
 
36
36
  result
37
- rescue ArgumentError
38
- raise
39
37
  end
40
- alias :t :translate
41
- alias :t! :translate
42
- alias :translate! :translate
38
+ alias_method :t, :translate
39
+ alias_method :t!, :translate
40
+ alias_method :translate!, :translate
43
41
 
44
42
  # can't super this one yet :-/
45
43
  def interpolate_hash_with_html_safety(string, values)
46
- if string.html_safe? || values.values.any?{ |v| v.is_a?(ActiveSupport::SafeBuffer) }
44
+ if string.html_safe? || values.values.any?(ActiveSupport::SafeBuffer)
47
45
  string = ERB::Util.h(string) unless string.html_safe?
48
46
  values.each do |key, value|
49
47
  values[key] = ERB::Util.h(value) unless value.html_safe?
@@ -61,22 +59,22 @@ module I18nliner
61
59
  end
62
60
  end
63
61
 
64
- private
62
+ private
65
63
 
66
64
  def apply_wrappers(string, wrappers)
67
65
  string = string.html_safe? ? string.dup : ERB::Util.h(string)
68
66
  unless wrappers.is_a?(Hash)
69
67
  wrappers = Array(wrappers)
70
- wrappers = Hash[wrappers.each_with_index.map{ |w, i| ['*' * (1 + i), w]}]
68
+ wrappers = wrappers.each_with_index.to_h { |w, i| ["*" * (1 + i), w] }
71
69
  end
72
70
  # If you are actually using nonprintable characters in your source string, you should feel ashamed
73
71
  string.gsub!("\\\\", 26.chr)
74
72
  string.gsub!("\\*", 27.chr)
75
- wrappers.sort_by{ |k, v| -k.length }.each do |k, v|
73
+ wrappers.sort_by { |k, _v| -k.length }.each do |k, v|
76
74
  pattern = pattern_for(k)
77
75
  string.gsub!(pattern, v)
78
76
  end
79
- string.gsub!(27.chr, '*')
77
+ string.gsub!(27.chr, "*")
80
78
  string.gsub!(26.chr, "\\")
81
79
  string.html_safe
82
80
  end
@@ -1,32 +1,36 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module I18nliner
2
4
  module Extensions
3
5
  module Inferpolation
4
6
  def inferpolate(options)
5
7
  default = options[:default]
6
8
  return options unless default
9
+
7
10
  if default.is_a?(Hash)
8
- default.each { |key, value| inferpolate_value!(value, options) }
11
+ default.each_value { |value| inferpolate_value(value, options) }
9
12
  else
10
- inferpolate_value!(default, options)
13
+ options[:default] = inferpolate_value(default, options)
11
14
  end
12
15
 
13
16
  options
14
17
  end
15
18
 
16
- def inferpolate_value!(value, options)
17
- value.gsub!(/%\{((@)?\w+(.\w+)*)\}/).each do
18
- match = $~
19
+ def inferpolate_value(value, options)
20
+ value.gsub(/%\{((@)?\w+(.\w+)*)\}/) do |match|
19
21
  key = $1
20
22
  ivar = $2
21
23
  next match if options[key] || options[key.to_sym]
22
- parts = key.split('.')
24
+
25
+ parts = key.split(".")
23
26
  receiver = ivar ? instance_variable_get(parts.shift) : self
24
27
  value = parts.inject(receiver) do |obj, message|
25
28
  obj.respond_to?(message) ? obj.send(message) : nil
26
29
  end
27
30
 
28
31
  next match if value.nil?
29
- new_key = key.sub(/\@/, '').gsub('.', '_')
32
+
33
+ new_key = key.delete("@").tr(".", "_")
30
34
  options[new_key.to_sym] = value
31
35
  "%{#{new_key}}"
32
36
  end
@@ -1,6 +1,8 @@
1
- require 'i18nliner/base'
2
- require 'i18nliner/call_helpers'
3
- require 'i18nliner/extensions/inferpolation'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18nliner/base"
4
+ require "i18nliner/call_helpers"
5
+ require "i18nliner/extensions/inferpolation"
4
6
 
5
7
  module I18nliner
6
8
  module Extensions
@@ -15,14 +17,14 @@ module I18nliner
15
17
  options[:i18nliner_scope] = i18nliner_scope
16
18
  I18n.translate(key, **options)
17
19
  end
18
- alias :t :translate
19
- alias :t! :translate
20
- alias :translate! :translate
20
+ alias_method :t, :translate
21
+ alias_method :t!, :translate
22
+ alias_method :translate!, :translate
21
23
 
22
- def localize(*args)
23
- I18n.localize(*args)
24
+ def localize(*)
25
+ I18n.localize(*)
24
26
  end
25
- alias :l :localize
27
+ alias_method :l, :localize
26
28
 
27
29
  def self.included(klass)
28
30
  klass.extend(self)
@@ -30,4 +32,3 @@ module I18nliner
30
32
  end
31
33
  end
32
34
  end
33
-
@@ -1,6 +1,8 @@
1
- require 'i18nliner/base'
2
- require 'i18nliner/call_helpers'
3
- require 'i18nliner/extensions/inferpolation'
1
+ # frozen_string_literal: true
2
+
3
+ require "i18nliner/base"
4
+ require "i18nliner/call_helpers"
5
+ require "i18nliner/extensions/inferpolation"
4
6
 
5
7
  module I18nliner
6
8
  module Extensions
@@ -13,17 +15,19 @@ module I18nliner
13
15
  # if a block gets through to here, it either means:
14
16
  # 1. the user did something weird (e.g. <%= t{ "haha" } %>)
15
17
  # 2. the erb pre processor missed it somehow (bug)
16
- raise InvalidBlockUsageError.new("block translate calls need to be output (i.e. `<%=`) and the block body must be of the form `%>your string<%`") if block_given?
18
+ if block_given?
19
+ raise InvalidBlockUsageError, "block translate calls need to be output (i.e. `<%=`) " \
20
+ "and the block body must be of the form `%>your string<%`"
21
+ end
22
+
17
23
  key, options = CallHelpers.infer_arguments(args)
18
24
  options = inferpolate(options) if I18nliner.infer_interpolation_values
19
25
  options[:i18nliner_scope] = i18nliner_scope
20
26
  super(key, **options)
21
- rescue ArgumentError
22
- raise
23
27
  end
24
- alias :t :translate
25
- alias :t! :translate
26
- alias :translate! :translate
28
+ alias_method :t, :translate
29
+ alias_method :t!, :translate
30
+ alias_method :translate!, :translate
27
31
  end
28
32
  end
29
33
  end