PocketByte / LocoLaser

Localization tool to import localized strings from external source to your project.
Apache License 2.0
31 stars 1 forks source link

Plurals support with web formatting #18

Closed avently closed 3 years ago

avently commented 3 years ago

Hello. Yes, I know, when you adapted web formatting you said that it doesn't have plurals. I looked at the code and don't think it's impossible to adapt the plurals anyways. Why not just try to parse parameter and check it's class based on parsed result?

Here can be made an exception for web formatting parameters: https://github.com/PocketByte/LocoLaser/blob/cc50e39232a2200e92cab49fbca38b36a5a5616f/resource-kotlin-mpp/src/main/java/ru/pocketbyte/locolaser/kotlinmpp/resource/file/BasePoetClassResourceFile.kt#L70

I mean if something will be wrong in runtime (for example, dev will pass non-number to first parameter) it's a developer problem and he should fix it.

What do you think?

avently commented 3 years ago

If you really want to prevent passing non-number to the plural parameter you can introduce this type of writing a plural parameter:

{{%d:parameter_name}} 

And if a user didn't specified {{%d:..}} then it's not a number. Otherwise (if specified correctly) this parameter looks like Long in a function as it should be. Nice or bad idea?

avently commented 3 years ago

Looks like I can pass %d to web formatted file and get expected result. Like %d days.

This is the code I use for parsing:

        override fun getPluralString(key: String, count: Long, vararg args: Pair<String, Any>): String {
            var value = repository[pluralKey(key, count)] ?: repository["${key}_plural_1"] ?: return key
            value = value.replace("%d", count.toString())
            return args.fold(value) { acc, pair ->
                return@fold acc.replace("{{${pair.first}}}", pair.second.toString())
            }
        }

        private fun pluralKey(key: String, count: Long): String = when (count) {
            1L -> "${key}_plural_0"
            2L, 3L, 4L -> "${key}_plural_1"
            else -> "${key}_plural_3"
        }

Maybe the code is wrong but for now it looks like it works correctly. So issue can be closed.

KamiSempai commented 3 years ago

This is not the best solution. There is some problems in handling plurals in jsons. I will try to handle it.

avently commented 3 years ago

Is this already fixed? Am I understand correctly that now I need to name parameter as {{count}} in order to get plural string?

KamiSempai commented 3 years ago

Fixed in version 2.2.1. Now if there is only JSON String resources, the argument with hame "count" will be recognised as a quantity.

avently commented 3 years ago

@KamiSempai cool, works as expected, thanks