Skip to content

Commit

Permalink
resolve mix docs issue + dependency updates (#44)
Browse files Browse the repository at this point in the history
* add mix docs to git workflow to ensure they build

* remove erroneous earmark dep

* remove applications config

* version bump and changelog updates

* add 1.13 to test matrix

* use actions cache@v4

* update deps

* add readme status badges

* cleanup mix file

* docs and typo fixes

* add dependabot config

* update git ignore

* update codeowners
  • Loading branch information
barrieloydall authored Feb 6, 2024
1 parent 40b2f97 commit af336fc
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 115 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Tag Change.org on changes to repo
* @change/co-elixir
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2

updates:
- package-ecosystem: "mix"
directory: "/"
schedule:
interval: "daily"
open-pull-requests-limit: 5

- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
include:
- elixir: '1.11'
otp: '23'
- elixir: '1.13'
otp: '24'
- elixir: '1.15'
otp: '25'

Expand All @@ -30,7 +32,7 @@ jobs:
otp-version: ${{ matrix.otp }}

- name: Restore deps cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-mix-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('**/mix.lock') }}
Expand All @@ -39,7 +41,7 @@ jobs:
${{ runner.os }}-mix-${{ matrix.otp }}-${{ matrix.elixir }}
- name: Restore _build cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: _build
key: ${{ runner.os }}-build-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('**/mix.lock') }}
Expand All @@ -64,3 +66,6 @@ jobs:

- name: Run unit tests
run: mix test

- name: Build docs (validate build)
run: mix docs
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ erl_crash.dump
.sobelow

/doc

# Ignore tarball, built by "mix hex.build"
linguist-*.tar
19 changes: 12 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Changelog

## v0.4.0 (2024-01-31)
* Add ci test and support Elixir 1.15 and OTP 25
* Upgrade `ex_cldr` to version 2.37
## v0.4.0 (2024-02-05)
* Now requires Elixir ~> 1.11
* Add Elixir 1.15 and OTP 25 to ci test matrix
* Upgrade `ex_cldr` to version 2.37
* Add mix docs to ci workflow, to ensure docs build after changes
* Add Elixir 1.13 OTP 24 to ci test matrix
* Remove unused `earmark` dep
* Remove unnecessary `applications:` config causing error with `mix docs`
* Update dependencies + credo fixes
* Update readme, add status badges
* Updates docs and typo fixes
+ Add dependabot and codeowner configs

## v0.3.2 (2021-08-11)
Wow, I held on to this release waaaaaay too long. Sorry about that. Also, a *huge thank you to @dolfinus for their contributes* (and nudges)!
Expand Down Expand Up @@ -40,7 +48,6 @@ Here's what changed:
## v0.2.1 (2019-01-25)
* [Add helper function](https://github.com/mertonium/linguist/commit/06807327e5095e54dd584ad5d65469e4358c92b4) for normalizing locales argument in MemorizedVocubalary.t/3. Locales will be made into the format "es-ES" or "es"


## v0.2.0 (2018-10-22)
* **LARGE SCALE REFACTOR** described in [this pull request](https://github.com/mertonium/linguist/pull/22)

Expand All @@ -49,21 +56,19 @@ Here's what changed:
* Bug Fixes
* Fix bug causing interpolations at beginning of string to be missed


## v0.1.0 (2014-07-06)

* Enhancements
* Add `locale` macro for locale definitions
* Support String filepath locale source for automated evaluation
* Suppport arbitrary locale source to fetch keyword list of translations, ie function call, Code.eval_file, etc.
* Support arbitrary locale source to fetch keyword list of translations, ie function call, Code.eval_file, etc.
* Add `t!` lookups where `NoTranslationError` is raised if translation not found

* Backwards incompatible changes
* Rename `Linguist.Compiler` to `Linguist.Vocabulary`
* Locale definitions now required to use `locale/2` macro instead of `use` options
* Update `t` lookups to return `{:ok, translation}` or `{:error, :no_translation}`


## v0.0.1 (2014-06-28)

Initial release
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
# Linguist

[![Test](https://github.com/change/linguist/actions/workflows/test.yml/badge.svg)](https://github.com/change/linguist/actions/workflows/test.yml)
[![Version on Hex.pm](https://img.shields.io/hexpm/v/linguist.svg)](https://hex.pm/packages/linguist)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/linguist)
[![Hex.pm downloads](https://img.shields.io/hexpm/dt/linguist.svg)](https://hex.pm/packages/linguist)
[![License](https://img.shields.io/hexpm/l/linguist.svg)](https://github.com/change/linguist/blob/master/LICENSE)
[![Latest commit](https://img.shields.io/github/last-commit/change/linguist.svg)](https://github.com/change/linguist/commits/master)
![GitHub top language](https://img.shields.io/github/languages/top/change/linguist)

> Linguist is a simple Elixir Internationalization library
## Installation

Add `:linguist` to your `mix.exs` dependencies:

```elixir
def deps do
[
{:linguist, "~> 0.4"}
]
end
```

Update your dependencies:

```bash
$ mix deps.get
```

## Usage

```elixir
defmodule I18n do
use Linguist.Vocabulary

locale "en", [
flash: [
notice: [
Expand All @@ -22,9 +48,8 @@ defmodule I18n do
]
]
]

locale "fr", Path.join([__DIR__, "fr.exs"])

locale "fr", Path.join([__DIR__, "fr.exs"])
end

# fr.exs
Expand Down
3 changes: 2 additions & 1 deletion lib/linguist.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Linguist do
@moduledoc false
defmodule NoTranslationError do
defexception [:message]

Expand All @@ -13,7 +14,7 @@ defmodule Linguist do
@impl true
def exception(value) do
msg =
"Invalid locale: expected a locale in the format 'es-ES' or 'es', but recieved: #{value}"
"Invalid locale: expected a locale in the format 'es-ES' or 'es', but received: #{value}"

%LocaleError{message: msg}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/linguist/cldr_backend.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Linguist.Cldr do
@moduledoc """
Backend Module for Cldr App configuration, required for ~> 2.0
Backend Module for Cldr App configuration, required for ~> 2.0.
"""
use Cldr,
otp_app: :linguist,
Expand Down
40 changes: 22 additions & 18 deletions lib/linguist/compiler.ex
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
defmodule Linguist.Compiler do
@moduledoc """
Translation Compiler Module.
"""
alias Linguist.Cldr.Number.Cardinal
alias Linguist.NoTranslationError

@doc ~S"""
Compiles keyword list of transactions into function definitions AST
Compiles keyword list of transactions into function definitions AST.
Examples
## Examples
iex> Linguist.Compiler.compile(en: [
hello: "Hello %{name}",
alert: "Alert!"
])
iex> Linguist.Compiler.compile(en: [
hello: "Hello %{name}",
alert: "Alert!"
])
quote do
def t(locale, path, binding \\ [])
quote do
def t(locale, path, binding \\ [])
def t("en", "hello", bindings), do: "Hello " <> Keyword.fetch!(bindings, :name)
def t("en", "alert", bindings), do: "Alert!"
def t("en", "hello", bindings), do: "Hello " <> Keyword.fetch!(bindings, :name)
def t("en", "alert", bindings), do: "Alert!"
def t(_locale, _path, _bindings), do: {:error, :no_translation}
def t!(locale, path, bindings \\ []) do
case t(locale, path, bindings) do
{:ok, translation} -> translation
{:error, :no_translation} ->
raise %NoTranslationError{message: "#{locale}: #{path}"}
def t(_locale, _path, _bindings), do: {:error, :no_translation}
def t!(locale, path, bindings \\ []) do
case t(locale, path, bindings) do
{:ok, translation} -> translation
{:error, :no_translation} ->
raise %NoTranslationError{message: "#{locale}: #{path}"}
end
end
end
end
end
"""

@interpol_rgx ~r/
Expand All @@ -41,6 +44,7 @@ defmodule Linguist.Compiler do
@escaped_interpol_rgx ~r/%%{/
@simple_interpol "%{"

# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
def compile(translations) do
langs =
translations
Expand Down
51 changes: 27 additions & 24 deletions lib/linguist/memorized_vocabulary.ex
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
defmodule Linguist.MemorizedVocabulary do
alias Linguist.Cldr.Number.Cardinal
alias Linguist.Compiler
alias Linguist.{LocaleError, NoTranslationError}
alias Linguist.{Compiler, LocaleError, NoTranslationError}

defmodule TranslationDecodeError do
defexception [:message]
end

@moduledoc """
Defines lookup functions for given translation locales, binding interopolation
Defines lookup functions for given translation locales, binding interpolation.
Locales are defined with the `locale/2` function, accepting a locale name and
a String path to evaluate for the translations list.
For example, given the following translations :
locale "en", [
flash: [
notice: [
hello: "hello %{first} %{last}",
For example, given the following translations:
locale "en", [
flash: [
notice: [
hello: "hello %{first} %{last}",
]
],
users: [
title: "Users",
]
]
],
users: [
title: "Users",
]
]
locale "fr", Path.join([__DIR__, "fr.exs"])
locale "fr", Path.join([__DIR__, "fr.exs"])
This module will respond to these functions:
this module will respond to these functions :
t("en", "flash.notice.hello", bindings \\ []), do: # ...
t("en", "users.title", bindings \\ []), do: # ...
t("fr", "flash.notice.hello", bindings \\ []), do: # ...
t("en", "flash.notice.hello", bindings \\ []), do: # ...
t("en", "users.title", bindings \\ []), do: # ...
t("fr", "flash.notice.hello", bindings \\ []), do: # ...
"""
def t(locale, path, bindings \\ [])
def t(nil, _, _), do: raise(LocaleError, nil)
Expand Down Expand Up @@ -118,14 +118,15 @@ defmodule Linguist.MemorizedVocabulary do
end

@doc """
Embeds locales from provided source
Embeds locales from provided source.
* name - The String name of the locale, ie "en", "fr"
* source - The String file path to load YAML from that returns a structured list of translations
Examples
## Examples
locale "es", Path.join([__DIR__, "es.yml"])
locale "es", Path.join([__DIR__, "es.yml"])
"""
def locale(name, source) do
loaded_source = Linguist.MemorizedVocabulary._load_yaml_file(source)
Expand All @@ -134,8 +135,9 @@ defmodule Linguist.MemorizedVocabulary do
end

@doc """
Function used internally to load a yaml file. Please use
the `locale` macro with a path to a yaml file - this function
Function used internally to load a yaml file.
Please use the `locale` macro with a path to a yaml file - this function
will not work as expected if called directly.
"""
def _load_yaml_file(source) do
Expand All @@ -161,6 +163,7 @@ defmodule Linguist.MemorizedVocabulary do

@doc """
Recursive function used internally for loading yaml files.
Not intended for external use
"""
def _yaml_reducer({key, value}, acc) when is_binary(value) do
Expand Down
Loading

0 comments on commit af336fc

Please sign in to comment.