boudra / alternate Goto Github PK
View Code? Open in Web Editor NEWPlug and Phoenix helpers to localize your web app via the URL
License: Apache License 2.0
Plug and Phoenix helpers to localize your web app via the URL
License: Apache License 2.0
iex(1)> [info] GET /pl
[info] Sent 500 in 36ms
[error] #PID<0.500.0> running Front.Endpoint terminated
Server: localhost:4010 (http)
Request: GET /pl
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Front.HomeController.init/1
(front) web/controllers/home_controller.ex:2: Front.HomeController.init([action: :index, locale: "pl"])
(front) web/router.ex:1: anonymous fn/1 in Front.Router.match_route/4
(front) lib/phoenix/router.ex:261: Front.Router.dispatch/2
(front) web/router.ex:1: Front.Router.do_call/2
(front) lib/front/endpoint.ex:1: Front.Endpoint.phoenix_pipeline/1
(front) lib/plug/debugger.ex:123: Front.Endpoint."call (overridable 3)"/2
(front) lib/front/endpoint.ex:1: Front.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{:polygot, "~> 0.1.0"}]
end
config :polygot,
locales: %{
"en" => %{ path_prefix: "en" },
"pl" => %{ path_prefix: "pl" }
},
gettext_module: Front.Gettext,
gettext_domain: "default"
def router do
quote do
use Phoenix.Router
import Polygot
end
end
localize get "/", HomeController, :index
$ mix phoenix.routes
home_path GET /en Front.HomeController [action: :index, locale: "en"]
home_path GET /pl Front.HomeController [action: :index, locale: "pl"]
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug Polygot.Plug
end
Also Route helpers doesn't work, because it cannot find Polygot.Helpers
. Maybe on hex.pm there is older version of your library?
I wanted to make english to be default language:
config :polygot,
locales: %{
"en" => %{ path_prefix: "" },
"pl" => %{ path_prefix: "pl" }
},
It doesn't work. So i modified Polygot.Plug to use language with empty prefix when there is no prefix in route.
defmodule Polygot.Plug do
#
def call(conn, _opts) do
{locale, _} = Enum.find @locales, fn item ->
{locale, %{path_prefix: prefix}} = item
prefix == ""
end
Gettext.put_locale(@gettext, locale)
conn |> assign(:locale, locale)
end
end
It works well in my very small (almost static) app, but I don't know if it's a good solution
Hi,
By reading the docs here, it seems that fetching the locale and have localized urls is optional, which is good.
I want to develop an application with same URLs for everyone for the main part (it is a game), and maybe localized URLs (I mean with a /en segment) for some content, blog, etc.
Is it possible ? There is this other library, set_locale
, but it forces all urls to have a locale segment and I want to avoid that.
Thank you
What would be the best approach of extending alternate
to support also live
routes? There's no assigns
available like in the regular routes.
I wanted to create translated routes, like here:
https://github.com/BeSimple/BeSimpleI18nRoutingBundle#yaml-routing-file
I have modified Polygot module
defmacro localize({verb, meta, [ path, plug, plug_opts | rest ]} = route) when verb in @http_methods do
Enum.map(@locales, fn({locale, info}) ->
path = if Enum.count(rest) > 0 do
translated_path = Enum.at(rest, 0)[:translations][String.to_atom(locale)]
if translated_path do
"/"<>translated_path
else
path
end
else
path
end
{ verb, meta, ["/#{info.path_prefix}#{path}", plug, [ action: plug_opts, locale: locale ], rest ]}
end)
end
and router:
localize get "/how-it-works", HowItWorksController, :index, translations: [
pl: "jak-to-dziala"
]
Same as in #2 I don't know if it's good solution, so I'm not creating a pull request
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.