blob: 7abadaa699ef1601972162361fb08ae84ecca925 [file] [log] [blame]
Benedikt Meurer8d94a722025-01-02 13:00:421// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import * as i18n from '../../core/i18n/i18n.js';
6import * as i18nRaw from '../../third_party/i18n/i18n.js';
7
Benedikt Meurerbeeb30b2025-01-31 15:18:598import {html, i18nTemplate, render} from './lit.js';
Benedikt Meurer8d94a722025-01-02 13:00:429
10describe('i18nTemplate', () => {
11 const uiStrings = {placeholder: 'a message with a {string} and {template} placeholder'};
12 let i18nInstance: i18nRaw.I18n.I18n;
13
14 beforeEach(() => {
15 i18nInstance = new i18nRaw.I18n.I18n(['en-US'], 'en-US');
16 i18nInstance.registerLocaleData('en-US', {});
17 });
18
19 function setLocale(locale: string) {
20 i18n.DevToolsLocale.DevToolsLocale.instance({
21 create: true,
22 data: {
23 settingLanguage: locale,
24 navigatorLanguage: locale,
25 lookupClosestDevToolsLocale: l => l,
26 },
27 });
28 }
29
30 it('localizes lit templates', () => {
31 const strings = i18nInstance.registerFileStrings('test.ts', uiStrings);
32 setLocale('en-US');
33
Benedikt Meurerbeeb30b2025-01-31 15:18:5934 const result = i18nTemplate(strings, uiStrings.placeholder, {string: 'STRING', template: html`TEMPLATE`});
35 const element = render(result, document.createElement('div'), {host: this});
Benedikt Meurer8d94a722025-01-02 13:00:4236 assert.deepEqual(
37 (element.parentNode as HTMLDivElement).innerText, 'a message with a STRING and TEMPLATE placeholder');
38 });
39
40 it('localizes lit templates with translations', () => {
41 i18nInstance.registerLocaleData(
42 'de', {'test.ts | placeholder': {message: 'a message with a {template} and {string} placeholder'}});
43 const strings = i18nInstance.registerFileStrings('test.ts', uiStrings);
44 setLocale('de');
45
Benedikt Meurerbeeb30b2025-01-31 15:18:5946 const result = i18nTemplate(strings, uiStrings.placeholder, {string: 'STRING', template: html`TEMPLATE`});
47 const element = render(result, document.createElement('div'), {host: this});
Benedikt Meurer8d94a722025-01-02 13:00:4248 assert.deepEqual(
49 (element.parentNode as HTMLDivElement).innerText, 'a message with a TEMPLATE and STRING placeholder');
50 });
51});