Skip to content

Commit e20b6c8

Browse files
authored
feat: add i18n supports (#49)
1 parent d237948 commit e20b6c8

File tree

15 files changed

+378
-27
lines changed

15 files changed

+378
-27
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ build {
4848
}
4949

5050
halo {
51-
version = "2.17.0"
51+
version = "2.20"
5252
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/lit/lit/main/packages/localize-tools/config.schema.json",
3+
"sourceLocale": "en",
4+
"targetLocales": [
5+
"es",
6+
"zh-CN",
7+
"zh-TW"
8+
],
9+
"tsConfig": "./tsconfig.json",
10+
"output": {
11+
"mode": "runtime",
12+
"outputDir": "./src/generated/locales",
13+
"localeCodesModule": "./src/generated/locale-codes.ts"
14+
},
15+
"interchange": {
16+
"format": "xliff",
17+
"xliffDir": "./xliff/"
18+
}
19+
}

packages/search-widget/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
"prettier": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path ./.gitignore --write"
2424
},
2525
"dependencies": {
26+
"@lit/localize": "^0.12.2",
2627
"lit": "^3.2.0",
2728
"lodash-es": "^4.17.21",
2829
"overlayscrollbars": "^2.10.0"
2930
},
3031
"devDependencies": {
3132
"@iconify/json": "^2.2.234",
3233
"@julr/unocss-preset-forms": "^0.1.0",
34+
"@lit/localize-tools": "^0.8.0",
3335
"@types/lodash-es": "^4.17.12",
3436
"@unocss/eslint-config": "^0.61.9",
3537
"@unocss/reset": "^0.61.9",
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,2 @@
11
export const HISTORY_KEY = 'halo:search-widgets:history:hits';
2-
export const MAX_HISTORY_ITEMS = 50;
3-
4-
export const SHORTCUT_HELP_LIST = [
5-
{
6-
text: '选择',
7-
kbdIcons: ['i-lucide-arrow-up', 'i-lucide-arrow-down'],
8-
},
9-
{
10-
text: '确认',
11-
kbdIcons: ['i-lucide-corner-down-left'],
12-
},
13-
{
14-
text: '关闭',
15-
kbdIcons: ['i-mdi-keyboard-esc'],
16-
},
17-
];
2+
export const MAX_HISTORY_ITEMS = 50;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Do not modify this file by hand!
2+
// Re-generate this file by running lit-localize.
3+
4+
/**
5+
* The locale code that templates in this source code are written in.
6+
*/
7+
export const sourceLocale = `en`;
8+
9+
/**
10+
* The other locale codes that this application is localized into. Sorted
11+
* lexicographically.
12+
*/
13+
export const targetLocales = [
14+
`es`,
15+
`zh-CN`,
16+
`zh-TW`,
17+
] as const;
18+
19+
/**
20+
* All valid project locale codes. Sorted lexicographically.
21+
*/
22+
export const allLocales = [
23+
`en`,
24+
`es`,
25+
`zh-CN`,
26+
`zh-TW`,
27+
] as const;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// Do not modify this file by hand!
3+
// Re-generate this file by running lit-localize
4+
5+
6+
7+
8+
/* eslint-disable no-irregular-whitespace */
9+
/* eslint-disable @typescript-eslint/no-explicit-any */
10+
11+
export const templates = {
12+
's5e8250fb85d64c23': `Cerrar`,
13+
'sa84142fc34654130': `Sin resultados de búsqueda`,
14+
'sad5f82f8364e5a1c': `Introducir palabras clave para buscar`,
15+
'sb4f1dffbb6be6302': `Borrar`,
16+
'sb85774dc5d18ff0f': `Confirmar`,
17+
'sbce70cbdb856635e': `Recientes`,
18+
'sfc63e31ac7c956ed': `Seleccionar`,
19+
};
20+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// Do not modify this file by hand!
3+
// Re-generate this file by running lit-localize
4+
5+
6+
7+
8+
/* eslint-disable no-irregular-whitespace */
9+
/* eslint-disable @typescript-eslint/no-explicit-any */
10+
11+
export const templates = {
12+
's5e8250fb85d64c23': `关闭`,
13+
'sa84142fc34654130': `没有搜索结果`,
14+
'sad5f82f8364e5a1c': `输入关键词以搜索`,
15+
'sb4f1dffbb6be6302': `清空`,
16+
'sb85774dc5d18ff0f': `确认`,
17+
'sbce70cbdb856635e': `最近搜索`,
18+
'sfc63e31ac7c956ed': `选择`,
19+
};
20+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// Do not modify this file by hand!
3+
// Re-generate this file by running lit-localize
4+
5+
6+
7+
8+
/* eslint-disable no-irregular-whitespace */
9+
/* eslint-disable @typescript-eslint/no-explicit-any */
10+
11+
export const templates = {
12+
's5e8250fb85d64c23': `關閉`,
13+
'sa84142fc34654130': `沒有搜尋結果`,
14+
'sad5f82f8364e5a1c': `輸入關鍵字以搜尋`,
15+
'sb4f1dffbb6be6302': `清空`,
16+
'sb85774dc5d18ff0f': `確認`,
17+
'sbce70cbdb856635e': `最近搜尋`,
18+
'sfc63e31ac7c956ed': `選擇`,
19+
};
20+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SearchForm } from './search-form';
22
import { SearchModal } from './search-modal';
3+
import './locale';
34

45
export { SearchForm, SearchModal };
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { configureLocalization, LocaleModule } from '@lit/localize';
2+
import * as templates_es from '../generated/locales/es';
3+
import * as templates_zh_CN from '../generated/locales/zh-CN';
4+
import * as templates_zh_TW from '../generated/locales/zh-TW';
5+
import { allLocales, sourceLocale, targetLocales } from '../generated/locale-codes';
6+
7+
const localizedTemplates = new Map([
8+
['es', templates_es],
9+
['zh-CN', templates_zh_CN],
10+
['zh-TW', templates_zh_TW],
11+
]);
12+
13+
const { setLocale } = configureLocalization({
14+
sourceLocale,
15+
targetLocales,
16+
loadLocale: async (locale) => localizedTemplates.get(locale) as LocaleModule,
17+
});
18+
19+
setLocale(getLocale());
20+
21+
function getLanguageFromCookie() {
22+
const match = document.cookie.match(new RegExp('(^| )language=([^;]+)'));
23+
const matchedLanguage = allLocales.find((locale) => locale === match?.[2]);
24+
return matchedLanguage;
25+
}
26+
27+
function getLanguageFromBrowser() {
28+
const language = allLocales.find((locale) => locale === navigator.language);
29+
return language;
30+
}
31+
32+
export function getLocale() {
33+
return getLanguageFromCookie() || getLanguageFromBrowser() || sourceLocale;
34+
}

0 commit comments

Comments
 (0)