This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
A free & type-safe way to interact with the Google Translation API
version 1.0.0
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.1
Committed At 2018-07-24 16:14:49 UTC
elm-lang/http 1.0.0 <= v < 2.0.0 1.0.0
elm-lang/html 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

README

GTranslate

The goal of this library is to provide a type-safe way to interact with the Google Translation api. This project will eventually come in two flavors: Free & Official. Currently, only the Free module is implemented which allows you to make a limited number of translations (try to avoid hundreds of requests/min from the same ip).

GTranslate.Free

This module takes advantage of a legacy endpoint which allows us to generate translations for free. No Google account, api key, or any other credentials are needed. I'd recommend this library for prototyping and personal projects that don't require heavy amounts of translations, because Google will throttle you if you make too many requests. As long as you don't spam Google with hundreds of requests/minute you shouldn't be affected.

GTranslate.Official

This should come as no surprise, but this module will eventually utilize the official Google Translation API. This will require authentication tokens in order to make requests. If you're cheap and you still don't want to pay Google, I'd recommend looking into https://www.npmjs.com/package/google-translate-token which will generate tokens for free.

Example

Below is a short program that translates a list of Spanish phrases into English. This snippet also shows how to use the apply function to update existing data structures with completed translations. (The logic for displaying these phrases are entirely up to you)

import Http
import GTranslate.Free exposing (..)


                        -- MESSAGES --
type Msg 
    = Response (Result Http.Error Translation )
    | Translate 
    | Apply 


                        -- Models --    
type alias Phrase =
    { text: String
    , id: Int
    }

type alias Model =
    { phrases: List Phrase
    , translations: List Translation 
    }


init : ( Model, Cmd Msg )
init = 
    ({ phrases =  
        [ Phrase "Caballo regalado no se le mira el diente." 1
        , Phrase "Al mal tiempo, buena cara." 2
        , Phrase "A falta de pan, buenas son tortas." 3
        , Phrase "Barriga llena, corazón contento." 4
        ]
    , translations = [] }
    , Cmd.none)


-- our configuration record used in the application
customConfig : Config Phrase Msg
customConfig =
    Response -- 'Response' is our message that will process incoming Translations
        |> initConfig "en"      -- specify that english is our target language
        |> withSourceLang "es"  -- **OPTIONAL** specify that spanish is our source language
        |> withUID (\ p -> toString p.id ) -- **OPTIONAL** the function used to generate a unique id for each record



update : Msg -> Model -> ( Model, Cmd Msg )
update message model =
    case message of 

        Apply -> 

             let 
                -- The update function used to apply a translation to a phrase
                update = (\ translation phrase -> { phrase | text = translatedText translation } )

                -- Apply the already completed translatations to our list of phrases
                newPhrases = model.phrases |> apply customConfig update model.translations 
            in

            -- update our model 
            ({ model | phrases = newPhrases }, Cmd.none )


        Translate ->
            -- Translate the all stored phrases
            (model, batchRecords customConfig (\p -> p.text) model.phrases ) 

        Response (Ok translation) -> 
            -- store our completed translations
            ({ model | translations = translation :: model.translations }, Cmd.none)

        Response (Err msg) ->  
            -- handle error here
            (model, Cmd.none)

Running Examples Locally

Clone this repo into a new project folder and run install script. To change which demo you want to run, modify the examples/static/index.js file to point to one of the other elm files in that folder.

With npm

$ git clone https://github.com/ndortega/elm-gtranslate.git
$ cd elm-gtranslate
$ npm install
$ npm run dev

With yarn

$ git clone https://github.com/ndortega/elm-gtranslate.git 
$ cd elm-gtranslate
$ yarn
$ yarn dev
 ```