Writing Localizable Code

To successfully localize your apps and services, strings need to be wrapped with a function appropriate to their file type. Localization in webOS OSE is available for the following programming languages: JavaScript, C/C++ and QML types.

JavaScript

For web apps developed in JavaScript, use iLib to write codes for localization.

Enact Apps

Enact framework offers the $L component as part of its i18n library, which provides functions to map to translated strings.

Example for $L usage
import $L, {toIString} from '@enact/i18n/$L';

$L('Close'); // => "Close" in the current locale
toIString('Close'); // => an ilib IString representing "Close" in the current locale

Non-Enact Apps

For web apps that do not utilize Enact framework, use the getString() API of ResBundle feature in iLib.

  1. First, load the iLib library ilib-web.js through the <script> tag.

    Example for loading ilib-web.js
    <html>
    <head>
    <script src=<path-to-ilib>/lib/ilib-web.js type="text/javascript"></script>
    ...
    </head>
    </html>
    

    For instance, if you're developing a built-in web app, the iLib library can be loaded as follows:

    <script src=file://usr/share/javascript/ilib/lib/ilib-web.js type="text/javascript"></script>
    
  2. After that, use the ResBundle.getString() method of iLib as shown in the example below:

    Example for a non-Enact web app
    var ResBundle = require("ResBundle.js");
    var rb = new ResBundle({locale: "en-US"});
    var str = rb.getString("String 1"); // str is iLib string object
    var jsStr = str.toString(); // jsStr is js string object
    

C++/C

If you are developing apps or services in C++/C, use libwebosi18n to write code for localization.

The following shows an example which uses the getLocString() API of libwebosi18n in C++ and C:

Using getLocString() in C++
#include <webosi18n.h>

std::string locale = "en-US";
const std::string file = "cppstrings.json";
const std::string resources_path = "/usr/share/localization/samplecpp";
ResBundle* resBundle = new ResBundle(locale, file, resources_path);

resBundle->getLocString("String 1");
Using getLocString() in C
#include <webosi18n_C.h>

const char* locale = "en-US";
const char* file = "cstrings.json";
const char* resources_path = "/usr/share/localization/sample_c";
ResBundleC* resBundle = resBundle_createWithRootPath(locale, file, resources_path);

resBundle_getLocString(resBundle, "String 1");

To prepare string resources without using the localization tool and XLIFF, write them in JSON format just like in JavaScript. The format and directory structure are basically the same as what's described above.

You can also specify the name of the JSON file. However, it is recommended that you use cppstrings.json for C++ and cstrings.json for C.

Qt/QML

Basically, you can follow the localization guidelines of Qt. For details, refer to the Qt documentation.

Contents