From e72ca03799aad6fd688ba5888eda21a4f4f98367 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Thu, 15 Sep 2022 14:24:34 -0700 Subject: [PATCH] feat(all): Implement stubs for lit-localize support PiperOrigin-RevId: 474657115 --- localization/localize.ts | 29 +++++++++++++++++++++++++++++ localization/localize_test.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 localization/localize.ts create mode 100644 localization/localize_test.ts diff --git a/localization/localize.ts b/localization/localize.ts new file mode 100644 index 0000000000..9d3dffa0d3 --- /dev/null +++ b/localization/localize.ts @@ -0,0 +1,29 @@ +/** + * @license + * Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import {TemplateResult} from 'lit'; + +/** Options object to the `msg()` function */ +export interface MsgOptions { + id?: string; + desc?: string; +} + +/** stand-in for lit-localize str function */ +export function str( + strings: TemplateStringsArray, ...values: unknown[]): string { + let out = strings[0]; + for (let i = 1; i < strings.length; i++) { + out += String(values[i - 1]) + strings[i]; + } + return out; +} + +/** stand-in for lit-localize msg function */ +export function msg( + template: T, options?: MsgOptions): T { + return template; +} \ No newline at end of file diff --git a/localization/localize_test.ts b/localization/localize_test.ts new file mode 100644 index 0000000000..2af0e31a7a --- /dev/null +++ b/localization/localize_test.ts @@ -0,0 +1,29 @@ +/** + * @license + * Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import 'jasmine'; + +import {html} from 'lit'; + +import {msg, str} from './localize.js'; + + +describe('localize test', () => { + it('passes through TemplateResults', () => { + const expression = html`world!` + const result = msg(html`
Hello ${expression}
`); + expect(msg(result)).toEqual(result); + }); + + it('passes through strings', () => { + expect(msg('Hello!')).toEqual('Hello!'); + }); + + it('converts str templates to strings', () => { + const expression = 'world'; + expect(msg(str`Hello ${expression}!`)).toEqual('Hello world!'); + }); +});