Hint for language-region pair on text.lang (#4183)

This commit is contained in:
Yip Coekjan 2024-05-25 23:31:04 +08:00 committed by GitHub
parent 794a215514
commit 485aa2e1ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 2 deletions

View File

@ -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)
}

View File

@ -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")