-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add localization to core-interop and hierarchy-builder packages (#349)
* Add localization * Add changeset * Add eslint disable * Resolve comments * Add test for 100% coverage * Remove unused export * Run extract api
- Loading branch information
Showing
14 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Localization } from "@itwin/core-common"; | ||
import { LOCALIZATION_NAMESPACE, LocalizationFunction } from "@itwin/presentation-hierarchy-builder"; | ||
|
||
/** | ||
* Create a `LocalizationFunction` that uses [Localization]($core-common) API to register the namespace and create a localized string. | ||
* @beta | ||
*/ | ||
export async function createLocalizationFunction(localization: Localization): Promise<LocalizationFunction> { | ||
await localization.registerNamespace(LOCALIZATION_NAMESPACE); | ||
return (input) => localization.getLocalizedString(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { expect } from "chai"; | ||
import sinon from "sinon"; | ||
import { Localization } from "@itwin/core-common"; | ||
import { LOCALIZATION_NAMESPACE } from "@itwin/presentation-hierarchy-builder"; | ||
import { createLocalizationFunction } from "../core-interop/Localization"; | ||
|
||
describe("createTranslator", () => { | ||
it("creates a localization function using provided core `Localization` object", async () => { | ||
const registerNamespaceSpy = sinon.spy(); | ||
const localization = { | ||
getLocalizedString: (input: string) => `${input}_localized`, | ||
registerNamespace: registerNamespaceSpy, | ||
} as unknown as Localization; | ||
const translator = await createLocalizationFunction(localization); | ||
expect(registerNamespaceSpy).to.be.calledWith(LOCALIZATION_NAMESPACE); | ||
const result = translator("Test"); | ||
expect(result).to.be.eq("Test_localized"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/full-stack-tests/src/hierarchy-builder/Localization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { expect } from "chai"; | ||
import { IModelApp } from "@itwin/core-frontend"; | ||
import { createLocalizationFunction } from "@itwin/presentation-core-interop"; | ||
import { setLocalizationFunction } from "@itwin/presentation-hierarchy-builder"; | ||
import { translate } from "@itwin/presentation-hierarchy-builder/lib/cjs/hierarchy-builder/Localization"; | ||
import { initialize, terminate } from "../IntegrationTests"; | ||
|
||
describe("Stateless hierarchy builder", () => { | ||
describe("Localization", () => { | ||
beforeEach(async () => { | ||
await initialize(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await terminate(); | ||
}); | ||
|
||
it("translates strings using `IModelApp.localization`", async function () { | ||
const localizationFunction = await createLocalizationFunction(IModelApp.localization); | ||
setLocalizationFunction(localizationFunction); | ||
const result = translate("grouping.other-label"); | ||
expect(result).to.be.eq("Òthér"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# start off ignoring everything | ||
* | ||
# then add back only the files we want | ||
!*.md | ||
|
||
!lib/**/*.d.ts | ||
!lib/**/*.d.ts.map | ||
!lib/**/*.js | ||
!lib/**/*.js.map | ||
!lib/**/public/**/* | ||
|
||
# then ignore some stuff again | ||
lib/**/test/** | ||
lib/**/public/locales/en-PSEUDO/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/hierarchy-builder/public/locales/en/PresentationHierarchyBuilder.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"grouping": { | ||
"other-label": "Other", | ||
"unspecified-label": "Not specified" | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/hierarchy-builder/src/hierarchy-builder/Localization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
/** A localization function implementation returns the same input. */ | ||
const NOOP_LOCALIZATION_FUNCTION = (input: string) => input; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
let g_localizationFunction = NOOP_LOCALIZATION_FUNCTION; | ||
|
||
/** | ||
* An type for a localization function used by this package. | ||
* @beta | ||
*/ | ||
export type LocalizationFunction = (input: string) => string; | ||
|
||
/** | ||
* A namespace that is used for localization. | ||
* @beta | ||
*/ | ||
export const LOCALIZATION_NAMESPACE = "PresentationHierarchyBuilder"; | ||
|
||
/** | ||
* Set localization function to use by this package. By default the package uses a no-op localization function. | ||
* @beta | ||
*/ | ||
export function setLocalizationFunction(localizationFunction?: LocalizationFunction) { | ||
g_localizationFunction = localizationFunction || NOOP_LOCALIZATION_FUNCTION; | ||
} | ||
|
||
/** | ||
* Use localization function that is set in this package. | ||
* @internal | ||
*/ | ||
export function translate(input: string) { | ||
return g_localizationFunction(`${LOCALIZATION_NAMESPACE}:${input}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { expect } from "chai"; | ||
import { LOCALIZATION_NAMESPACE, setLocalizationFunction, translate } from "../hierarchy-builder/Localization"; | ||
|
||
describe("translate", () => { | ||
it("returns same input with namespace appended when localizationFunction isn't set", () => { | ||
expect(translate("Test")).to.eq(`${LOCALIZATION_NAMESPACE}:Test`); | ||
}); | ||
|
||
it("returns same input with namespace appended when localizationFunction is set to undefined", () => { | ||
setLocalizationFunction(); | ||
expect(translate("Test")).to.eq(`${LOCALIZATION_NAMESPACE}:Test`); | ||
}); | ||
|
||
it("returns input modified by custom localizationFunction with namespace appended when localizationFunction is set", () => { | ||
setLocalizationFunction((input) => `${input}_translated`); | ||
expect(translate("Test")).to.eq(`${LOCALIZATION_NAMESPACE}:Test_translated`); | ||
}); | ||
}); |