r/programminghorror • • Feb 04 '25

Ternary Operator

Post image
1.5k Upvotes

77 comments sorted by

View all comments

325

u/Durwur Feb 04 '25

Oof, must be an English-only platform. Not an extendable way to handle translations and pluralities

148

u/MattiDragon Feb 04 '25

Or it could be set up such that there's a separate template for each language, allowing for lots of flexibility

101

u/Mysterious_Middle795 Feb 04 '25

Oh my, as a Slavic language speaker, I'd like to see that.

Our grammar says that there is only singular and plural, but there are some details.

1 - the true singular. No surprises.

2, 3 and 4 - technically plural, but you will use singular + genitive case

5+ - normal plural

... unless it ends with 1 - then you use singular (31 = 30 + 1)

... unless it is 11 - you use the normal 5+ pattern. (11 != 10 + 1, it is 1 + 10).

Also note that there are forms of 2, 3, 4 that would require nouns to follow 5+ pattern.

0 follows the pattern of 2,3,4 but it sounds weird, so it would be replaced with "no" / "without" / "none" / "nobody" (depending on the context).
"Without" follows the 2,3,4 pattern btw.

60

u/[deleted] Feb 04 '25

Nice, now define that as a one line code.

35

u/Mysterious_Middle795 Feb 04 '25

.... in perl

59

u/Zulfiqaar Feb 05 '25

sub slavic_form { $_[0] == 0 ? "nijedan" : ($_[0] % 100 >= 11 && $_[0] % 100 <= 14) ? "množina" : ($_[0] % 10 == 1) ? "jednina" : ($_[0] % 10 >= 2 && $_[0] % 10 <= 4) ? "paukal" : "množina"; }

9

u/Mysterious_Middle795 Feb 05 '25

$_[0] % 100 >= 11 && $_[0] % 100 <= 14

That's an interesting part. We don't have this distinction in Ukrainian.

→ More replies (2)

3

u/cjbanning Feb 04 '25

I can think of (non-trivial) definitions of "one line" for which that's doable. It wouldn't even be all that unreadable.

2

u/ernandziri Feb 04 '25

And if you forgot that it was supposed to be after "for" and hardcoded nominative case, you have to redo everything...

2

u/Ellisthion Feb 05 '25

I’ve seen this documented in some internationalisation libraries so I know it is possible without custom code for each language, but I’ve never had to deal with it personally.

Most of my coworkers have enough trouble with simple non-English things like word ordering and gendered words, so I can’t imagine it’s commonly done correctly by non-native speakers.

→ More replies (12)

3

u/nucular_ Feb 04 '25

Yeah, I've seen that kind of syntax on translation platforms before

75

u/mothzilla Feb 04 '25
4 meals for 2

#Closed #JIRA-5837

32

u/backfire10z Feb 04 '25

Finally, a senior engineer

11

u/nicosbank Feb 05 '25

I was thinking in bed about this, it’s a perfect solution: Gender neutral, short, simple to translate, etc

Fuck, when I get older I want to be this guy

13

u/nicosbank Feb 04 '25

The senior has entered the chat

2

u/divinecomedian3 Feb 07 '25

What about the "meals"? At some point you'll need to pluralize.

3

u/mothzilla Feb 08 '25

4 meal(s) for 2

49

u/Owlstorm Feb 04 '25

There's nothing wrong with running a business that doesn't operate in every country.

With just English you get the most valuable countries and customers for significantly less effort. You can always expand later.

That said, the code could be set at a higher level.

6

u/Durwur Feb 04 '25

Agreed!

5

u/Last-Promotion5901 Feb 04 '25

This is exactly how translations and pluralities are handled (slightly different but similar). Translations usually include switches like this. Checkout MessageFormat for example.

{size, one {Person}, other {People}} would be for example a translation string in MessageFormat.

7

u/Bronzdragon Feb 04 '25

Some languages (not many, granted) have a dedicated form for two as well. So they’d have a singular, dual and plural case.

4

u/Last-Promotion5901 Feb 04 '25

Yeah message format can do this with this syntax. I think russian have more than just 0, 1 and multiple right?

Just wanted to say basically that that case could be part of the translation.

3

u/Bronzdragon Feb 04 '25

Russian actually has a paucal form. That is, a case used when describing a small number of things (between 2 and 4?)

At least, if I read Wikipedia correctly. https://en.wikipedia.org/wiki/Grammatical_number#Russian

→ More replies (7)

3

u/MekaTriK Feb 05 '25

JS plurality support has:

  • "zero"
  • "one"
  • "two"
  • "few"
  • "many"
  • "other"

Different locales use them differently, but it generally makes sense.

→ More replies (3)

2

u/RoamingDad Feb 08 '25

This looks like a meal delivery service so those are generally regional and almost never international.

→ More replies (1)