UserEngineのlibフォルダの中も日本語化

UserEngineでまだ一カ所、日本語化できていないところがあった。それは、ログインページの一番最初のメッセージだ。

このメッセージはlib/user_engine/authorized_system.rbファイルで定義されている。libフォルダのコードについてはGetText未処理だったので、早速_()で囲ってみた。(ついでにlibフォルダの他のメッセージも_()で囲った。)

flash[:message] = _("You need to log in.")

後は、ターミナルからお決まりの処理を実行する。

  • rake updatepo
  • user_engine.poを翻訳
  • rake makemo

ところが、これだけでは日本語化されなかった...。

プラグインのlibフォルダでの設定

libフォルダの中のファイルを日本語するためには、モジュール定義の先頭で以下のように追記することが必要だった。

require 'gettext'
module UserEngine
  include GetText
  GetText.bindtextdomain("user_engine", "vendor/plugins/user_engine/locale")
  
  # This module will be automatically included into the ApplicationController
  # when the UserEngine is included. It defines methods to be used as filters
  # for authorization.
  module AuthorizedSystem
    def self.included(base)
...(途中省略)...

4行目で、GetText.bindtextdomain("翻訳で利用する.moファイル名","左記.moファイルが存在するlocaleフォルダまでのパス")のように、どこの.moファイルを利用するかを指定している。


これで最初のメッセージも日本語で表示される!