Packages
fermo
0.14.8
0.20.1
0.20.0
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.1
0.17.0
0.16.7
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.0
0.14.9
0.14.8
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.9
0.13.8
0.13.7
0.13.6
0.13.5
0.13.4
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.1
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.0
0.3.0
0.2.4
0.2.2
0.2.1
0.2.0
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A static site generator
Current section
Files
Jump to
Current section
Files
lib/fermo/i18n.ex
defmodule Fermo.I18n do
def load(path \\ "priv/locales/**/*.yml") do
files = Path.wildcard(path)
translations = Enum.reduce(files, %{}, fn (file, translations) ->
content = YamlElixir.read_from_file(file)
{:ok, atom_keys} = Morphix.atomorphiform(content)
DeepMerge.deep_merge(translations, atom_keys)
end)
{:ok} = I18n.put(translations)
end
@doc """
Cross references all config pages, finding those with the same `:id`
but with different locales.
Each page with an `:id` and `:locale` in its parameters has
a `:localized_paths` Map added indicating which locale has
which path.
E.g.
%{
path: "/",
template: "home.html.slim",
params: %{
id: "home_page",
locale: :en,
localized_paths: %{
en: "/",
it: "/it/"
}
}
}
"""
@callback optionally_build_path_map(map()) :: map()
def optionally_build_path_map(%{i18n: nil} = config), do: config
def optionally_build_path_map(%{path_map: true, i18n: _i18n} = config) do
pages_with_locale_and_id = Enum.filter(
config.pages,
fn %{params: params} ->
Map.has_key?(params, :locale) && Map.has_key?(params, :id)
end
)
path_locale_id = Enum.map(
pages_with_locale_and_id,
fn page ->
%{path: page.path, id: page.params.id, locale: atom(page.params.locale)}
end
)
by_id = Enum.group_by(path_locale_id, &(&1.id))
path_map = Enum.into(
by_id,
%{},
fn {id, pages} ->
{
id,
Enum.into(pages, %{}, fn item -> {item.locale, item.path} end)
}
end
)
pages = Enum.map(
config.pages,
fn %{params: params} = page ->
if Map.has_key?(params, :locale) && Map.has_key?(params, :id) do
map = path_map[params.id]
put_in(page, [:localized_paths], map)
else
page
end
end
)
config
|> put_in([:pages], pages)
|> put_in([:stats, :optionally_build_path_map_completed], Time.utc_now)
end
def optionally_build_path_map(config), do: config
def root_locale(config) do
no_root_locale = Map.get(config, :no_root_locale, false)
if no_root_locale do
nil
else
default_locale = default_locale(config)
if default_locale do
default_locale
else
first_locale(config)
end
end
end
def default_locale(%{default_locale: default_locale}), do: default_locale
def default_locale(_), do: nil
def first_locale(%{i18n: []}), do: nil
def first_locale(%{i18n: [locale | _rest]}), do: locale
def first_locale(_config), do: nil
defp atom(x) when is_atom(x), do: x
defp atom(x), do: String.to_atom(x)
end