pseudoloc v0.2.1 Pseudoloc

Creates a pseudolocalized translation of Gettext data files.

Because this module is designed to work with Gettext, it specifically ignores interpolated sections of the strings it localizes.

Link to this section Summary

Types

A mapping of individual graphemes to a list of alternate representations.

Represents a range of text within a string by starting index and length.

Functions

Gets the ranges within the text that need to be localized.

Localizes the grapheme if there are valid alternates.

Localizes text within the range with the alternates.

Localizes text with the default alternates.

Localizes text with the given alternates.

Link to this section Types

Link to this type

alternates()
alternates() :: %{optional(String.t()) => [String.t()]}

A mapping of individual graphemes to a list of alternate representations.

Both the key and all entries in the value list should be a single character.

Examples

%{
  "a" => ["à", "á", "å"],
  "b" => ["ḅ"]
}

Represents a range of text within a string by starting index and length.

Link to this section Functions

Link to this function

get_localizable_ranges(text)
get_localizable_ranges(String.t()) :: [range()]

Gets the ranges within the text that need to be localized.

In other words, the ranges of the text that are not interpolations.

Returns a list of tuples containing the index and length of each range to be localized.

Examples

A string with no interpolations:

iex> Pseudoloc.get_localizable_ranges("foo")
[{0, 3}]

A string consisting of only interpolations:

iex> Pseudoloc.get_localizable_ranges("%{foo}")
[]

A string containing multiple interpolations:

iex> Pseudoloc.get_localizable_ranges("foo%{bar}baz%{quux}quuux")
[{0, 3}, {9, 3}, {19, 5}]
Link to this function

localize_grapheme(grapheme, alternates)
localize_grapheme(String.t(), alternates()) :: String.t()

Localizes the grapheme if there are valid alternates.

Examples

Returns the grapheme unchanged if there are no alternates:

iex> Pseudoloc.localize_grapheme("a", %{"b" => ["ḅ"]})
"a"

Returns a random alternative if they exist:

iex> Pseudoloc.localize_grapheme("a", %{"a" => ["α"]})
"α"
iex> alts = ["1", "2", "3"]
iex> Pseudoloc.localize_grapheme("a", %{"a" => alts}) in alts
true
Link to this function

localize_range(text, range, alternates)
localize_range(String.t(), range(), alternates()) :: String.t()

Localizes text within the range with the alternates.

Examples

iex> Pseudoloc.localize_range("foo", {1, 1}, %{"o" => ["ṓ"]})
"fṓo"
Link to this function

localize_string(text)
localize_string(String.t()) :: String.t()

Localizes text with the default alternates.

See localize_string/2 for details.

Link to this function

localize_string(text, alternates)
localize_string(String.t(), alternates()) :: String.t()

Localizes text with the given alternates.

Examples

Localizing the non-interpolated sections of a string:

iex> alternates = %{"a" => ["α"], "f" => ["ϝ"], "u" => ["ṵ"]}
iex> text = "foo%{bar}baz%{quux}quuux"
iex> Pseudoloc.localize_string(text, alternates)
"ϝoo%{bar}bαz%{quux}qṵṵṵx"