-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translate numbers to selected language #11898
Conversation
@yairm210 how do i fix this delekt error? |
Although not sure why the regex is having issues. See: https://regexr.com/82b23 |
"delekt" is a very nice typo! But detekt is a static code analysis tool, it's about overall code quality. Master currently fails too. So, just wait and merge forward later. Normally, tweaking stuff like what detekt dislikes until we're back below threshold would be right up my alley, but I don't have the energy today. |
How to check this locally? |
Also, do you have any idea why all numbers inside the screen is not being translated properly? |
Maybe fixing the evil wildcard import in ConsoleLauncher is enough
Not sure. Search for detekt PR's, go to their repo. Download separately and run. I wouldn't, I got the checks running on my fork too, so I can read the output for local changes as soon as I push. |
That regex is over the top. I believe both Also, I wouldn't stress tr() so. I'd classify it as extremely performance critical, even though it's UI only. Better.... Better rely on any interesting numbers being within placeholders, so only treat strings inside translateIndividualWord (I'd have named that translateIndividualPhrase) that are in their entirety a number: Index: core/src/com/unciv/models/translations/Translations.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt
--- a/core/src/com/unciv/models/translations/Translations.kt (revision b20c89733bc2283bbd8124aad46652298d6822b9)
+++ b/core/src/com/unciv/models/translations/Translations.kt (date 1719848052584)
@@ -443,6 +443,9 @@
private fun String.translateIndividualWord(language: String, hideIcons: Boolean): String {
if (Stats.isStats(this)) return Stats.parse(this).toString()
+ val number = toDoubleOrNull()
+ if (number != null) return numberFormatter.format(number)
+
val translation = UncivGame.Current.translations.getText(this, language, TranslationActiveModsCache.activeMods)
val stat = Stat.safeValueOf(this) (Or later in the function, not sure)
I have a broken color management so I'm colorblind atm, but - any coder passing a string to the UI, if they know it'll always be a number, they would treat it as not needing translation at all. Many of our helpers will auto-translate, but some things won't. Soooo... Look at e.g. WorldScreenTopBarStats: Last comment for today! Bye and have fun. |
Detekt fixed |
I suspected this also, but all of my tests said otherwise.
This has to be the reason why
So, in the end the best option might be trying to detect and translate numbers during placeholder fill. I tried this but it does not work at all. --- a/core/src/com/unciv/models/translations/Translations.kt
+++ b/core/src/com/unciv/models/translations/Translations.kt
@@ -491,9 +491,16 @@ fun String.fillPlaceholders(vararg strings: String): String {
if (keys.size > strings.size)
throw Exception("String $this has a different number of placeholders ${keys.joinToString()} (${keys.size}) than the substitutive strings ${strings.joinToString()} (${strings.size})!")
+ val numberFormatter = UncivGame.Current.settings.getNumberFormatFromLocale()
+
+ val translatedStrings = strings.map {
+ val num = toDoubleOrNull() ?: return@map it
+ return@map numberFormatter.format(num)
+ }
+
var filledString = this.replace(squareBraceRegex, "[]")
for (i in keys.indices)
- filledString = filledString.replaceFirst("[]", "[${strings[i]}]")
+ filledString = filledString.replaceFirst("[]", "[${translatedStrings[i]}]")
return filledString
} |
Then maybe new rule: always use |
Need guidance, @yairm210. What do you think should be done? |
A. Not during placeholder fill, you'll miss all the non-placeholder labels |
Caching |
Am recovering from a bricked box thanks to one little mis-keytap trying to fix the new graphics driver issues, plus one timeshift mishap. So, might be a few days until I can catch up here. |
Where do I put it? |
8b06bea
to
333b16d
Compare
Done in 5e47a3a |
@yairm210 @SomeTroglodyte need review. |
Please add tests for the following to ensure they remain the same text:
|
As we are using |
One plan to fix both cases would be something like this: playground Output: '1' -> '১'
'+1' -> '+১'
'-1' -> '-১'
'1.0' -> '১.০'
'+1.0' -> '+১.০'
'-1.0' -> '-১.০'
'0%' -> '০%'
'2/2' -> '২/২'
'(4/5)' -> '(৪/৫)' Should I continue with it? |
Tell me if I should add this to this pr. |
Well, it will commit them anyways. (turned out to be less complex than my initial write). |
There should be nowhere that we need to translate "2/2" or "5%" directly, since we can translate each piece by putting them in square brackets (like "{2}/{2}") |
val translation = UncivGame.Current.translations.getText( | ||
this, language, TranslationActiveModsCache.activeMods | ||
).replace(digitsRegex) { it.value.toLong().tr(language) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I personally can't think of a problem except for maybe performance, which should hopefully not be a problem
resolves #11895