Skip to content

Commit

Permalink
feat(all): Implement stubs for lit-localize support
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 474657115
  • Loading branch information
dfreedm authored and copybara-github committed Sep 15, 2022
1 parent f75dd05 commit e72ca03
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions localization/localize.ts
Original file line number Diff line number Diff line change
@@ -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<T extends(string | TemplateResult)>(
template: T, options?: MsgOptions): T {
return template;
}
29 changes: 29 additions & 0 deletions localization/localize_test.ts
Original file line number Diff line number Diff line change
@@ -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`<span>world!</span>`
const result = msg(html`<div tmpl>Hello ${expression}</div>`);
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!');
});
});

0 comments on commit e72ca03

Please sign in to comment.