Manage Plural / gender in localization on flutter using json
Asked Answered
I

2

7

I'm developing a mobile application using flutter / dart. The app needs to be localized using JSON files.

I need to handle the plural / gender in strings. Actually i'm using sprintf library, with some placeholder in the JSON code like:

{
  ...
   "vehiclelabelKmTraveledReadable": "In %s Km",
  ...
}

so I replaced the '%s' placeholder with a variable value.

The problem is how to handle plurals, like

{
"vehiclePeriod": "Every %s month(s?)"
}

But if %s is 1, it's wrong. Does anybody knows how to manage this kind of cases?

Interpolate answered 21/1, 2020 at 11:10 Comment(0)
O
6

I managed this kind of case using intl package from flutter, but it uses .arb files instead of .json.

Here an example how to handle it with intl package

Intl.plural(howMany,
      zero: "$howMany An",
      one: "$howMany An",
      other: "$howMany Ans",
      name: 'dogYearsOld',
      locale: localeName,
      args: [howMany]);

Hope this can help you get started.

Orderly answered 21/1, 2020 at 17:5 Comment(1)
I just saw arb files, and intl library, but i was wondering if in the json file i can use some kind of functions or similar...Interpolate
C
1

I like to use this plugin for localization. It's really easy to use. And it supports many formats as well as JSON. Also it supports plurals. It's better to read docs, but small code snippet here

ru.json

{
  "day": {
    "zero":"{} дней",
    "one": "{} день",
    "two": "{} дня",
    "few": "{} дня",
    "many": "{} дней",
    "other": "{} дней"
  },
}

main.dart

print('day'.plural(21));
Calesta answered 4/11, 2023 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.