Many features of the Internationalization plugin merely access the browser-provided Intl APIs, such as for formatting dates and times, identifying plural types, and so on. When writing code, you may as well access these APIs directly rather than through the script interface, so those APIs are not duplicated here. Instead the script interface only provides access to string lookups for translations.
The code sample below demonstrates how it can be used.
const intl = runtime.objects.Internationalization;
// Normal lookup having to repeat the context
let string1 = intl.lookup("forest-world.chapter-1.intro-text.title");
let string2 = intl.lookup("forest-world.chapter-1.intro-text.message");
let string3 = intl.lookup("forest-world.chapter-1.intro-text.start");
// Shorter lookups using a context
const ctx = intl.createContext("forest-world.chapter-1.intro-text");
string1 = ctx.lookup(".title");
string2 = ctx.lookup(".message");
string3 = ctx.lookup(".start");
I18NLookupContext APIs
- lookup(context, ...args)
- lookupPlural(context, count, ...args)
- These methods are equivalent to those on the Internationalization script interface, but may use contexts relative to the context this
I18NLookupContext
was created with.
- createContext(context)
- Create another
I18NLookupContext
for the given context, which must be relative. This allows creating further I18NLookupContext
objects for more deeply nested contexts.