From dfee374df9abffe92e2ab603dbd6cbee724656f8 Mon Sep 17 00:00:00 2001 From: Sakura Knoll Date: Sat, 8 Jan 2022 21:52:39 +0900 Subject: [PATCH] Translate elf pages --- public/locales/en-US/common.json | 12 +++++++- public/locales/ko-KR/common.json | 12 +++++++- src/components/molecules/ElfCard.tsx | 11 +++++-- src/lib/honkai3rd/elfs.ts | 3 ++ src/pages/honkai3rd/elfs/[elfId].tsx | 29 ++++++++++++------ src/pages/honkai3rd/elfs/index.tsx | 13 ++++---- src/server/data/honkai3rd/elfs.ts | 45 +++++++++++++++++++++++++++- 7 files changed, 105 insertions(+), 20 deletions(-) diff --git a/public/locales/en-US/common.json b/public/locales/en-US/common.json index e0bf89ae..ced61f86 100644 --- a/public/locales/en-US/common.json +++ b/public/locales/en-US/common.json @@ -11,7 +11,8 @@ "honkai-3rd": "Honkai 3rd", "battlesuits": "Battlesuits", "stigmata": "Stigmata", - "weapons": "Weapons" + "weapons": "Weapons", + "elfs": "ELFs" }, "battlesuits-list": { "heading": "Battlesuits", @@ -50,5 +51,14 @@ "lance": "Lance", "gauntlet": "Gauntlet", "bow": "Bow" + }, + "elfs-list": { + "elfs": "ELFs" + }, + "elfs-show": { + "passive": "Passive", + "basic": "Basic", + "ultimate": "Ultimate", + "team": "Team" } } diff --git a/public/locales/ko-KR/common.json b/public/locales/ko-KR/common.json index ee35d714..8c0d2064 100644 --- a/public/locales/ko-KR/common.json +++ b/public/locales/ko-KR/common.json @@ -14,7 +14,8 @@ "top": "(상)", "mid": "(중)", "bot": "(하)", - "weapons": "무기" + "weapons": "무기", + "elfs": "무장인형" }, "battlesuits-list": { "heading": "발키리", @@ -53,5 +54,14 @@ "lance": "랜스", "gauntlet": "건틀릿", "bow": "활" + }, + "elfs-list": { + "elfs": "무장인형" + }, + "elfs-show": { + "passive": "패시브", + "basic": "기본 공격", + "ultimate": "필살기", + "team": "팀" } } diff --git a/src/components/molecules/ElfCard.tsx b/src/components/molecules/ElfCard.tsx index 4b4f9fcd..ab97ce62 100644 --- a/src/components/molecules/ElfCard.tsx +++ b/src/components/molecules/ElfCard.tsx @@ -1,16 +1,21 @@ /** @jsxImportSource theme-ui */ import { Box, Card, Text } from '@theme-ui/components' +import { useRouter } from 'next/router' import { ElfData } from '../../lib/honkai3rd/elfs' +import { translate } from '../../lib/i18n' import PageLink from '../atoms/PageLink' import SquareImageBox from '../atoms/SquareImageBox' interface ElfCardProps { - elf: Pick + elf: Pick hidden?: boolean } const ElfCard = ({ elf }: ElfCardProps) => { + const { locale } = useRouter() + const elfName = translate(locale, { 'ko-KR': elf.krName }, elf.name) + return ( { > @@ -37,7 +42,7 @@ const ElfCard = ({ elf }: ElfCardProps) => { textAlign: 'center', }} > - {elf.name} + {elfName} diff --git a/src/lib/honkai3rd/elfs.ts b/src/lib/honkai3rd/elfs.ts index 817665f3..e997c321 100644 --- a/src/lib/honkai3rd/elfs.ts +++ b/src/lib/honkai3rd/elfs.ts @@ -1,13 +1,16 @@ export interface ElfSkill { type: 'passive' | 'basic' | 'ultimate' | 'team' name: string + krName?: string requiredRank: number description: string + krDescription?: string } export interface ElfData { id: string name: string + krName?: string baseRank: number features: string[] skillRows: [ElfSkill[], ElfSkill[], ElfSkill[], ElfSkill[]] diff --git a/src/pages/honkai3rd/elfs/[elfId].tsx b/src/pages/honkai3rd/elfs/[elfId].tsx index dae8298b..3d3b2175 100644 --- a/src/pages/honkai3rd/elfs/[elfId].tsx +++ b/src/pages/honkai3rd/elfs/[elfId].tsx @@ -8,15 +8,20 @@ import SecondaryLabel from '../../../components/atoms/SecondaryLabel' import Breadcrumb from '../../../components/organisms/Breadcrumb' import Honkai3rdNavigator from '../../../components/organisms/Honkai3rdNavigator' import { ElfData } from '../../../lib/honkai3rd/elfs' -import { capitalize } from '../../../lib/string' import { getElfById, listElfs } from '../../../server/data/honkai3rd/elfs' import { generateI18NPaths, getI18NProps } from '../../../server/i18n' - +import { translate, useTranslation } from '../../../lib/i18n' +import { useRouter } from 'next/router' interface ElfShowPageProps { elf: ElfData } const ElfShowPage = ({ elf }: ElfShowPageProps) => { + const { locale } = useRouter() + const { t } = useTranslation() + + const elfName = translate(locale, { 'ko-KR': elf.krName }, elf.name) + return ( @@ -24,16 +29,16 @@ const ElfShowPage = ({ elf }: ElfShowPageProps) => { - {elf.name} + {elfName} { as={columnIndex === 0 ? 'h2' : 'h3'} sx={{ mb: 1 }} > - {item.name} + {translate(locale, { 'ko-KR': item.krName }, item.name)} - {capitalize(item.type)} + + {t(`elfs-show.${item.type}`)} + { '&:last-child': { borderBottom: 'none' }, }} > - {item.description} + {translate( + locale, + { 'ko-KR': item.krDescription }, + item.description + )} ) diff --git a/src/pages/honkai3rd/elfs/index.tsx b/src/pages/honkai3rd/elfs/index.tsx index 0adbbaa8..c52ab08e 100644 --- a/src/pages/honkai3rd/elfs/index.tsx +++ b/src/pages/honkai3rd/elfs/index.tsx @@ -8,12 +8,15 @@ import Honkai3rdNavigator from '../../../components/organisms/Honkai3rdNavigator import { ElfData } from '../../../lib/honkai3rd/elfs' import { getI18NProps } from '../../../server/i18n' import { listElfs } from '../../../server/data/honkai3rd/elfs' +import { useTranslation } from '../../../lib/i18n' interface ElfListPageProps { - elfs: Pick[] + elfs: Pick[] } const ElfListPage = ({ elfs }: ElfListPageProps) => { + const { t } = useTranslation() + return ( @@ -21,12 +24,12 @@ const ElfListPage = ({ elfs }: ElfListPageProps) => { - ELFs + {t('elfs-list.elfs')} pick(['id', 'name'], elf)), + elfs: listElfs().map((elf) => pick(['id', 'name', 'krName'], elf)), ...(await getI18NProps(locale)), }, } diff --git a/src/server/data/honkai3rd/elfs.ts b/src/server/data/honkai3rd/elfs.ts index fd41accc..7a5dd666 100644 --- a/src/server/data/honkai3rd/elfs.ts +++ b/src/server/data/honkai3rd/elfs.ts @@ -1,4 +1,4 @@ -import { readdirSync, readJsonFileSync } from '../fs' +import { readdirSync, readFileSync, readJsonFileSync } from '../fs' import { compareVersion } from '../../../lib/string' import { ElfData } from '../../../lib/honkai3rd/elfs' @@ -8,6 +8,28 @@ const elfDataList = elfFileNameList const filePathname = 'honkai3rd/elfs/' + fileName const data = readJsonFileSync(filePathname) as ElfData + const krDataFilePath = `honkai3rd/ko-KR/elfs/${data.id}.md` + try { + const krData = parseElfData(readFileSync(krDataFilePath).toString()) + data.krName = krData.name + data.skillRows.forEach((skillRow, skillRowIndex) => { + skillRow.forEach((skill, skillIndex) => { + if (krData.skillRows?.[skillRowIndex]?.[skillIndex]?.name != null) { + skill.krName = krData.skillRows?.[skillRowIndex]?.[skillIndex]?.name + } + if ( + krData.skillRows?.[skillRowIndex]?.[skillIndex]?.description != null + ) { + skill.krDescription = + krData.skillRows?.[skillRowIndex]?.[skillIndex]?.description + } + }) + }) + } catch (error) { + // console.warn('Failed to read', krDataFilePath) + // console.warn(error) + } + return data }) .sort((a, b) => { @@ -37,3 +59,24 @@ export function listElfs() { export function getElfById(id: string) { return elfMap.get(id) } + +function parseElfData(rawData: string) { + const [name, ...skillRowSections] = rawData.split('\n## ') + const skillRows = skillRowSections.map((skillRowSection) => { + const skillSections = skillRowSection.split('\n### ') + return skillSections.map((skillSection) => { + const [name, description] = skillSection + .split('\n\n') + .map((data) => data.replace(/#+\s/, '').trim()) + return { + name, + description, + } + }) + }) + + return { + name: name.replace(/#+\s/, '').trim(), + skillRows, + } +}