fluent-rs icon indicating copy to clipboard operation
fluent-rs copied to clipboard

Ability to parametrise arguments

Open juliancoffee opened this issue 2 years ago • 6 comments

Blocked on https://github.com/projectfluent/fluent/issues/80


As far as I know in Fluent we can't parametrise arguments. In a sense that this code will be impossible to parse.

skeleton = {$case ->
  [genitive] Skeleton
  [possessive] by Skeleton
}
cultist = {$case ->
  [genitive] Cultist
  [possessive] by Cultist
}
shoot = { $victim } has be shoot { $attacker(case: "possessive") }

This is obviously not a problem for English, but it is a problem for other languages with grammatical cases, for example, Slavic ones. (I translated the bear was killed by another bear to Polish and got niedźwiedź został również zabity przez innego niedźwiedzia. You can see the difference between niedźwiedź and niedźwiedzia)

~~So to solve it, I was thinking about adding EVAL function, which would take its first parameter as a term and pass args to it.~~ ~~shoot = { $victim } has been shoot { EVAL($attacker, case: "possessive") }~~

~~Doing it externally would require allocating the bundle on the heap so we can capture its address in closure. But the reference to the bundle is always here when we call function, as all functions are stored in the bundle, so this may be some built-in.~~

juliancoffee avatar Aug 06 '22 12:08 juliancoffee

So, I was trying to create a function for EVAL but lifetimes were stronger than me :) But I was able to add another branch to ResolveValue impl (and WriteValue) and it seems to work.

            Self::FunctionReference {
                id: ast::Identifier { name: "EVAL", ..},
                arguments,
            } => {
                let (resolved_positional_args, resolved_named_args) =
                    scope.get_arguments(Some(arguments));

                let key = if resolved_positional_args.len() == 1 {
                    match &resolved_positional_args[0] {
                        FluentValue::String(s) => s,
                        _ => return FluentValue::Error,
                    }
                } else {
                    return FluentValue::Error
                };

                let msg = if let Some(msg) = scope.bundle.get_message(&key) {
                    msg
                } else {
                    return FluentValue::Error
                };

                let pattern = if let Some(pat) = msg.value() {
                    pat
                } else {
                    return FluentValue::Error
                };

                FluentValue::String(scope.bundle.format_pattern(
                    pattern,
                    Some(&resolved_named_args),
                    &mut Vec::new()
                ).into_owned().into())
            }

juliancoffee avatar Aug 06 '22 22:08 juliancoffee

@zbraniecki how feasible would it be to get something like that?

juliancoffee avatar Aug 06 '22 22:08 juliancoffee

Oh, I just found this proposal which is basically does the same thing https://github.com/projectfluent/fluent/issues/80

juliancoffee avatar Aug 06 '22 23:08 juliancoffee

Yep, this is being now added to MessageFormat 2.0 (successor of Fluent) and may be backported to Fluent - see https://github.com/projectfluent/fluent/issues/349

What I'd be against is adding a Rust only extension to Fluent feature/syntax. If you want it in Fluent, please suggest it in https://github.com/projectfluent/fluent repo and let @eemeli decide (he's the maintainer of the spec now).

zbraniecki avatar Aug 07 '22 11:08 zbraniecki

Makes sense, yes. Keeping this open, because in theory this problem is still not solved.

juliancoffee avatar Aug 07 '22 13:08 juliancoffee

(Also I think that using an idea from projectfluent/fluent#80 is better than ad-hoc function, so renamed this issue)

juliancoffee avatar Aug 07 '22 13:08 juliancoffee