projectfluent / fluent

Fluent — planning, spec and documentation
https://projectfluent.org
Apache License 2.0
1.4k stars 45 forks source link

How to define selectors based on the gender of a weekday? #289

Open jamesking opened 5 years ago

jamesking commented 5 years ago

In English we want to say something like on Wednesday (as in "See you on Wednesday!"), where wednesday is a generated by a datetime variable, $date. This is easy to do in Fluent:

on-weekday = 
    on {DATETIME($date, weekday: "long")}

In Portuguese, the preposition on is either no or na depending on the gender of the weekday. We are struggling to come up with a way of formatting this in Fluent. We were hoping something like this is possible.

on-weekday = 
    { DATETIME($date) ->
        [saturday] no
        [sunday] no
       *[other] na
    } {DATETIME($date, weekday: "long")}

Is something like this possible in Fluent?

Is there a way of doing this?

zbraniecki commented 5 years ago

This is one of the complex cases that we're only now slowly starting to play with (French has so many of them!).

One idea I can suggest is a builtin function that you can pass some string to and it computes a category for you.

Example:

on-weekday = 
    { WEEKDAY_GENDER(DATETIME($date, weekday: "long")) ->
        [masculine] no
        [feminine] na
       *[other] ne
    } {DATETIME($date, weekday: "long")}

Would that solve your case?

Pike commented 5 years ago

I'm pretty sure that you'll hit a declension problem, too. That is, the dateformat function will produce "Wednesday" in a grammatical form that's OK for standalone, but not in the relative clause you're expecting. Think "at Wednesdiya"

I'd suggest to do 7 strings to localize.

Only if we had dynamic term references and you did a lot of stuff around weekdays, it'd make sense to create terms for weekdays, and have them have gender and grammatical forms for languages in need. That's #80, but not triaged or scheduled.

jamesking commented 5 years ago

Thank you for the quick response!

@zbraniecki That function would certainly work for the particular instance we are trying to implement in Portuguese right now. Whether it scales to other use-cases is difficult to say.

@Pike #80 sounds really promising, and your suggestion to simply use 7 strings is appreciated. We might do that.

If we attempted this with a single string, however, this is the only that I can get working right now:

on-day = 
    { $weekday-number ->
   *[1] na {DATETIME($date, weekday: "long")}
    [6] no {DATETIME($date, weekday: "long")}
    [7] no {DATETIME($date, weekday: "long")}
    }

Where $weekday-number is a helper variable passed into Fluent. It is the weekday of $date represented as an integer between 1 and 7. Monday = 1, Tuesday = 2 etc.

Its a bit hacky, but it seems to work.

I tried to derive a weekday-number from $date directly, but Intl.DateTimeFormat does not seem to have an option for that, so we would have to calculate that outside of Fluent and pass it in.

I also tried:

on-day = 
    { DATETIME($date, weekday: "long") ->
   *[segunda-feira] na {DATETIME($date, weekday: "long")}
    [sábado] no {DATETIME($date, weekday: "long")}
    [domingo] no {DATETIME($date, weekday: "long")}
    }

This does not seem to work when I try it in the Playground:

zbraniecki commented 5 years ago

I would not recommend the last approach. Intl data is not meant to be stable or consistent across implementations. Writing such code indicates an expectation that the string returned from Intl.DateTimeFormat will remain the same in all implementations and over time which may not be the case.

jamesking commented 5 years ago

@zbraniecki Thank you for the heads-up

Pike commented 5 years ago

There's also the point that you can't enforce a localization to actually implement 7 weekdays in one string.

That's one of the corner stones of Fluent. Localizations are allowed to choose how complex their implementation of a string is. Like, one localization might choose to use "on Wednesdiya", the other might choose "soon".

You're applications intent OTH is to have a string per week day.

We wrote this down in https://github.com/projectfluent/fluent/wiki/Good-Practices-for-Developers#prefer-wet-over-dry, WET over DRY. Write Everything Twice (or seven times) over Don't Repeat Yourself.

jamesking commented 5 years ago

@Pike thanks for the link. Always good to be reminded of those best practices.

… the other might choose "soon".

This is actually what we have decided for now. Keep things simple and remove the reference to the specific weekday in Portuguese.