10
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[初学者]simple calendarを日本語化 rails

Posted at

#概要
表題の通り、simple calendarを日本語化します。
結論から言うと、今回 i18n という英語を日本語に変えるgemを使用します。

恐らく、私を含め初学者はsimple calendarを日本語にする時に
i18nが紐づいてでてきにくいのではないかと思い記事にしました。
(実際、私はこれを機に初めてi18nというgemを初めて知りました。)

###完成イメージ
スクリーンショット 2020-01-28 19.42.22.png
曜日が英語表記だったのが日本語表記に!
###目的

  • i18nの理解
  • simplecalendarの日本語化

#環境

  • ruby 2.5.6
  • rails 5.2.3

#前提

#i18nの導入
こちらの記事↓の3(18nの複数ロケールファイルが読み込まれるようpathを通す)まですすめてください。
[初学者]Railsのi18nによる日本語化対応

#2つのファイル作成

app/config/localesに下記のように2つのファイルを作成します。
1.ja.yml
2.model.ja.yml
スクリーンショット 2020-01-28 15.39.00.png

#ファイルの内容

app/config/locales/ja.yml
---
ja:
  date:
    abbr_day_names:
      - 
      - 
      - 
      - 
      - 
      - 
      - 
    abbr_month_names:
      -
      - 1月
      - 2月
      - 3月
      - 4月
      - 5月
      - 6月
      - 7月
      - 8月
      - 9月
      - 10月
      - 11月
      - 12月
    day_names:
      - 日曜日
      - 月曜日
      - 火曜日
      - 水曜日
      - 木曜日
      - 金曜日
      - 土曜日
    formats:
      default: "%Y/%m/%d"
      long: "%Y年%m月%d日(%a)"
      short: "%m/%d"
    month_names:
      -
      - 1月
      - 2月
      - 3月
      - 4月
      - 5月
      - 6月
      - 7月
      - 8月
      - 9月
      - 10月
      - 11月
      - 12月

  time:
    am: 午前
    formats:
      default: "%Y年%m月%d日(%a) %H時%M分%S秒 %z"
      long: "%Y/%m/%d %H:%M"
      short: "%m/%d %H:%M"
    pm: 午後

このように辞書みたいに登録された英語が日本になります。

app/config/locales/model.ja.yml
ja:
  activerecord:
    models:
      user: ユーザー # view側: User.model_name.human => "ユーザ" / t("activerecord.models.user")と同じ
      board: 掲示板
    attributes:
      user:
        id: ID
        first_name: 名前 # view側: User.human_attribute_name :name => "名前" / t("activerecord.attributes.user.name")と同じ
        last_name: 
        email: メールアドレス
        file: プロフィール画像
        crypted_password: パスワード
        created_at: 作成日
        updated_at: 更新日

これでカレンダーが日本語表記になりました!!

#最後に
railsでのシンプルカレンダーを調べると関連記事が少なく感じました。
特に導入時の記事はあるもののカスタマイズの記事が少ないです。
なので、これからカスタマイズ編も記事にしていけたらと思います。

まだまだ勉強不足なところがありますので、
アップデートできた知識は追加で記載していきます。

もし、何か修正点とかございましたらコメント等
恐縮ですが、宜しくおねがいします。

10
15
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
10
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?