Translator for gleam

Package Version Hex Docs Tests

Further documentation can be found at https://hexdocs.pm/translate.

Getting Started

Installation

gleam add translate

Usage

Create a file for each locale in src/

src/
└── locale/
    └── de-CH/
        └── lang.json
    └── en-US/
        └── lang.json

de-CH/lang.json

{
    "hello": {
        "value": "Willkommen"
    },
    "amount_planets": {
        "value": "Es gibt {AMOUNT} Planeten. Aber es waren mal {AMOUNT_BEFORE}."
    }
}

en-US/lang.json

{
    "hello": {
        "value": "Welcome"
    },
    "amount_planets": {
        "value": "There are {CURRENT} planets"
    }
}

And use the translator like this

import translate/translator

let assert Ok(translator) =
  translator.new_translator("de-CH")
  |> translator.with_directory("src/locale")
  |> translator.from_json()

let assert Ok(value) =
  translator
  |> translator.get_key("hello")
  // value -> Willkommen

let assert Ok(value) =
  translator
  |> translator.get_key_with_args("amount_planets",  [#("CURRENT", "7"), #("BEFORE", "8")])
  // Es gibt 7 Planeten. Aber es waren mal 8.

Development

Fork the project and clone it

You can run the tests with:

gleam test
Search Document