Skip to content

Commit a90148f

Browse files
authored
Merge pull request #230 from ryu-sato/i18n
support i18n
2 parents cd64cf3 + 27a342c commit a90148f

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,20 @@ Constants are formed by first stripping all non-word characters and then upcasin
606606

607607
The field specified as the _enum_accessor_ must contain unique data values.
608608

609+
## I18n
610+
611+
ActiveHash supports i18n as ActiveModel.
612+
Put following code in one of your locale file (e.g. `config/locales/LANGUAGE_CODE.yml`)
613+
614+
```yaml
615+
# for example, inside config/locales/ja.yml
616+
ja:
617+
activemodel:
618+
models:
619+
# `Country.model_name.human` will evaluates to "国"
620+
country: ""
621+
```
622+
609623
## Contributing
610624
611625
If you'd like to become an ActiveHash contributor, the easiest way it to fork this repo, make your changes, run the specs and submit a pull request once they pass.

lib/active_hash/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Base
2424
class_attribute :_data, :dirty, :default_attributes, :scopes
2525

2626
if Object.const_defined?(:ActiveModel)
27-
extend ActiveModel::Naming
27+
extend ActiveModel::Translation
2828
include ActiveModel::Conversion
2929
else
3030
def to_param

spec/active_hash/base_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -1830,4 +1830,28 @@ class Book < ActiveRecord::Base
18301830
end
18311831
end
18321832

1833+
describe 'ActiveModel::Translation' do
1834+
around(:example) do |example|
1835+
if Object.const_defined?(:ActiveModel)
1836+
example.run
1837+
else
1838+
skip
1839+
end
1840+
end
1841+
1842+
context 'if the locale is set to :ja' do
1843+
around(:example) do |example|
1844+
current_locale = I18n.locale
1845+
I18n.locale = :ja
1846+
1847+
example.run
1848+
1849+
I18n.locale = current_locale
1850+
end
1851+
1852+
subject { Country.model_name.human }
1853+
1854+
it { is_expected.to eq('国') }
1855+
end
1856+
end
18331857
end

spec/fixtures/locales/ja.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ja:
2+
activemodel:
3+
models:
4+
country: ""

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323
end
2424
end
2525
end
26+
27+
I18n.load_path << File.expand_path("fixtures/locales/ja.yml", __dir__)

0 commit comments

Comments
 (0)