Skip to content

Commit

Permalink
feat(all): Remove crash if key is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
qk7b authored Oct 28, 2021
2 parents d7888f7 + 21d8627 commit 529b4af
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions gero/src/main/java/com/github/quentin7b/android/gero/Gero.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.quentin7b.android.gero

import android.content.Context
import android.content.res.Resources
import android.util.Log
import kotlinx.coroutines.*
import java.io.BufferedReader
Expand Down Expand Up @@ -239,7 +238,7 @@ class Gero private constructor(
context: Context,
locale: Locale,
fallbackLocale: Locale = Locale.US,
sendKeyIfNotFound: Boolean = false
sendKeyIfNotFound: Boolean = false,
): Deferred<Unit> {
val loadingDeferred = CompletableDeferred<Unit>()
val shouldReload = CURRENT_GERO == null ||
Expand Down Expand Up @@ -292,13 +291,13 @@ class Gero private constructor(
* @param args any arguments used to format the string (%d, %f, ...)
* @return the formatted string value
* @see SingleValues.getText
*
* @throws Resources.NotFoundException if the string value is not found
*/
@Synchronized
fun getText(key: String, vararg args: Any?): String {
var locale: Locale? = null
if (CURRENT_GERO != null && CURRENT_GERO!!.hasTextLoaded) {
val translation = CURRENT_GERO!!.singleForKey(key, *args)
locale = CURRENT_GERO!!.currentLocale
if (translation != null) {
return translation
}
Expand All @@ -307,17 +306,18 @@ class Gero private constructor(
// Check for fallback
if (FALLBACK_GERO != null && FALLBACK_GERO!!.hasTextLoaded) {
val translation = FALLBACK_GERO!!.singleForKey(key, *args)
locale = CURRENT_GERO!!.currentLocale
if (translation != null) {
return translation
}
}

if (CURRENT_GERO!!.sendKeyIfNotFound) {
return key
Log.w("Gero", "The key [$key] is not found for locale [$locale]")
return if (CURRENT_GERO?.sendKeyIfNotFound == true) {
key
} else {
""
}

// No translation, no fallback, exit
throw Resources.NotFoundException("Cant find string with key $key in current files")
}

/**
Expand All @@ -328,13 +328,13 @@ class Gero private constructor(
* @param args any arguments used to format the string (%d, %f, ...)
* @return the formatted string value
* @see PluralsValues.getText
*
* @throws Resources.NotFoundException if the string value is not found
*/
@Synchronized
fun getQuantityText(key: String, quantity: Int, vararg args: Any?): String {
var locale: Locale? = null
if (CURRENT_GERO != null && CURRENT_GERO!!.hasTextLoaded) {
val translation = CURRENT_GERO!!.pluralForKey(key, quantity, *args)
locale = CURRENT_GERO!!.currentLocale
if (translation != null) {
return translation
}
Expand All @@ -343,16 +343,18 @@ class Gero private constructor(
// Check for fallback
if (FALLBACK_GERO != null && FALLBACK_GERO!!.hasTextLoaded) {
val translation = FALLBACK_GERO!!.pluralForKey(key, quantity, *args)
locale = CURRENT_GERO!!.currentLocale
if (translation != null) {
return translation
}
}

if (CURRENT_GERO!!.sendKeyIfNotFound) {
return key
Log.w("Gero", "The key [$key] is not found for locale [$locale]")
return if (CURRENT_GERO?.sendKeyIfNotFound == true) {
key
} else {
""
}
// No translation, no fallback, exit
throw Resources.NotFoundException("Cant find string with key $key in current files")
}

/**
Expand All @@ -362,13 +364,17 @@ class Gero private constructor(
*/
@Synchronized
fun getCurrentLocale(): Locale {
if (CURRENT_GERO != null && CURRENT_GERO!!.hasTextLoaded) {
return CURRENT_GERO!!.currentLocale;
}
if (FALLBACK_GERO != null && FALLBACK_GERO!!.hasTextLoaded) {
return FALLBACK_GERO!!.currentLocale;
return when {
CURRENT_GERO?.hasTextLoaded == true -> {
CURRENT_GERO!!.currentLocale
}
FALLBACK_GERO?.hasTextLoaded != true -> {
FALLBACK_GERO!!.currentLocale
}
else -> {
throw NullPointerException("Gero is null or has no loaded text ($CURRENT_GERO, ${CURRENT_GERO?.hasTextLoaded}, $FALLBACK_GERO, ${FALLBACK_GERO?.hasTextLoaded}")
}
}
throw NullPointerException("Gero is null or has no loaded text ($CURRENT_GERO, ${CURRENT_GERO?.hasTextLoaded}, $FALLBACK_GERO, ${FALLBACK_GERO?.hasTextLoaded}")
}
}
}
Expand Down

0 comments on commit 529b4af

Please sign in to comment.