Code Monkey home page Code Monkey logo

elm-i18next's Introduction

Elm i18next - Load and use JSON translations files at runtime

Functions for working with dynamically loaded translations in Elm. PRs and suggestions welcome.

Simple Example

elm install ChristophP/elm-i18next

Then use the module in your app like this.

In JS do:

// translations is a JSON string or JS object
Elm.Main.init({ flags: translations });

Then in elm, you use them in the init function of your app:

import Html exposing (Html)
import I18Next exposing
      ( t
      , tr
      , Translations
      , Delims(..)
      , translationsDecoder
      )
import Json.Encode
import Json.Decode

type alias Model = {
  translations: Translations
}

type Msg = ..

init: Json.Encode.Value -> (Model, Cmd msg)
init flags = ({ translations = initialTranslations } , Cmd.none)
    case JD.decodeValue translationsDecoder flags of
        Ok translations ->
            ( Model translations, Cmd.none )

        Err err ->
            -- handle the error

{- Imagine your translations file looks like this:
  {
    "hallo": "Hallo",
    "greetings": {
      "goodDay": "Good Day.",
      "greetName": "Hi {{name}}"
    }
  }
-}

view : Model -> Html Msg
view model =
    div []
        [ div [] [ text (t model.translations "hello") ] -- "Hallo"
        ,  div [] [ text (t model.translations "greetings.goodDay") ] -- "Good day."
        , div [] [ text (t model.translations "nonExistingKey") ] -- "nonExistingKey"
        , div [] [ text (tr model.translations Curly "greetings.greetName" [("name", "Peter")]) ] -- "Hi Peter"
        ]

Check out more complete examples here

Fetching Translations

If you can't pass the translations as flags but want to fetch them from Elm code instead do the same as in the simple example but apply the decoder to the Http call.

Advanced Stuff: Placeholders and fallback languages

There is also support for placeholders and fallback languages. Check the official docs for usage examples.

Background

Dealing with Translations in Elm has always come with some hoops to jump through. Existing solutions include tricks like passing already translated strings into the elm app as flags or generating Translation modules as a pre-build step like here or here.

Inspired by the i18next client in from the JS world. This elm module allows you to load JSON translation files via HTTP and then use the data in your Elm app. This should allow for a easier-to-use internationalization as existing solutions.

Contributing

If you want to contribute, PRs are highly welcome. If you need a feature or want to share ideas, please open an issue or catch me in the elm slack channel.

elm-i18next's People

Contributors

christophp avatar allanderek avatar

Watchers

Adam Groves avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.