From 8cae2fb1251f1445e3e9d83aa52d4bc72633a0d1 Mon Sep 17 00:00:00 2001 From: JackRainy Date: Sun, 1 Mar 2020 23:10:43 +0200 Subject: [PATCH] Include Nations in the calculation of a translation`s percentage (#2044) * Include Nations in the calculation of a translation`s percentage * Refactoring: Parameter renaming * Check for empty originals --- .../unciv/models/translations/Translations.kt | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt index 439097e1ed..17a5b1e155 100644 --- a/core/src/com/unciv/models/translations/Translations.kt +++ b/core/src/com/unciv/models/translations/Translations.kt @@ -1,9 +1,10 @@ package com.unciv.models.translations import com.badlogic.gdx.Gdx +import com.unciv.JsonParser import com.unciv.UncivGame +import com.unciv.models.ruleset.Nation import java.util.* -import kotlin.collections.ArrayList import kotlin.collections.HashMap class Translations : LinkedHashMap(){ @@ -124,13 +125,20 @@ class Translations : LinkedHashMap(){ Gdx.files.internal("jsons/translationsByLanguage/template.properties") .reader().forEachLine { if(it.contains(" = ")) allTranslations+=1 } + val notTranslatedNations = JsonParser().getFromJson(emptyArray().javaClass, + "jsons/Nations/Nations.json") + for(language in getLanguagesWithTranslationFile()){ val translationFileName = "jsons/translationsByLanguage/$language.properties" var translationsOfThisLanguage=0 Gdx.files.internal(translationFileName).reader() .forEachLine { if(it.contains(" = ") && !it.endsWith(" = ")) translationsOfThisLanguage+=1 } - percentComplete[language] = translationsOfThisLanguage*100/allTranslations + + val translatedAndTotal = calculatePercentageForNationsFile(language, notTranslatedNations) + translationsOfThisLanguage += translatedAndTotal.first + + percentComplete[language] = translationsOfThisLanguage*100/(allTranslations+translatedAndTotal.second) } @@ -139,6 +147,43 @@ class Translations : LinkedHashMap(){ return percentComplete } + private fun calculatePercentageForNationsFile(language: String, originalNations: Array): Pair { + + val translationFileName = "jsons/Nations/Nations_$language.json" + val translationFile = Gdx.files.internal(translationFileName) + if (!translationFile.exists()) { + // calculate how many fields are missing + val allTranslatables = Nation::class.java.declaredFields.count { + it.type == String::class.java || it.type == ArrayList::class.java} + return Pair(0, allTranslatables*originalNations.size) + } + + var translationsOfThisLanguage = 0 + var allTranslations = 0 + + val translatedNations = JsonParser().getFromJson(emptyArray().javaClass, translationFileName) + + for (nation in originalNations) + { + val translatedNation = translatedNations.find { it.name == nation.name } + + for (field in nation.javaClass.declaredFields. + filter { it.type == String::class.java || it.type == ArrayList::class.java}) { + field.isAccessible = true + val originalValue = field.get(nation) + if (translatedNation != null && // we could exit *before* this loop, however, we need it here to count not translated fields + (field.name in setOf("name", "startBias") || // skip fields which must not be translated + originalValue == null || originalValue == "" || + ((originalValue is ArrayList<*>) && originalValue.isEmpty()) || + originalValue != field.get(translatedNation))) + translationsOfThisLanguage++ + + allTranslations++ + } + } + + return Pair(translationsOfThisLanguage, allTranslations) + } companion object { fun translateBonusOrPenalty(unique:String): String {