GithubHelp home page GithubHelp logo

boudra / alternate Goto Github PK

View Code? Open in Web Editor NEW
27.0 4.0 5.0 60 KB

Plug and Phoenix helpers to localize your web app via the URL

License: Apache License 2.0

Elixir 100.00%
phoenix gettext localization i18n elixir plug

alternate's People

Contributors

boudra avatar franciscoj avatar gilacost avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

alternate's Issues

Doesn't work

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?

Default language

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

Is the URL support opt-in ?

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

Translated 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.