feat: i18n, page title

(cherry picked from commit 0d9fc72bee009c591400055725680a6a0f5a9c83)
This commit is contained in:
saicaca
2023-10-06 02:53:47 +08:00
parent 52959f230f
commit 2c4cc28e1f
19 changed files with 209 additions and 19 deletions

26
src/i18n/i18nKey.ts Normal file
View File

@@ -0,0 +1,26 @@
enum I18nKey {
home = "home",
about = "about",
archive = "archive",
tags = "tags",
categories = "categories",
recentPosts = "recentPosts",
comments = "comments",
untitled = "untitled",
uncategorized = "uncategorized",
noTags = "noTags",
wordCount = "wordCount",
wordsCount = "wordsCount",
minuteCount = "minuteCount",
minutesCount = "minutesCount",
postCount = "postCount",
postsCount = "postsCount",
primaryColor = "primaryColor",
}
export default I18nKey;

27
src/i18n/languages/en.ts Normal file
View File

@@ -0,0 +1,27 @@
import type { Translation } from "../translation.ts";
import Key from "../i18nKey.ts";
export const en: Translation = {
[Key.home]: "Home",
[Key.about]: "About",
[Key.archive]: "Archive",
[Key.tags]: "Tags",
[Key.categories]: "Categories",
[Key.recentPosts]: "Recent Posts",
[Key.comments]: "Comments",
[Key.untitled]: "Untitled",
[Key.uncategorized]: "Uncategorized",
[Key.noTags]: "No Tags",
[Key.wordCount]: "word",
[Key.wordsCount]: "words",
[Key.minuteCount]: "minute",
[Key.minutesCount]: "minutes",
[Key.postCount]: "post",
[Key.postsCount]: "posts",
[Key.primaryColor]: "Primary Color",
};

View File

@@ -0,0 +1,27 @@
import type { Translation } from "../translation.ts";
import Key from "../i18nKey.ts";
export const zh_CN: Translation = {
[Key.home]: "主页",
[Key.about]: "关于",
[Key.archive]: "归档",
[Key.tags]: "标签",
[Key.categories]: "分类",
[Key.recentPosts]: "最新文章",
[Key.comments]: "评论",
[Key.untitled]: "无标题",
[Key.uncategorized]: "未分类",
[Key.noTags]: "无标签",
[Key.wordCount]: "字",
[Key.wordsCount]: "字",
[Key.minuteCount]: "分钟",
[Key.minutesCount]: "分钟",
[Key.postCount]: "篇文章",
[Key.postsCount]: "篇文章",
[Key.primaryColor]: "主题色",
};

View File

@@ -0,0 +1,27 @@
import type { Translation } from "../translation.ts";
import Key from "../i18nKey.ts";
export const zh_TW: Translation = {
[Key.home]: "首頁",
[Key.about]: "關於",
[Key.archive]: "彙整",
[Key.tags]: "標籤",
[Key.categories]: "分類",
[Key.recentPosts]: "最新文章",
[Key.comments]: "評論",
[Key.untitled]: "無標題",
[Key.uncategorized]: "未分類",
[Key.noTags]: "無標籤",
[Key.wordCount]: "字",
[Key.wordsCount]: "字",
[Key.minuteCount]: "分鐘",
[Key.minutesCount]: "分鐘",
[Key.postCount]: "篇文章",
[Key.postsCount]: "篇文章",
[Key.primaryColor]: "主題色",
};

30
src/i18n/translation.ts Normal file
View File

@@ -0,0 +1,30 @@
import {en} from "./languages/en.ts";
import {zh_TW} from "./languages/zh_TW.ts";
import {zh_CN} from "./languages/zh_CN.ts";
import type I18nKey from "./i18nKey.ts";
import {getConfig} from "../utils/config-utils.ts";
export type Translation = {
[K in I18nKey]: string;
}
const defaultTranslation = en;
const map: { [key: string]: Translation } = {
"en": en,
"en_us": en,
"en_gb": en,
"en_au": en,
"zh_cn": zh_CN,
"zh_tw": zh_TW,
}
export function getTranslation(lang: string): Translation {
lang = lang.toLowerCase();
return map[lang] || defaultTranslation;
}
export function i18n(key: I18nKey): string {
const lang = getConfig().lang || "en";
return getTranslation(lang)[key];
}