5
2

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 3 years have passed since last update.

PhoenixでのLocale・言語設定の実装

Posted at

概要

こちらの記事が参考になり、要所をまとめました。

実装

Localeを定義する

config :appname,
  AppnameWeb.Gettext,
  locales: ~w(en ja),
  default_locale: "ja"
mix gettext.extract
mix gettext.merge priv/gettext
mix gettext.merge priv/gettext --locale ja
$ mix gettext.merge priv/gettext
Wrote priv/gettext/en/LC_MESSAGES/errors.po (0 new translations, 0 removed, 21 unchanged, 0 reworded (fuzzy))

$ mix gettext.merge priv/gettext --locale ja
Created directory priv/gettext/ja/LC_MESSAGES
Wrote priv/gettext/ja/LC_MESSAGES/errors.po (21 new translations, 0 removed, 0 unchanged, 0 reworded (fuzzy))

<h2><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h2>
priv/gettext/default.pot
## This file is a PO Template file.
##
## `msgid`s here are often extracted from source code.
## Add new translations manually only if they're dynamic
## translations that can't be statically extracted.
##
## Run `mix gettext.extract` to bring this file up to
## date. Leave `msgstr`s empty as changing them here as no
## effect: edit them in PO (`.po`) files instead.
msgid ""
msgstr ""
# , elixir-format
# : lib/lokalise_demo_web/templates/page/index.html.eex:2
msgid "Welcome to %{name}!"
msgstr ""
priv/gettext/en/LC_MESSAGES/default.po
# ... some other stuff goes here ...
# , elixir-format
# : lib/lokalise_demo_web/templates/page/index.html.eex:2
msgid "Welcome to %{name}!"
msgstr "Вас приветствует %{name}"

Localeを切り替える

パッケージをインストールします

こちらのREADMEに記載されていることを実施します。

Add set_locale to your list of dependencies in mix.exs:

def deps do
  [{:set_locale, "~> 0.2.1"}]
end

Ensure set_locale is started before your application:

def application do
  [applications: [:set_locale]]
end

router.exファイルに新しいプラグを追加します

defmodule MyApp.Router do
  use MyApp.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    ...
    # cookie_key and additional_locales are optional
    plug(SetLocale,
      gettext: MyApp.Gettext,
      default_locale: "en",
      cookie_key: "project_locale",
      additional_locales: ["ja"]
    )
  end

LocaleをCookieに保存する

def call(%Plug.Conn{params: %{"locale" => locale}} = conn, _options) when locale in @supported_locales do
  LokaliseDemoWeb.Gettext
  |> Gettext.put_locale(locale)
  
  conn
  |> put_resp_cookie "locale", locale, max_age: 365*24*60*60
end

ref:
https://github.com/smeevil/set_locale#setup

Localeを制御

下記の実装で言語設定を切り替えます。

defmodule LokaliseDemoWeb.LayoutView do
  use LokaliseDemoWeb, :view
  def new_locale(conn, locale, language_title) do
    "<a href=\"#{page_path(conn, :index, locale: locale)}\">#{language_title}</a>" |> raw
  end
end
<body>
  <div class="container">
    <header class="header">
      <%= new_locale @conn, :en, "English" %>
      <%= new_locale @conn, :ru, "Russian" %>
    </header>
    <!-- other stuff -->
  </div>
</body>

ref:
https://lokalise.com/blog/localization-of-phoenix-applications/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?