はじめに
みなさん、こんばんは。ちけんです。
この記事ではi18nの設定をRailsテンプレートAPIを使ってi18nの設定を自動化することを目的とします。
具体的には、下記リポジトリのlocalesのフォルダ構成とconfig/application.rbをRailsテンプレートAPIを使って再現するといったことをしようと思います。
https://github.com/kytiken/rails_locale_ja_setting_sample
localesの構成について
下記のように構成します
config
|--locales
| |--defaults
| | |--en.yml
| | |--ja.yml
| |--models
| | |--ja.yml
| |--views
| | |--ja.yml
RailsテンプレートAPI
RailsテンプレートAPIを使うには
- template.rbにテンプレートの設定を書く
-
rails new hoge -m ./template.rb
を実行する - 設定済みのrailsプロジェクトが作成される
- ╭( ・ㅂ・)و ̑̑ 完了 !
config/application.rbの設定
# template.rb
application 'config.i18n.default_locale = :ja'
application "config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]"
参考
http://railsguides.jp/generators.html#application
locales以下のフォルダ構成を構築する
# template.rb
inside('config/locales') do
# config/locales以下のディレクトリを作る
run 'mkdir defaults'
run 'mkdir models'
run 'mkdir views'
# en.ymlをconfig/locales/defaultsに移動
run 'mv en.yml defaults'
end
# 日本語の辞書ファイルをconfig/locales/defaultsにダウンロード
inside('config/locales/defaults') do
run 'wget https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml'
end
参考
http://railsguides.jp/rails_application_templates.html#inside-dir
完成
参考にしたURL
- https://github.com/kytiken/rails_locale_ja_setting_sample
- http://railsguides.jp/rails_application_templates.html
- http://railsguides.jp/generators.html#%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88
- http://morizyun.github.io/blog/i18n-english-rails-ruby-many-languages/
- https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml