advplyr / audiobookshelf

Self-hosted audiobook and podcast server
https://audiobookshelf.org
GNU General Public License v3.0
6.55k stars 465 forks source link

[Enhancement]: Proper plurals support for localisation #3540

Open biuklija opened 1 day ago

biuklija commented 1 day ago

Type of Enhancement

Web Interface/Frontend

Describe the Feature/Enhancement

Currently, some of the strings and labels for localisation are being reused in multiple locations without proper plurals support. While this works for English, problems arise in many inflected languages. Weblate has full support for these plural forms, so my suggestion is to separate labels/strings into "fixed" strings (meaning those that don't need plurals support) and "plural" strings require alternative translations in places where the translation depends on the number of items.

Why would this be helpful?

To take Croatian as an example, the string LabelBooks, if translated in the nominative case in Croatian (in plural) as "Knjige", would serve fine in the search results/search suggestions, e.g. here: vivaldi_oLWveEENnL However, the same word would be incorrect as a caption for the number of books available in a single library, where the translation is supposed to vary depending on the actual number of books. vivaldi_6cxm860taj Please note that in the above example with plurals the translation "knjiga/e" is my attempt to offer a correct translation for plurals of this word but some words have even more variants.

Future Implementation (Screenshot)

As per Weblate's documentation, we should have plural-supported strings. For example, the correct translation for the word "Book" in Croatian should be: 0, 1 knjiga 2, 3, 4 knjige 5-20 knjiga 21, 31... knjiga 22, 23, 24; 32, 33, 34... - knjige 25-30; 35-40... - knjiga

Audiobookshelf Server Version

v2.15.1

Current Implementation (Screenshot)

No response

nichwall commented 1 day ago

Would we need separate labels in the translation file for each combination of plurals in the code? Would we need to add language specific pluralization handling everywhere? I see we can add that in Weblate easily, but not sure to handle that nicely in the code without having a lot more translation targets.

Right now, we have a function which just substitutes values into the string called getString. An example of that is here for the sleep timer:

https://github.com/advplyr/audiobookshelf/blob/72e59e77a7e4b4d029cef3dd11662c58ba1d60a5/client/components/modals/SleepTimerModal.vue#L71-L112

and the string label/value is defined here: https://github.com/advplyr/audiobookshelf/blob/72e59e77a7e4b4d029cef3dd11662c58ba1d60a5/client/strings/en-us.json#L622-L624

biuklija commented 1 day ago

If I understand this correctly, we're not using a multi-language translation library in this project (one which would automatically handle translations i.e. export and update a list of strings for translation and, presumably, provide support for plurals) and just doing string substitution?

I can see why adding project-wide plurals support might be a tall order, especially if done from scratch. I'm not sure how this is usually handled in other projects.

In any case, if reworking the entire localisation support is too much work and if we want to avoid creating too many new translation targets, separating strings which follow or precede a number could be an acceptable alternative. For instance, we can have a fixed string Books that will always be Knjige (for the homepage links) and a {0} books string which I could translate as {0} knjiga/knjige, shorten as {0} knjiga/e or just do knjiga: {0} which would also work. There shouldn't be too many of these cases.

advplyr commented 1 day ago

We can write a function that takes an optional number parameter and returns the correct plural form but I'm not seeing any examples in weblate how this is formatted

biuklija commented 21 hours ago

Weblate's documentation provides a list of file formats that support plurals along with format examples, but it seems that basic json files of the "key":"translation" type are not supported. To quote their documentation:

Pluralizing strings need proper support from the application framework as well. Choose native format of your platform such as GNU gettext PO (Portable Object), Android string resources or Stringsdict format.