Skip to content

Commit

Permalink
i18n accepts mismatched number of arguments. (fix #667)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmsnr committed Jan 5, 2017
1 parent b633e21 commit bccff5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/review/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def t(str, args = nil)
remove_args.reverse_each do |idx|
args.delete_at idx
end
args_matched = (frmt.count("%") == args.size)
args_matched = (frmt.count("%") <= args.size)
frmt.gsub!('##', '%%')
args_matched ? (frmt % args) : frmt
rescue
Expand Down
17 changes: 17 additions & 0 deletions test/test_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ def test_custom_format_numbers
end
end

def test_format_with_mismatched_number_of_arguments
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
file = File.join(dir, "locale.yml")

File.open(file, "w"){|f| f.write %Q|locale: ja\nformat_number_header: "%2$d"| }
I18n.setup("ja")
assert_equal "10", I18n.t("format_number_header", [1, 10])

File.open(file, "w"){|f| f.write %Q|locale: ja\nformat_number_header: "%2$d-%1$d"| }
I18n.setup("ja")
# ERROR: returns raw format
assert_equal "%2$d-%1$d", I18n.t("format_number_header", [1])
end
end
end

def test_ja
I18n.setup("ja")
assert_equal "図", I18n.t("image")
Expand Down

1 comment on commit bccff5e

@kmuto
Copy link
Owner

@kmuto kmuto commented on bccff5e Jan 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

おぉ、なるほど

Please sign in to comment.