-
Notifications
You must be signed in to change notification settings - Fork 11
Glottologist's API
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"
...
})
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"
As you may think 🤔, this function will get the value of the assigned one.
glot.get("Hello")
=> "Hello"
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 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"
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
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?"
This function will be similar to the t
function but will be able to translate the same phrase automatically in different languages.
glot.autoGen("Hello", ["en","fr","de","sp","zh"]) // will translate hello in English, French, German, Spanish and Chinese.
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()
})