LoginSignup
1
2

More than 3 years have passed since last update.

【日本語化】 i18n rails 楽々日本語化 viewの表示のみ

Posted at

【ゴール】

viewだけ日本語表示へと切り替え

【メリット】

■ UIの向上
■ 日本語なので可読性向上し、開発効率化

【開発環境】

■ Mac OS catalina
■ Ruby on Rails (5.2.4.2)
■ Virtual Box:6.1
■ Vagrant: 2.2.7

【実装】

アプリケーション作成

※ touchでfileを作成

mac.terminal
$ rails new japoanese
$ rails g controller homes index
$ cd config/locale
$ touch ja.yml

※下記追記後 bundle install

gemfile.追加
gem 'rails-i18n', '~> 5.1' 

※ホーム画面へhomes/indexを!!

routes.rb
root 'homes#index'

※インデント要注意、

locale/ja.yml
ja:
 homes:
    index:
      title: '題名'
      name: ’名前’
      text: ’文章’

## modelが関連すると書き方少し変化します。下記みたいな感じです。。。

  activerecord:
    models:
      user: "ユーザー"
      attributes:
        user:
        name: "名前"
        age: "年齢"

下記重要

config/application.rb
config.i18n.default_locale = :ja #追加

※ja.ymlで定義」したものを引っ張ってきます。

homes/index.htmnl.erb

<h2><%= t '.title'%></h2>
<h2><%= t '.name'%></h2>
<h2><%= t '.text'%></h2>

以上!!

【合わせて読みたい】

■もっと詳しい記事
https://qiita.com/shimadama/items/7e5c3d75c9a9f51abdd5

■エラーメッセージ
https://qiita.com/tanaka-yu3/items/63b189d3f15653cae263

■rails メソッド 初学者者向け
https://qiita.com/tanaka-yu3/items/89abad875187494bec53

1
2
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
2