Skip to content

Commit ba8fc5d

Browse files
authored
Add locale option, defaulting to I18n.locale (#4)
1 parent d9515f0 commit ba8fc5d

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

lib/enumlingo/helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ def enumlingo(*attributes)
33
attributes.each do |attribute|
44
pluralized_attribute = attribute.to_s.pluralize
55

6-
define_method "#{attribute}_lingo" do
6+
define_method "#{attribute}_lingo" do |locale: I18n.locale|
77
value = send(attribute)
88
# return nil if value.nil?
9-
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{value}")
9+
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{value}", locale: locale)
1010
end
1111

12-
define_singleton_method "#{pluralized_attribute}_lingo" do
12+
define_singleton_method "#{pluralized_attribute}_lingo" do |locale: I18n.locale|
1313
send(attribute.to_s.pluralize).map do |key, value|
14-
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}"), key]
14+
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale), key]
1515
end
1616
end
1717

18-
define_singleton_method "#{attribute}_lingo" do |key|
19-
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}")
18+
define_singleton_method "#{attribute}_lingo" do |key, locale: I18n.locale|
19+
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale)
2020
end
2121

22-
define_singleton_method "#{pluralized_attribute}_lingo_values" do |locale = I18n.locale|
22+
define_singleton_method "#{pluralized_attribute}_lingo_values" do |locale: I18n.locale|
2323
send(attribute.to_s.pluralize).map do |key, value|
2424
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale), value]
2525
end

test/enumlingo_test.rb

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
11
require "test_helper"
22

33
class EnumlingoTest < ActiveSupport::TestCase
4+
setup do
5+
@book = products(:book)
6+
@other = products(:other)
7+
end
8+
49
test "it has a version number" do
510
assert Enumlingo::VERSION
611
end
712

813
test "it return English product kind of book" do
9-
I18n.locale = "en"
10-
book = products(:book)
11-
12-
assert_equal "Book", book.kind_lingo
14+
assert_equal "Book", @book.kind_lingo(locale: :en)
1315
end
1416

1517
test "it return Chinese product kind of book" do
16-
I18n.locale = "zh-CN"
17-
book = products(:book)
18-
19-
assert_equal "书籍", book.kind_lingo
18+
assert_equal "书籍", @book.kind_lingo(locale: "zh-CN")
2019
end
2120

2221
test "it return missing translation of product kind of book" do
23-
I18n.locale = "zh-CN"
24-
book = products(:other)
25-
assert_equal "Translation missing: zh-CN.activerecord.attributes.product.kinds.other", book.kind_lingo
22+
assert_equal "Translation missing: zh-CN.activerecord.attributes.product.kinds.other", @other.kind_lingo(locale: "zh-CN")
2623
end
2724

2825
test "it return options of product kind" do
29-
I18n.locale = "en"
30-
assert_equal [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]], Product.kinds_lingo
26+
assert_equal [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]], Product.kinds_lingo(locale: :en)
3127
end
3228

3329
test "it return options of product kind in Chinese" do
34-
I18n.locale = "zh-CN"
35-
assert_equal [["书籍", "book"], ["食品", "food"], ["药品", "medical"], ["Translation missing: zh-CN.activerecord.attributes.product.kinds.other", "other"]], Product.kinds_lingo
30+
assert_equal [["书籍", "book"], ["食品", "food"], ["药品", "medical"], ["Translation missing: zh-CN.activerecord.attributes.product.kinds.other", "other"]], Product.kinds_lingo(locale: "zh-CN")
3631
end
3732

3833
test "it return text of product kind" do
39-
I18n.locale = "en"
40-
assert_equal "Book", Product.kind_lingo(:book)
34+
assert_equal "Book", Product.kind_lingo(:book, locale: :en)
4135
end
4236

4337
test "it return text of product kind in Chinese" do
44-
I18n.locale = "zh-CN"
45-
assert_equal "书籍", Product.kind_lingo(:book)
38+
assert_equal "书籍", Product.kind_lingo(:book, locale: "zh-CN")
4639
end
4740

4841
test "return options of product kind value with custom translation" do
49-
assert_equal [["Book", 0], ["Food", 1], ["Medical", 2], ["Other", 3]], Product.kinds_lingo_values(:en)
42+
assert_equal [["Book", 0], ["Food", 1], ["Medical", 2], ["Other", 3]], Product.kinds_lingo_values(locale: :en)
5043
end
5144
end

0 commit comments

Comments
 (0)