Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
elm-translations
Advanced tools
Generate type safe translations for your Elm app
elm-translations
is a command line script that generates an Elm module from your JSON translation files. This module just includes a Translations
type (nested Record), a JSON decoder and a JSON parser, but not the actual translation data. So you just need to generate the Elm code once and then use it for all the languages you want to support in your app by e.g. loading them dynamically at runtime.
Won't compile if you try to access the wrong keys in your Elm code
Let's you organize your translations easily
Just pass a Record and never forget to set a variable again
# get a preview of the generated Elm code
$ npx elm-translations --from your-translations.json
# generate Elm code and store it here: ./src/Translations.elm
$ npx elm-translations --from your-translations.json --out src
# or use a custom module name - will be stored here: ./src/I18n/Trans.elm
$ npx elm-translations --from your-translations.json --module I18n.Trans --out src
# see all possible options
$ npx elm-translations --help
Type safe translations for Elm
Usage
$ elm-translations
Options
--from, -f path to your translations file (JSON)
--module, -m custom Elm module name (default: Translations)
--root, -r key path to use as root (optional)
--out, -o path to a folder where the generated translations should be saved (optional)
--version show version
Examples
$ elm-translations --from en.json
$ elm-translations -f en.json -m I18n.Translations -o src
flags
Translations.elm
file with e.g.:$ npx elm-translations -f en.json -o src
...
<script>
Elm.Main.init({
node: document.getElementById("elm"),
flags: {
// your translation data inlined by e.g. EJS or Handlebars:
// ...
welcome: "Welcome {{name}}!",
home: {
intro: "This App is about ...",
},
// ...
},
});
</script>
...
module Main exposing (..)
import Json.Decode
import Json.Encode
import Translations exposing (Translations)
-- ...
type Model
= Initialized Translations
| Failed Json.Decode.Error
init : Json.Encode.Value -> ( Model, Cmd Msg )
init flags =
case Translations.parse flags of
Ok t ->
( Initialized t, Cmd.none )
Err error ->
( Failed error, Cmd.none )
view : Model -> Html Msg
view model =
case model of
Initialized t ->
div []
[ h1 [] [ text <| t.welcome { name = "John" } ]
, p [] [ text t.home.intro ]
]
Failed error ->
p [] [ text "Error: Cannot proccess translation data" ]
-- ...
There's an example available in the git repository. To let it run locally on your machine, follow these steps:
$ git clone git@github.com:layflags/elm-translations.git
$ cd elm-translations/example
Translations.elm
module$ npx elm-translations --from en.json --out src
$ elm reactor
Or just visit: https://elm-translations-example.surge.sh/
🟢 YES
{ "buttonTitle": "Submit" }
{ "headline": "Welcome to Elm!" }
🔴 NO
{ "button-title": "Submit" }
{ "Headline": "Submit" }
🟢 YES
{ "welcome": "Hi {{name}}!" }
{ "welcome": "Hi {{firstName}} {{lastName}}!" }
🔴 NO
{ "welcome": "Hi {{Name}}!" }
{ "welcome": "Hi {{first_Name}} {{last_Name}}!" }
String
values (nesting possible)!🟢 YES
{ "buttonTitle": "Submit" }
{ "form": { "buttonTitle": "Submit" } }
🔴 NO
{ "count": 3 }
{ "isVisible": true }
{ "name": null }
FAQs
Type safe translations for Elm
The npm package elm-translations receives a total of 4 weekly downloads. As such, elm-translations popularity was classified as not popular.
We found that elm-translations demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.