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 | |
Benedikt Meurer | beeb30b | 2025-01-31 15:18:59 | [diff] [blame] | 8 | import {html, i18nTemplate, render} from './lit.js'; |
Benedikt Meurer | 8d94a72 | 2025-01-02 13:00:42 | [diff] [blame] | 9 | |
| 10 | describe('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 Meurer | beeb30b | 2025-01-31 15:18:59 | [diff] [blame] | 34 | const result = i18nTemplate(strings, uiStrings.placeholder, {string: 'STRING', template: html`TEMPLATE`}); |
| 35 | const element = render(result, document.createElement('div'), {host: this}); |
Benedikt Meurer | 8d94a72 | 2025-01-02 13:00:42 | [diff] [blame] | 36 | 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 Meurer | beeb30b | 2025-01-31 15:18:59 | [diff] [blame] | 46 | const result = i18nTemplate(strings, uiStrings.placeholder, {string: 'STRING', template: html`TEMPLATE`}); |
| 47 | const element = render(result, document.createElement('div'), {host: this}); |
Benedikt Meurer | 8d94a72 | 2025-01-02 13:00:42 | [diff] [blame] | 48 | assert.deepEqual( |
| 49 | (element.parentNode as HTMLDivElement).innerText, 'a message with a TEMPLATE and STRING placeholder'); |
| 50 | }); |
| 51 | }); |