Tolgee
TolgeeCore
Allows you to gradually initialize Tolgee instance. You can repeatedly call use and updateDefaults. Finally when you call init it will apply everything at once and return TolgeeInstance.
function TolgeeCore(): TolgeeChainer;
Basic usage:
import { TolgeeCore } from '@tolgee/core';
const tolgee = TolgeeCore()
.use(...)
.updateDefaults(...)
.init(...);
Constructor called Tolgee is located in @tolgee/web package as it automatically includes browser-specific features, which can't be included directly in the core. Use that if you run Tolgee in the browser.
use
Add tolgee plugin.
TolgeeCore().use(plugin: TolgeePlugin): TolgeeChainer
Plugins are applied after init is called. To see how TolgeePlugin interface work check Plugin API.
updateDefaults
Updates Tolgee options before init is called. Extends existing options,
so it only changes the fields, that are listed.
TolgeeCore().updateDefaults(options: TolgeeOptions): TolgeeChainer
You can call it multiple times and rewrite only part of the configuration:
const tolgee = TolgeeCore()
.updateDefaults({ apiKey: 'key' })
.updateDefaults({ language: 'en' })
.init({ apiUrl: 'url' });
// resulting configuration will be:
{
apiUrl: 'url',
apiKey: 'key',
language: 'en'
}
TolgeeInstance
Represents initialized tolgee instance. Allows you to read translations and manipulate lanugage or cache.
run
Changes internal state to running: true and loads initial files. Starts Observer if present.
tolgee.run(): Promise<void>
stop
Changes internal state to running: false and stops runnable plugins.
tolgee.stop(): void
tolgee.init(options: TolgeeOptions): TolgeeInstance
on
Allows you to listen to tolgee events.
tolgee.on(event: TolgeeEvent, handler: ListenerHandler): Listener
Returned object has tolgee.unsubscribe, call it to stop listening.
const listner = tolgee.on(...)
listener.unsubscribe()
Check Events for details.
t
Returns formatted translation based on current language. If Observer is present and tolgee is running, wraps the result to be identifiable in the DOM.
tolgee.t(key: string, defaultValue?: string, options?: CombinedOptions): string
tolgee.t(key: string, options?: CombinedOptions): string
tolgee.t(props: TranslateProps): string
t function provides flexibility in how to pass the options:
// with arguments
const translation = t('key', 'default value', {
ns: 'test',
// any unknown property in CombinedOptions is taken as param
parameter: 'parameter',
});
// with props object
const translation = t({
key: 'key',
defaultValue: 'default value',
ns: 'test',
params: { parameter: 'parameter' },
});
All the options:
key:string- translation keydefaultValue:string- value displayed when no translation is availablenoWrap:boolean- returns translation without wrapping (default: false)orEmpty:boolean- return empty string instead of key if translation doesn't exist (default: false)params:Record<string, value>object with parameters for the formatterns:string- namespace, if not provided default namespace is usedlanguage:string- override current Tolgee language. This way you can override current language and use different one from the cache. Language needs to be loaded manually bytolgee.loadRecord.
tfunction serves only for reading, not for loading new namespaces or languages. UseaddActiveNsfor loading additional namespaces.