Skip to content

Glottologist's API

Arthur Guiot edited this page Jan 22, 2019 · 3 revisions

⚠️ Here, we are working on the same project

assign

This function will assign the translation in any languages to a model. Here is an example:

glot.assign("Hello", {
   en: "Hello",  // you'll need to add the default language, even if it's in the model name
   fr: "Bonjour"
   ...
})

Models

You'll be able to add models with ${}. The object that will have all the data in the data object.

glot.assign("Hello ...", {
   en: "Hello ${data.name}",
   fr: "Bonjour ${data.name}",
   ...
})

glot.get("Hello ...", { name: "Arthur" }, "en") // we'l come back to this function later
=> "Hello Arthur"

get()

As you may think 🤔, this function will get the value of the assigned one.

glot.get("Hello")
=> "Hello"

Custom lang

By default, Glottologist will always take the browser lang. If it's not supported, it will choose the first item in the list. But, if you add the 2 letters lang code at the end of the get function, it will force Glottologist to choose this one.

glot.get("Hello", "fr")
=> "Bonjour"

Data

Data will be your second option in the get function. If you want to choose a custom lang, it will be the third option.

glot.get("Hello ...", { name: "Arthur" })
=> "Hello Arthur"
glot.get("Hello ...", { name: "Arthur" }, "fr")
=> "Bonjour Arthur"

render

This function will be very useful, if you want to automatically push all your translated models to your html page. Here is how you should integrate it in your html page:

<div class="demo" glot-model="Hello">Hello</div>
glot.render() // will render with user's language
glot.render("fr") // will render html in french

t

This function will translate a string in the language you want, using the unofficial and free Google Translate API.

glot.t("Hello, how are you?", "fr").then(result => {
    console.log(result)
})
=> "Salut comment allez-vous?"

autoGen

This function will be similar to the t function but will be able to translate the same phrase automatically in different languages. ⚠️ This function will not return any promises, so it will only be useful during the development. We don't recommend to use it in production.

glot.autoGen("Hello", ["en","fr","de","sp","zh"]) // will translate hello in English, French, German, Spanish and Chinese.

import

If you don't want to have glot.assign everywhere in your js file, you can export the data of glot.data, and put it in a .json file on your server, and import it using this function.

glot.import("data.json").then(() => {
	glot.render()
})