-
Notifications
You must be signed in to change notification settings - Fork 2
/
UiComponents.ts
96 lines (84 loc) · 3.02 KB
/
UiComponents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Common
*/
import { enablePatches } from "immer";
import { Logger } from "@itwin/core-bentley";
import type { Localization } from "@itwin/core-common";
import { UiCore } from "@itwin/core-react";
import { getObjectClassName } from "@itwin/core-react";
/**
* Manages the localization service for the components-react package.
* @public
*/
export class UiComponents {
private static _initialized = false;
private static _localization?: Localization;
/**
* Registers the localization service namespace for UiComponents. Also initializes UiCore.
* @param localization The internationalization service created by the host application.
*/
public static async initialize(localization: Localization): Promise<void> {
if (UiComponents._initialized) {
Logger.logInfo(
UiComponents.loggerCategory(UiComponents),
`UiComponents.initialize already called`
);
return;
}
enablePatches();
UiComponents._localization = localization;
await UiComponents._localization.registerNamespace(
UiComponents.localizationNamespace
);
await UiCore.initialize(UiComponents._localization);
UiComponents._initialized = true;
}
/** Unregisters the UiComponents localization namespace */
public static terminate() {
if (UiComponents._localization)
UiComponents._localization.unregisterNamespace(
UiComponents.localizationNamespace
);
UiComponents._localization = undefined;
UiCore.terminate();
UiComponents._initialized = false;
}
/** Determines if UiComponents has been initialized */
public static get initialized(): boolean {
return UiComponents._initialized;
}
/** The internationalization service namespace. */
public static get localizationNamespace(): string {
return "UiComponents";
}
/** @internal */
public static get packageName(): string {
return "components-react";
}
/** Calls localization.getLocalizedString with the "UiComponents" namespace. Do NOT include the namespace in the key.
* @internal
*/
public static translate(key: string | string[]): string {
if (!UiComponents._localization) {
Logger.logError(
UiComponents.loggerCategory(this),
`translate: UiComponents.initialize has not been called. Returning blank string.`
);
return "";
}
return UiComponents._localization.getLocalizedString(
`${UiComponents.localizationNamespace}:${key}`
);
}
/** @internal */
public static loggerCategory(obj: any): string {
const className = getObjectClassName(obj);
const category =
UiComponents.packageName + (className ? `.${className}` : "");
return category;
}
}