Skip to content
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

Merged
merged 20 commits into from
Jul 5, 2024

Conversation

touhidurrr
Copy link
Contributor

resolves #11895

@touhidurrr
Copy link
Contributor Author

Ok, here is the result for now. Green means translated properly and red means was not translated for some reason. Maybe the regex I wrote was problematic?
image

@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 1, 2024

@yairm210 how do i fix this delekt error?

@touhidurrr
Copy link
Contributor Author

Although not sure why the regex is having issues. See: https://regexr.com/82b23

@SomeTroglodyte
Copy link
Collaborator

"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.

@touhidurrr
Copy link
Contributor Author

tweaking stuff

How to check this locally?

@touhidurrr
Copy link
Contributor Author

Also, do you have any idea why all numbers inside the screen is not being translated properly?

@SomeTroglodyte
Copy link
Collaborator

Maybe fixing the evil wildcard import in ConsoleLauncher is enough

How to check this locally?

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.

@SomeTroglodyte
Copy link
Collaborator

SomeTroglodyte commented Jul 1, 2024

That regex is over the top. I believe both \b are misguided (well, the comment says what they're for, but I think in our input that's irrelevant), and I'm sure we'll never have fractional values without a leading zero, or values in scientific notation.

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)

not translated for some reason

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: cultureLabel.setText(getCultureText(civInfo, nextTurnStats)) -> aha! setText is direct Gdx, no translate call. Many more of course.

Last comment for today! Bye and have fun.

@yairm210
Copy link
Owner

yairm210 commented Jul 1, 2024

Detekt fixed
The reason the main screen contains some untranslated numbers is because they're never .tr()'d

@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 1, 2024

I believe both \b are misguided

I suspected this also, but all of my tests said otherwise.

aha! setText is direct Gdx, no translate call. Many more of course.

This has to be the reason why

translateIndividualWord

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
 }

@touhidurrr
Copy link
Contributor Author

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.

Then maybe new rule: always use toTranslatedString() to convert numbers to strings?

@touhidurrr
Copy link
Contributor Author

Need guidance, @yairm210. What do you think should be done?

@yairm210
Copy link
Owner

yairm210 commented Jul 2, 2024

A. Not during placeholder fill, you'll miss all the non-placeholder labels
B. Yes, you'll need to add like a .tr() for numbers

@touhidurrr
Copy link
Contributor Author

Although I am using String.translateIndividualWord() currently in my pr, my concern is that many translations might have numbers that we might miss. For example: 100a is a word, but cannot be parsed with toDouble(). So, this will be missed.
And from the looks of it, I am not getting words here after all.
image

@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 2, 2024

font issues aside, this is the current update:
image
image
image

As expected, stuff like 0% or 2/2 is broken.

@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 2, 2024

Caching NumberFormat classes is also a problem. It is getting saved to GameSettings.kt which I do not want to.

@SomeTroglodyte
Copy link
Collaborator

getting saved to

@Transient

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.

@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 2, 2024

@Transient

Where do I put it?

@touhidurrr
Copy link
Contributor Author

B. Yes, you'll need to add like a .tr() for numbers

Done in 5e47a3a

@touhidurrr
Copy link
Contributor Author

@yairm210 @SomeTroglodyte need review.

@yairm210
Copy link
Owner

yairm210 commented Jul 2, 2024

Please add tests for the following to ensure they remain the same text:

  • "1"
  • "1.0"
  • "+1"
  • "-1"

@touhidurrr touhidurrr requested a review from yairm210 July 2, 2024 20:52
@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 2, 2024

Please add tests for the following to ensure they remain the same text:

  • "1"
  • "1.0"
  • "+1"
  • "-1"

As we are using toDoubleOrNull(), I don't think this is possible. +1 would be 1, 1.0 would be 1. Should implement this or it is ok as is?

@touhidurrr
Copy link
Contributor Author

touhidurrr commented Jul 3, 2024

As expected, stuff like 0% or 2/2 is broken.

"+1", "1.0"

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?

@touhidurrr
Copy link
Contributor Author

Although this did not fix cases where String.tr() is not called. This this all fix other cases. From my local test, all previously broken numbers are fixed here. (Except main screen ones where .tr() is not called)
image

@touhidurrr
Copy link
Contributor Author

Tell me if I should add this to this pr.

@touhidurrr
Copy link
Contributor Author

Well, it will commit them anyways. (turned out to be less complex than my initial write).
Tell me what you think!

@yairm210
Copy link
Owner

yairm210 commented Jul 3, 2024

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}")
For the examples I listed above, it is important - For example "[+20]% Strength" should be displayed - in English as "+20% Strength" and NOT as "20% Strength", so losing the + because of int parsing -> stringifying is not acceptable

Comment on lines +471 to +473
val translation = UncivGame.Current.translations.getText(
this, language, TranslationActiveModsCache.activeMods
).replace(digitsRegex) { it.value.toLong().tr(language) }
Copy link
Owner

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

@yairm210 yairm210 merged commit 461a7ac into yairm210:master Jul 5, 2024
4 checks passed
@touhidurrr touhidurrr deleted the numerical-translation branch July 5, 2024 20:06
@touhidurrr touhidurrr mentioned this pull request Jul 5, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Numbers Translation
3 participants