This means basic match
statements can now be generated from fluent source code, however it requires a dependency on icu_{locid, plurals}
, which is not ideal. Selectors such as numbers and ordinal CLDR plurals are not yet supported
UOMQT7LTURIIWHZT2ZHLCJG6XESYTN26EJC7IHRFR4PYJ355PNYAC
Expression::Select { selector, variants } => match_inline_expression(selector),
Expression::Select { selector, variants } => {
let match_target = match_inline_expression(selector);
let match_arms: Vec<syn::Arm> = variants.iter().map(match_variant).collect();
parse_quote! {
{
// FIXME: this is a hack and should be cleaned up later
use icu_locid::locale;
use icu_plurals::{PluralCategory, PluralRuleType, PluralRules};
let plural_rules = PluralRules::try_new(&locale!("en").into(), PluralRuleType::Cardinal)
.expect("locale should be present");
match plural_rules.category_for(#match_target) {
#(#match_arms),*
}
}
}
}
_ => todo!(),
InlineExpression::VariableReference { id } => {
let ident = format_ident!("{}", id.name);
parse_quote!(#ident)
}
_ => {
dbg!(expression);
todo!()
}
}
}
fn match_variant<'resource>(variant: &Variant<&'resource str>) -> syn::Arm {
let base_pattern = match_pattern(&variant.key);
let body = match_fluent_pattern(&variant.value);
// Default patterns match anything else
// TODO: this can potentially generate unreachable patterns,
// should be replaced with a more sophisticated implementation
let pattern = if variant.default {
parse_quote!(#base_pattern | _)
} else {
base_pattern
};
parse_quote!(#pattern => #body)
}
fn match_pattern<'resource>(pattern: &VariantKey<&'resource str>) -> syn::Pat {
match pattern {
VariantKey::Identifier { name } => {
let ident = format_ident!("{}", name.to_upper_camel_case());
parse_quote!(PluralCategory::#ident)
}
VariantKey::NumberLiteral { value } => todo!(),
let ftl = r#"test-message = the number is { 2 }! what a { "cool" } number :)"#;
// Example from https://projectfluent.org/fluent/guide/selectors.html
let ftl = r#"emails =
{ $unreadEmails ->
[one] You have one unread email.
*[other] You have { $unreadEmails } unread emails.
}"#;