From e0d59304055619f6834109168dfb02f9a895f333 Mon Sep 17 00:00:00 2001 From: pan93412 Date: Thu, 27 Apr 2023 19:16:27 +0800 Subject: [PATCH] Add translation for Chinese (Traditional) (#1000) --- library/src/math/mod.rs | 4 +++- library/src/meta/bibliography.rs | 4 +++- library/src/meta/heading.rs | 4 +++- library/src/meta/numbering.rs | 28 ++++++++++++++++++++-------- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 47c28d179..21e5d8059 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -34,6 +34,7 @@ use ttf_parser::{GlyphId, Rect}; use typst::eval::{Module, Scope}; use typst::font::{Font, FontWeight}; use typst::model::Guard; +use typst::util::option_eq; use unicode_math_class::MathClass; use self::ctx::*; @@ -277,10 +278,11 @@ impl Count for EquationElem { } impl LocalName for EquationElem { - fn local_name(&self, lang: Lang, _: Option) -> &'static str { + fn local_name(&self, lang: Lang, region: Option) -> &'static str { match lang { Lang::ARABIC => "معادلة", Lang::BOKMÅL => "Ligning", + Lang::CHINESE if option_eq(region, "TW") => "方程式", Lang::CHINESE => "等式", Lang::CZECH => "Rovnice", Lang::FRENCH => "Équation", diff --git a/library/src/meta/bibliography.rs b/library/src/meta/bibliography.rs index 99f2111fe..1e922c42a 100644 --- a/library/src/meta/bibliography.rs +++ b/library/src/meta/bibliography.rs @@ -7,6 +7,7 @@ use ecow::{eco_vec, EcoVec}; use hayagriva::io::{BibLaTeXError, YamlBibliographyError}; use hayagriva::style::{self, Brackets, Citation, Database, DisplayString, Formatting}; use hayagriva::Entry; +use typst::util::option_eq; use super::{LinkElem, LocalName, RefElem}; use crate::layout::{BlockElem, GridElem, ParElem, Sizing, TrackSizings, VElem}; @@ -210,10 +211,11 @@ impl Finalize for BibliographyElem { } impl LocalName for BibliographyElem { - fn local_name(&self, lang: Lang, _: Option) -> &'static str { + fn local_name(&self, lang: Lang, region: Option) -> &'static str { match lang { Lang::ARABIC => "المراجع", Lang::BOKMÅL => "Bibliografi", + Lang::CHINESE if option_eq(region, "TW") => "書目", Lang::CHINESE => "参考文献", Lang::CZECH => "Bibliografie", Lang::FRENCH => "Bibliographie", diff --git a/library/src/meta/heading.rs b/library/src/meta/heading.rs index 43505448a..01804edc3 100644 --- a/library/src/meta/heading.rs +++ b/library/src/meta/heading.rs @@ -1,4 +1,5 @@ use typst::font::FontWeight; +use typst::util::option_eq; use super::{Counter, CounterUpdate, LocalName, Numbering, Refable}; use crate::layout::{BlockElem, HElem, VElem}; @@ -234,10 +235,11 @@ impl Refable for HeadingElem { } impl LocalName for HeadingElem { - fn local_name(&self, lang: Lang, _: Option) -> &'static str { + fn local_name(&self, lang: Lang, region: Option) -> &'static str { match lang { Lang::ARABIC => "الفصل", Lang::BOKMÅL => "Kapittel", + Lang::CHINESE if option_eq(region, "TW") => "小節", Lang::CHINESE => "小节", Lang::CZECH => "Kapitola", Lang::FRENCH => "Chapitre", diff --git a/library/src/meta/numbering.rs b/library/src/meta/numbering.rs index b5416f689..dc3ae9878 100644 --- a/library/src/meta/numbering.rs +++ b/library/src/meta/numbering.rs @@ -259,7 +259,14 @@ enum NumberingKind { Roman, Symbol, Hebrew, - Chinese, + SimplifiedChinese, + // TODO: Pick the numbering pattern based on languages choice. + // As the `1st` numbering character of Chinese (Simplifed) and + // Chinese (Traditional) is same, we are unable to determine + // if the context is Simplified or Traditional by only this + // character. + #[allow(unused)] + TraditionalChinese, HiraganaIroha, KatakanaIroha, } @@ -273,7 +280,7 @@ impl NumberingKind { 'i' => NumberingKind::Roman, '*' => NumberingKind::Symbol, 'א' => NumberingKind::Hebrew, - '一' | '壹' => NumberingKind::Chinese, + '一' | '壹' => NumberingKind::SimplifiedChinese, 'い' => NumberingKind::HiraganaIroha, 'イ' => NumberingKind::KatakanaIroha, _ => return None, @@ -288,7 +295,8 @@ impl NumberingKind { Self::Roman => 'i', Self::Symbol => '*', Self::Hebrew => 'א', - Self::Chinese => '一', + Self::SimplifiedChinese => '一', + Self::TraditionalChinese => '一', Self::HiraganaIroha => 'い', Self::KatakanaIroha => 'イ', } @@ -437,18 +445,22 @@ impl NumberingKind { } fmt } - Self::Chinese => { - let chinesecase = match case { + l @ (Self::SimplifiedChinese | Self::TraditionalChinese) => { + let chinese_case = match case { Case::Lower => ChineseCase::Lower, Case::Upper => ChineseCase::Upper, }; match (n as u8).to_chinese( - ChineseVariant::Simple, - chinesecase, + match l { + Self::SimplifiedChinese => ChineseVariant::Simple, + Self::TraditionalChinese => ChineseVariant::Traditional, + _ => unreachable!(), + }, + chinese_case, ChineseCountMethod::TenThousand, ) { - Ok(chinesestring) => EcoString::from(chinesestring), + Ok(num_str) => EcoString::from(num_str), Err(_) => '-'.into(), } }