Photo by Michael Kappel | Flickr - Photo Sharing! Railsでの国際化(i18n: Internationalization)の方法をまとめています。 デフォルトのlocale設定、基本的な翻訳の流れ、翻訳ファイルの作成、localeを動的に変更する方法を説明します。 動作確認 Ruby 2.2.0 Rails 4.2.0 i18n 0.7.0 目次 1. デフォルトのlocaleの設定と翻訳ファイルの読み込み 2. i18nの翻訳の基本 3. 翻訳ファイルの作成 3.1. 翻訳ファイルのディレクトリ構成 3.2. 各言語の基本的な翻訳テンプレートファイル 3.3. 遅延参照(lazy lookup) 3.4. 式展開 3.5. 翻訳ファイル内でのHTMLの使用 3.6. ActiveRecordの翻訳 4. URLによるlocaleの設定
Working on a rails 3 app where I want to check if a translation exists before outputting it, and if it doesn't exist fall back to some static text. I could do something like: if I18n.t("some_translation.key").to_s.index("translation missing") But I feel like there should be a better way than that. What if rails in the future changes the "translation missing" to "translation not found". Or what if
Padrinoは i18n ライブラリに対応しており、簡単な設定で使いはじめることができます。まずは、config/boot.rbにて以下の記述を追加します。 Padrino.before_load do I18n.enforce_available_locales = true I18n.default_locale = :ja end ちなみに I18n.enforce_available_locales を true にすると、その言語が利用可能ではない(翻訳ファイルが見つからない)時に例外を投げてくれるようになるらしいです。この設定はPadrino::Helpersモジュールによりデフォルトでtrueになっているのでわざわざ設定は不要な気もしますが、念のため明示的にtrueを指定してます(下記ソース参照)。 https://github.com/padrino/padrino-fr
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く