-
Notifications
You must be signed in to change notification settings - Fork 2
/
UiCore.ts
92 lines (79 loc) · 2.93 KB
/
UiCore.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utilities
*/
/** Import color variables, layout variables and Sass classes barrel file */
import "./colorthemes.scss";
import "./colorvariables.scss";
import "./layout-variables.scss";
import "./classes.scss";
import { Logger } from "@itwin/core-bentley";
import type { Localization } from "@itwin/core-common";
import { getObjectClassName } from "./utils/getObjectClassName";
/** Manages the Localization service for the core-react package.
* @public
*/
export class UiCore {
private static _initialized = false;
private static _localization?: Localization;
/**
* Registers the Localization service namespace for UiCore.
* @param localization The internationalization service created by the host application.
*/
public static async initialize(localization: Localization): Promise<void> {
if (UiCore._initialized) {
Logger.logInfo(
UiCore.loggerCategory(UiCore),
`UiCore.initialize already called`
);
return;
}
UiCore._localization = localization;
await UiCore._localization.registerNamespace(UiCore.localizationNamespace);
UiCore._initialized = true;
}
/** Unregisters the UiCore localization namespace */
public static terminate() {
if (UiCore._localization)
UiCore._localization.unregisterNamespace(UiCore.localizationNamespace);
UiCore._localization = undefined;
UiCore._initialized = false;
}
/** Determines if UiCore has been initialized */
public static get initialized(): boolean {
return UiCore._initialized;
}
/** The internationalization service namespace. */
public static get localizationNamespace(): string {
return "UiCore";
}
/** Calls localization.getLocalizedString with the "UiCore" namespace. Do NOT include the namespace in the key.
* @deprecated in 4.12.x. Do not use this internally, this is replaced by `useTranslation`.
* @internal
*/
public static translate(key: string | string[]): string {
if (!UiCore._localization) {
Logger.logError(
UiCore.loggerCategory(this),
`translate: UiCore must be initialize with a localization provider. Returning blank string.`
);
return "";
}
return UiCore._localization.getLocalizedString(
`${UiCore.localizationNamespace}:${key}`
);
}
/** @internal */
public static get packageName(): string {
return "core-react";
}
/** @internal */
public static loggerCategory(obj: any): string {
const className = getObjectClassName(obj);
const category = UiCore.packageName + (className ? `.${className}` : "");
return category;
}
}