@itwin/core-i18n
Version:
iTwin.js localization code
91 lines • 4.24 kB
TypeScript
/** @packageDocumentation
* @module Localization
*/
import { i18n, InitOptions, Module, TOptionsBase } from "i18next";
import { DetectorOptions } from "i18next-browser-languagedetector";
import { HttpBackendOptions as BackendOptions } from "i18next-http-backend";
import type { Localization } from "@itwin/core-common";
/** Options for ITwinLocalization
* @public
*/
export interface LocalizationOptions {
urlTemplate?: string;
backendPlugin?: Module;
detectorPlugin?: Module;
initOptions?: InitOptions;
backendHttpOptions?: BackendOptions;
detectorOptions?: DetectorOptions;
}
/** Supplies localizations for iTwin.js
* @note this class uses the [i18next](https://www.i18next.com/) package.
* @public
*/
export declare class ITwinLocalization implements Localization {
i18next: i18n;
private readonly _initOptions;
private readonly _backendOptions;
private readonly _detectionOptions;
private readonly _namespaces;
constructor(options?: LocalizationOptions);
initialize(namespaces: string[]): Promise<void>;
/** Replace all instances of `%{key}` within a string with the translations of those keys.
* For example:
* ``` ts
* "MyKeys": {
* "Key1": "First value",
* "Key2": "Second value"
* }
* ```
*
* ``` ts
* i18.translateKeys("string with %{MyKeys.Key1} followed by %{MyKeys.Key2}!"") // returns "string with First Value followed by Second Value!"
* ```
* @param line The input line, potentially containing %{keys}.
* @returns The line with all %{keys} translated
* @public
*/
getLocalizedKeys(line: string): string;
/** Return the translated value of a key.
* @param key - the key that matches a property in the JSON localization file.
* @note The key includes the namespace, which identifies the particular localization file that contains the property,
* followed by a colon, followed by the property in the JSON file.
* For example:
* ``` ts
* const dataString: string = IModelApp.localization.getLocalizedString("iModelJs:BackgroundMap.BingDataAttribution");
* ```
* assigns to dataString the string with property BackgroundMap.BingDataAttribution from the iModelJs.json localization file.
* @returns The string corresponding to the first key that resolves.
* @throws Error if no keys resolve to a string.
* @public
*/
getLocalizedString(key: string | string[], options?: TOptionsBase): string;
/** Gets the English translation.
* @param namespace - the namespace that identifies the particular localization file that contains the property.
* @param key - the key that matches a property in the JSON localization file.
* @returns The string corresponding to the first key that resolves.
* @throws Error if no keys resolve to a string.
* @internal
*/
getEnglishString(namespace: string, key: string | string[], options?: TOptionsBase): string;
/** Get the promise for an already registered Namespace.
* @param name - the name of the namespace
* @public
*/
getNamespacePromise(name: string): Promise<void> | undefined;
/** @internal */
getLanguageList(): readonly string[];
/** override the language detected in the browser */
changeLanguage(language: string): Promise<void>;
/** Register a new Namespace and return it. If the namespace is already registered, it will be returned.
* @param name - the name of the namespace, which is the base name of the JSON file that contains the localization properties.
* @note - The registerNamespace method starts fetching the appropriate version of the JSON localization file from the server,
* based on the current locale. To make sure that fetch is complete before performing translations from this namespace, await
* fulfillment of the readPromise Promise property of the returned LocalizationNamespace.
* @see [Localization in iTwin.js]($docs/learning/frontend/Localization.md)
* @public
*/
registerNamespace(name: string): Promise<void>;
/** @internal */
unregisterNamespace(name: string): void;
}
//# sourceMappingURL=ITwinLocalization.d.ts.map