Skip to content

Commit 9a09408

Browse files
committed
init
0 parents  commit 9a09408

9 files changed

+174
-0
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
locale_plug-*.tar
24+

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# LocalePlug
2+
3+
A plug to set locale for web applications.
4+
5+
## Installation
6+
7+
```elixir
8+
def deps do
9+
[
10+
{:locale_plug, "~> 0.1.0"}
11+
]
12+
end
13+
```

config/config.exs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# third-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure your application as:
12+
#
13+
# config :locale_plug, key: :value
14+
#
15+
# and access this configuration in your application as:
16+
#
17+
# Application.get_env(:locale_plug, :key)
18+
#
19+
# You can also configure a third-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env()}.exs"

lib/locale_plug.ex

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule LocalePlug do
2+
@moduledoc """
3+
Fetch locale from params or cookies and set it for the specified Gettext backend.
4+
5+
## Example
6+
7+
plug LocalePlug, backend: MyApp.Gettext
8+
"""
9+
10+
import Plug.Conn
11+
12+
@max_age 365 * 24 * 60 * 60
13+
14+
def init(opts) do
15+
backend = Keyword.fetch!(opts, :backend)
16+
[backend: backend]
17+
end
18+
19+
def call(conn, [backend: backend]) do
20+
case fetch_locale(conn, backend) do
21+
nil ->
22+
conn
23+
locale ->
24+
Gettext.put_locale(backend, locale)
25+
put_resp_cookie(conn, "locale", locale, max_age: @max_age)
26+
end
27+
end
28+
29+
defp fetch_locale(conn, backend) do
30+
(conn.params["locale"] || conn.cookies["locale"])
31+
|> check_locale(backend)
32+
end
33+
34+
defp check_locale(locale, backend) do
35+
supported_locales = Gettext.known_locales(backend)
36+
if locale in supported_locales do
37+
locale
38+
else
39+
nil
40+
end
41+
end
42+
end

mix.exs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule LocalePlug.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :locale_plug,
7+
version: "0.1.0",
8+
elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps(),
11+
12+
# Publish package
13+
name: "LocalePlug",
14+
description: description(),
15+
package: package()
16+
]
17+
end
18+
19+
def application do
20+
[
21+
extra_applications: [:logger]
22+
]
23+
end
24+
25+
defp deps do
26+
[
27+
{:plug, "~> 1.7"},
28+
{:gettext, ">= 0.0.0"},
29+
{:ex_doc, "~> 0.19", only: :dev, runtime: false}
30+
]
31+
end
32+
33+
defp description do
34+
"A plug to set locale for web applications."
35+
end
36+
37+
defp package do
38+
[
39+
name: :locale_plug,
40+
licenses: ["MIT"],
41+
maintainers: ["goofansu"],
42+
links: %{"Github" => "https://github.com/goofansu/locale_plug"}
43+
]
44+
end
45+
end

mix.lock

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%{
2+
"earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"},
3+
"ex_doc": {:hex, :ex_doc, "0.20.1", "88eaa16e67c505664fd6a66f42ddb962d424ad68df586b214b71443c69887123", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
4+
"gettext": {:hex, :gettext, "0.16.1", "e2130b25eebcbe02bb343b119a07ae2c7e28bd4b146c4a154da2ffb2b3507af2", [:mix], [], "hexpm"},
5+
"makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
6+
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
7+
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
8+
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
9+
"plug": {:hex, :plug, "1.8.0", "9d2685cb007fe5e28ed9ac27af2815bc262b7817a00929ac10f56f169f43b977", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
10+
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
11+
}

test/locale_plug_test.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule LocalePlugTest do
2+
use ExUnit.Case
3+
doctest LocalePlug
4+
end

test/test_helper.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)