diff --git a/Makefile b/Makefile new file mode 100644 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + + +locale: + mix gettext.extract + mix gettext.merge priv/gettext diff --git a/lib/kolab_chat/locale.ex b/lib/kolab_chat/locale.ex new file mode 100644 --- /dev/null +++ b/lib/kolab_chat/locale.ex @@ -0,0 +1,38 @@ +defmodule KolabChat.Locale do + import Plug.Conn + + def init(opts), do: opts + + def call(conn, _opts) do + case get_session(conn, :locale) || client_locale(conn) do + nil -> + conn + locale -> + Gettext.put_locale(KolabChat.Gettext, locale) + put_session(conn, :locale, locale) + end + end + + # Gets supported locale code from the client + # Uses 'locale' parameter or Accept-Language header + defp client_locale(conn) do + supported = Gettext.known_locales(KolabChat.Gettext) + locale = conn.params["locale"] || get_req_header(conn, "accept-language") + + locale + |> to_string() + |> String.split(",") + |> Enum.map(&parse_locale/1) + |> Enum.filter(fn(x) -> Enum.member?(supported, x) end) + |> Enum.at(0) + end + + # Extracts locale code from an element of Accept-Language header + defp parse_locale(locale) do + locale + |> String.split(";") + |> Enum.at(0) + |> String.trim() + |> String.replace("-", "_") + end +end diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot new file mode 100644 --- /dev/null +++ b/priv/gettext/default.pot @@ -0,0 +1,28 @@ +## 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 "" +"Language: INSERT LANGUAGE HERE\n" + +#: web/templates/layout/app.html.eex:10 +msgid "Kolab Real Time Communication" +msgstr "" + +#: web/templates/layout/app.html.eex:19 +msgid "Login" +msgstr "" + +#: web/templates/page/index.html.eex:3 +msgid "Real-time communication for the Kolab groupware system." +msgstr "" + +#: web/templates/page/index.html.eex:2 +msgid "Welcome to %{name}" +msgstr "" diff --git a/priv/gettext/errors.pot b/priv/gettext/errors.pot --- a/priv/gettext/errors.pot +++ b/priv/gettext/errors.pot @@ -7,7 +7,6 @@ ## 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. - ## From Ecto.Changeset.cast/4 msgid "can't be blank" msgstr "" diff --git a/web/router.ex b/web/router.ex --- a/web/router.ex +++ b/web/router.ex @@ -7,6 +7,7 @@ plug :fetch_flash plug :protect_from_forgery plug :put_secure_browser_headers + plug KolabChat.Locale end pipeline :api do diff --git a/web/templates/layout/app.html.eex b/web/templates/layout/app.html.eex --- a/web/templates/layout/app.html.eex +++ b/web/templates/layout/app.html.eex @@ -7,7 +7,7 @@ - Kolab Real Time Communication + <%= gettext "Kolab Real Time Communication" %> "> @@ -16,7 +16,7 @@
diff --git a/web/templates/page/index.html.eex b/web/templates/page/index.html.eex --- a/web/templates/page/index.html.eex +++ b/web/templates/page/index.html.eex @@ -1,5 +1,4 @@

<%= gettext "Welcome to %{name}", name: "Kolab Chat!" %>

-

Real-time communication for the Kolab groupware system.

+

<%= gettext "Real-time communication for the Kolab groupware system." %>

-