LoginSignup
1
1

More than 5 years have passed since last update.

RailsテンプレートAPIを使ったi18n設定の自動化について

Posted at

はじめに

みなさん、こんばんは。ちけんです。
この記事では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を使うには

  1. template.rbにテンプレートの設定を書く
  2. rails new hoge -m ./template.rbを実行する
  3. 設定済みのrailsプロジェクトが作成される
  4. ╭( ・ㅂ・)و ̑̑ 完了 !

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1