Translations - Joolee/Homey-unofficial-documentation GitHub Wiki
Pulling translated strings from locales/*.json is easy using the provided Homey.__()
method. Sometimes though, you need to be able to provide translations for strings defined elsewhere. Place the following method in your app.js
class to provide this simple functionality.
Note: It does not provide the __variable__
substitution!
Code
getI18nString(i18n) {
const lang = Homey.ManagerI18n.getLanguage();
if (i18n[lang])
return i18n[lang];
else if (i18n['en'])
return i18n['en'];
else
return `Untranslated string: ${i18n}`;
}
Example
const buttonName = {
"en": "Power off",
"nl": "Schakel uit"
}
console.log(`Button name: ${Homey.app.getI18nString(buttonName)}`);
Getting translatable strings from your manifest (app.json) file
The string you need a translation of might already be defined in your manifest file. It would be a shame to do the work twice! You can fetch the translation objects from your full manifest in a few ways:
- In you whole app;
Homey.manifest
orHomey.app.manifest
- In app.js;
this.manifest
Or fetch only a specific driver's information from the manifest:
- In driver.js;
this.getManifest()
- In device.js;
this.getDriver().getManifest()
Example
Get your translated app description:
const appDescr = Homey.manifest.description;
console.log(`App description: ${Homey.app.getI18nString(appDescr)}`);