diff --git a/src/lib/i18n/en.ts b/src/lib/i18n/en.ts new file mode 100644 index 0000000..e5a6807 --- /dev/null +++ b/src/lib/i18n/en.ts @@ -0,0 +1,17 @@ + +export default { + home: { + titles: { + continue: 'Continue from pause', + history: 'History', + myPlaylists: 'My Playlists', + recPlaylists: 'Recommended Playlists' + }, + btn: { + importFromNetease: 'Import from NetEase' + }, + text: { + playlistCreatedBy: 'From {u}' + } + } +} \ No newline at end of file diff --git a/src/lib/i18n/index.ts b/src/lib/i18n/index.ts new file mode 100644 index 0000000..f11e722 --- /dev/null +++ b/src/lib/i18n/index.ts @@ -0,0 +1,62 @@ +import EN from "./en" +import ZH from "./zh" + +type Lang = 'en' | 'zh' + +const msgs: Record = { + en: EN, + zh: ZH +} + +let lang: Lang = 'en' + +// Infer language from browser +if (navigator.language.startsWith('zh')) { + lang = 'zh' +} + +export const getI18n = () => msgs[lang] + +// export function ts(key: string, variables?: { [index: string]: any }) { +// return t(key as keyof LocalizedMessages, variables) +// } + +// /** +// * Load the translation for the given key +// * +// * @param key +// * @param variables +// */ +// export function t(key: keyof LocalizedMessages, variables?: { [index: string]: any }) { +// // Check if the key exists +// let msg = getI18n()[key] +// if (!msg) { +// // Check if the key exists in English +// if (!(msg = getI18n()[key])) { +// msg = key +// console.error(`ERROR!! Missing translation reference entry (English) for ${key}`) +// } +// else console.warn(`Missing translation for ${key} in ${lang}`) +// } +// // Replace variables +// if (variables) { +// return msg.replace(/\${(.*?)}/g, (_: string, v: string | number) => variables[v] + "") +// } +// return msg +// } +// Object.assign(window, { t }) + + +export {} + +declare global { + interface String { + sed(variables: { [index: string]: any }): string + } +} + +String.prototype.sed = function (variables: { [index: string]: any }) { + return this.replace(/\${(.*?)}/g, (_: string, v: string | number) => variables[v] + "") +} + +Object.defineProperty(String.prototype, 'sed', { enumerable: false }) diff --git a/src/lib/i18n/zh.ts b/src/lib/i18n/zh.ts new file mode 100644 index 0000000..261c042 --- /dev/null +++ b/src/lib/i18n/zh.ts @@ -0,0 +1,17 @@ + +export default { + home: { + titles: { + continue: '从暂停的位置继续', + history: '历史数据', + myPlaylists: '我的歌单', + recPlaylists: '推荐歌单' + }, + btn: { + importFromNetease: '从网易云导入' + }, + text: { + playlistCreatedBy: '{u} 创建' + } + } +} \ No newline at end of file diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 482cbca..2e47312 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,14 +1,17 @@