Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数字のフォーマットを追加 #520

Merged
merged 1 commit into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/review/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class I18n
ALPHA_L = %w[0 a b c d e f g h i j k l m n o p q r s t u v w x y z]
ROMAN_U = %w[0 I II III IV V VI VII VIII IX X XI XII XIII XIV XV XVI XVII XVIII XIX XX XXI XXII XXIII XXIV XXV XXVI XXVII]
ROMAN_L = %w[0 i ii iii iv v vi vii viii ix x xi xii xiii xiv xv xvi xvii xviii xix xx xxi xxii xxiii xxiv xxv xxvi xxvii]
ALPHA_UW = %w[0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z]
ALPHA_LW = %w[0 a b c d e f g h i j k l m n o p q r s t u v w x y z]
ROMAN_UW = %w[0 Ⅰ Ⅱ Ⅲ Ⅳ V Ⅵ Ⅶ Ⅷ Ⅸ X Ⅺ Ⅻ]
ARABIC_UW = %w[〇 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]
ARABIC_LW = %w[〇 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]
JAPAN = %w[〇 一 二 三 四 五 六 七 八 九 十 十一 十二 十三 十四 十五 十六 十七 十八 十九 二十 二十一 二十二 二十三 二十四 二十五 二十六 二十七]

def self.setup(locale="ja", ymlfile = "locale.yml")
@i18n = ReVIEW::I18n.new(locale)
Expand Down Expand Up @@ -106,7 +112,7 @@ def t(str, args = nil)

frmt = @store[@locale][str].dup
frmt.gsub!('%%', '##')
percents = frmt.scan(/%\w\w?/)
percents = frmt.scan(/%\w{1,3}/)
percents.each_with_index do |i, idx|
case i
when "%pA"
Expand All @@ -115,12 +121,30 @@ def t(str, args = nil)
when "%pa"
frmt.sub!(i, ALPHA_L[args[idx]])
args.delete idx
when "%pAW"
frmt.sub!(i, ALPHA_UW[args[idx]])
args.delete idx
when "%paW"
frmt.sub!(i, ALPHA_LW[args[idx]])
args.delete idx
when "%pR"
frmt.sub!(i, ROMAN_U[args[idx]])
args.delete idx
when "%pr"
frmt.sub!(i, ROMAN_L[args[idx]])
args.delete idx
when "%pRW"
frmt.sub!(i, ROMAN_UW[args[idx]])
args.delete idx
when "%pJ"
frmt.sub!(i, JAPAN[args[idx]])
args.delete idx
when "%pdW"
frmt.sub!(i, ARABIC_LW[args[idx]])
args.delete idx
when "%pDW"
frmt.sub!(i, ARABIC_UW[args[idx]])
args.delete idx
end
end
frmt.gsub!('##', '%%')
Expand Down
40 changes: 40 additions & 0 deletions test/test_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,53 @@ def test_custom_format
I18n.setup("ja")
assert_equal "第B章", I18n.t("chapter", 2)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pAW章")}
I18n.setup("ja")
assert_equal "第B章", I18n.t("chapter", 2)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%paW章")}
I18n.setup("ja")
assert_equal "第b章", I18n.t("chapter", 2)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pR章")}
I18n.setup("ja")
assert_equal "第I章", I18n.t("chapter", 1)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pr章")}
I18n.setup("ja")
assert_equal "第ii章", I18n.t("chapter", 2)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pRW章")}
I18n.setup("ja")
assert_equal "第Ⅻ章", I18n.t("chapter", 12)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pJ章")}
I18n.setup("ja")
assert_equal "第二十七章", I18n.t("chapter", 27)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pdW章")}
I18n.setup("ja")
assert_equal "第1章", I18n.t("chapter", 1)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pdW章")}
I18n.setup("ja")
assert_equal "第27章", I18n.t("chapter", 27)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pDW章")}
I18n.setup("ja")
assert_equal "第1章", I18n.t("chapter", 1)

File.open(file, "w"){|f| f.write("locale: ja\nchapter: 第%pDW章")}
I18n.setup("ja")
assert_equal "第27章", I18n.t("chapter", 27)

File.open(file, "w"){|f| f.write("locale: ja\npart: Part %pRW")}
I18n.setup("ja")
assert_equal "Part 0", I18n.t("part", 0)

File.open(file, "w"){|f| f.write("locale: ja\npart: 第%pJ部")}
I18n.setup("ja")
assert_equal "第一部", I18n.t("part", 1)
end
end
end
Expand Down