Benedikt Meurer | 8d94a72 | 2025-01-02 13:00:42 | [diff] [blame^] | 1 | // 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 | |
| 5 | import * as i18n from '../../core/i18n/i18n.js'; |
| 6 | import * as i18nRaw from '../../third_party/i18n/i18n.js'; |
| 7 | |
| 8 | import * as LitHtml from './lit-html.js'; |
| 9 | |
| 10 | const {html} = LitHtml; |
| 11 | |
| 12 | describe('i18nTemplate', () => { |
| 13 | const uiStrings = {placeholder: 'a message with a {string} and {template} placeholder'}; |
| 14 | let i18nInstance: i18nRaw.I18n.I18n; |
| 15 | |
| 16 | beforeEach(() => { |
| 17 | i18nInstance = new i18nRaw.I18n.I18n(['en-US'], 'en-US'); |
| 18 | i18nInstance.registerLocaleData('en-US', {}); |
| 19 | }); |
| 20 | |
| 21 | function setLocale(locale: string) { |
| 22 | i18n.DevToolsLocale.DevToolsLocale.instance({ |
| 23 | create: true, |
| 24 | data: { |
| 25 | settingLanguage: locale, |
| 26 | navigatorLanguage: locale, |
| 27 | lookupClosestDevToolsLocale: l => l, |
| 28 | }, |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | it('localizes lit templates', () => { |
| 33 | const strings = i18nInstance.registerFileStrings('test.ts', uiStrings); |
| 34 | setLocale('en-US'); |
| 35 | |
| 36 | const result = LitHtml.i18nTemplate(strings, uiStrings.placeholder, {string: 'STRING', template: html`TEMPLATE`}); |
| 37 | const element = LitHtml.render(result, document.createElement('div'), {host: this}); |
| 38 | assert.deepEqual( |
| 39 | (element.parentNode as HTMLDivElement).innerText, 'a message with a STRING and TEMPLATE placeholder'); |
| 40 | }); |
| 41 | |
| 42 | it('localizes lit templates with translations', () => { |
| 43 | i18nInstance.registerLocaleData( |
| 44 | 'de', {'test.ts | placeholder': {message: 'a message with a {template} and {string} placeholder'}}); |
| 45 | const strings = i18nInstance.registerFileStrings('test.ts', uiStrings); |
| 46 | setLocale('de'); |
| 47 | |
| 48 | const result = LitHtml.i18nTemplate(strings, uiStrings.placeholder, {string: 'STRING', template: html`TEMPLATE`}); |
| 49 | const element = LitHtml.render(result, document.createElement('div'), {host: this}); |
| 50 | assert.deepEqual( |
| 51 | (element.parentNode as HTMLDivElement).innerText, 'a message with a TEMPLATE and STRING placeholder'); |
| 52 | }); |
| 53 | }); |