Jer*_*Ges 6 elixir internationalization phoenix-framework
我有一个Elixir/Phoenix应用程序,根据域名(也称为租户)做出不同反应.
租户具有特定的区域设置,例如"fr_FR","en_US"等.
我想根据当前的语言环境翻译路由器的URI :
# EN
get "/classifieds/new", ClassifiedController, :new
# FR
get "/annonces/ajout", ClassifiedController, :new
Run Code Online (Sandbox Code Playgroud)
到目前为止,我认为可以做类似的事情(伪代码):
if locale() == :fr do
scope "/", Awesome.App, as: :app do
pipe_through :browser # Use the default browser stack
get "/annonces/ajout", ClassifiedController, :new
end
else
scope "/", Awesome.App, as: :app do
pipe_through :browser # Use the default browser stack
get "/classifieds/new", ClassifiedController, :new
end
end
Run Code Online (Sandbox Code Playgroud)
由于路由器是在服务器启动期间编译的,因此它不起作用,因此您没有当前连接的上下文(区域设置,域,主机等).
到目前为止,我的解决方案(可行)是创建两个带有两个别名的作用域:
scope "/", Awesome.App, as: :fr_app do
pipe_through :browser # Use the default browser stack
get "/annonces/ajout", ClassifiedController, :new
end
scope "/", Awesome.App, as: :app do
pipe_through :browser # Use the default browser stack
get "/classifieds/new", ClassifiedController, :new
end
Run Code Online (Sandbox Code Playgroud)
和一个帮手相关:
localized_path(conn, path, action)
Run Code Online (Sandbox Code Playgroud)
如果当前语言环境是" fr "(例如),则采用路径(:app_classified_new_path)和前缀fr(:fr_app_classified_new_path).如果当前语言环境的路径不存在,我将回退到默认语言环境"en").
它工作得很好,但我看到了一些痛点:
每次使用"something_foo_path()"助手必须由新的"localized_path()"替换
该应用程序将接受每个本地化的细分(fr,en,it,等等),这不是我想要的(我希望只有fr段才能在租户"fr"上工作)
我应该能够直接在路由器(feature/bugfix?)中获取当前的locale/conn信息,并避免像这样的黑客攻击.
看看这篇文章Practical i18n with Phoenix and Elixir。我相信它应该能够满足您的需求。
或者,您可以使用一些宏来创建翻译路线块。下面的代码不能满足您的所有要求,因为您仍然需要在内部转换路径。不过,它可能会给你一些想法。
defmodule LocalRoutes.Web.Router do
use LocalRoutes.Web, :router
@locales ~w(en fr)
import LocalRoutes.Web.Gettext
use LocalRoutes.LocalizedRouter
def locale(conn, locale) do
Plug.Conn.assign conn, :locale, locale
end
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", LocalRoutes.Web do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
for locale <- @locales do
Gettext.put_locale(LocalRoutes.Web.Gettext, locale)
pipeline String.to_atom(locale) do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :locale, locale
end
scope "/", LocalRoutes.Web do
pipe_through String.to_atom(locale)
get "/#{~g"classifieds"}", ClassifiedController, :index
get "/#{~g"classifieds"}/#{~g"new"}", ClassifiedController, :new
get "/#{~g"classifieds"}/:id", ClassifiedController, :show
get "/#{~g"classifieds"}/:id/#{~g"edit"}", ClassifiedController, :edit
post "/#{~g"classifieds"}", ClassifiedController, :create
patch "/#{~g"classifieds"}/:id", ClassifiedController, :update
put "/#{~g"classifieds"}/:id", ClassifiedController, :update
delete "/#{~g"classifieds"}/:id", ClassifiedController, :delete
end
end
end
defmodule LocalRoutes.LocalizedRouter do
defmacro __using__(opts) do
quote do
import unquote(__MODULE__)
end
end
defmacro sigil_g(string, _) do
quote do
dgettext "routes", unquote(string)
end
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
446 次 |
| 最近记录: |