mirror of
https://github.com/typst/typst
synced 2025-05-24 14:05:27 +08:00
Hint for language-region pair on text.lang
(#4183)
This commit is contained in:
parent
794a215514
commit
485aa2e1ff
@ -29,13 +29,14 @@ pub use self::smartquote::*;
|
||||
pub use self::space::*;
|
||||
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
use std::str::FromStr;
|
||||
|
||||
use ecow::{eco_format, EcoString};
|
||||
use rustybuzz::{Feature, Tag};
|
||||
use smallvec::SmallVec;
|
||||
use ttf_parser::Rect;
|
||||
|
||||
use crate::diag::{bail, warning, SourceResult, StrResult};
|
||||
use crate::diag::{bail, warning, At, Hint, SourceResult, StrResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{
|
||||
cast, category, dict, elem, Args, Array, Cast, Category, Construct, Content, Dict,
|
||||
@ -393,6 +394,7 @@ pub struct TextElem {
|
||||
/// = Einleitung
|
||||
/// In diesem Dokument, ...
|
||||
/// ```
|
||||
#[parse(parse_lang(args)?)]
|
||||
#[default(Lang::ENGLISH)]
|
||||
#[ghost]
|
||||
pub lang: Lang,
|
||||
@ -1300,3 +1302,27 @@ cast! {
|
||||
ret
|
||||
},
|
||||
}
|
||||
|
||||
/// Function to parse the language argument.
|
||||
/// Provides a hint if a region is used in the language parameter.
|
||||
fn parse_lang(args: &mut Args) -> SourceResult<Option<Lang>> {
|
||||
let Some(Spanned { v: iso, span }) = args.named::<Spanned<EcoString>>("lang")? else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let result = Lang::from_str(&iso);
|
||||
if result.is_err() {
|
||||
if let Some((lang, region)) = iso.split_once('-') {
|
||||
if Lang::from_str(lang).is_ok() && Region::from_str(region).is_ok() {
|
||||
return result
|
||||
.hint(eco_format!(
|
||||
"you should leave only \"{}\" in the `lang` parameter and specify \"{}\" in the `region` parameter",
|
||||
lang, region,
|
||||
))
|
||||
.at(span)
|
||||
.map(Some);
|
||||
}
|
||||
}
|
||||
}
|
||||
result.at(span).map(Some)
|
||||
}
|
||||
|
@ -77,4 +77,9 @@
|
||||
#set text(lang: "qaa")
|
||||
#outline()
|
||||
#set text(lang: "qaa", region: "aa")
|
||||
#outline()
|
||||
#outline()
|
||||
|
||||
--- text-lang-hint-region-parameter ---
|
||||
// Error: 17-24 expected two or three letter language code (ISO 639-1/2/3)
|
||||
// Hint: 17-24 you should leave only "en" in the `lang` parameter and specify "gb" in the `region` parameter
|
||||
#set text(lang: "en-gb")
|
||||
|
Loading…
x
Reference in New Issue
Block a user