Skip to content

Commit

Permalink
Merge pull request #230 from ryu-sato/i18n
Browse files Browse the repository at this point in the history
support i18n
  • Loading branch information
kbrock authored Jul 22, 2024
2 parents cd64cf3 + 27a342c commit a90148f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,20 @@ Constants are formed by first stripping all non-word characters and then upcasin

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

## I18n

ActiveHash supports i18n as ActiveModel.
Put following code in one of your locale file (e.g. `config/locales/LANGUAGE_CODE.yml`)

```yaml
# for example, inside config/locales/ja.yml
ja:
activemodel:
models:
# `Country.model_name.human` will evaluates to "国"
country: ""
```
## Contributing
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.
Expand Down
2 changes: 1 addition & 1 deletion lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Base
class_attribute :_data, :dirty, :default_attributes, :scopes

if Object.const_defined?(:ActiveModel)
extend ActiveModel::Naming
extend ActiveModel::Translation
include ActiveModel::Conversion
else
def to_param
Expand Down
24 changes: 24 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1830,4 +1830,28 @@ class Book < ActiveRecord::Base
end
end

describe 'ActiveModel::Translation' do
around(:example) do |example|
if Object.const_defined?(:ActiveModel)
example.run
else
skip
end
end

context 'if the locale is set to :ja' do
around(:example) do |example|
current_locale = I18n.locale
I18n.locale = :ja

example.run

I18n.locale = current_locale
end

subject { Country.model_name.human }

it { is_expected.to eq('国') }
end
end
end
4 changes: 4 additions & 0 deletions spec/fixtures/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ja:
activemodel:
models:
country: ""
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
end
end
end

I18n.load_path << File.expand_path("fixtures/locales/ja.yml", __dir__)

0 comments on commit a90148f

Please sign in to comment.