Skip to content

Commit 44fc14a

Browse files
committed
fix: review fixes
1 parent 2cb6eec commit 44fc14a

12 files changed

Lines changed: 97 additions & 30 deletions

File tree

core/src/androidMain/kotlin/io/tolgee/TolgeeAndroid.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ data class TolgeeAndroid internal constructor(
139139
}?.ifEmpty { null } ?: resources.getStringArray(id).toList()
140140
}
141141

142+
/**
143+
* Provides an immediate translation for the given string resource ID within the given context.
144+
* If a translation key is derived from the string resource, it retrieves the translation using Tolgee.
145+
* Otherwise, it falls back to returning the default string resource value.
146+
*
147+
* This is a special version that allows returning a [CharSequence] instead of a [String] and
148+
* will fall back to the Android `getText` method if no translation is found - preserving formatting.
149+
*
150+
* If translation is found, no style information is preserved and the method acts the same as [t].
151+
*
152+
* @param context The context used to access resources and provide localization settings.
153+
* @param id The resource ID of the string to be translated.
154+
* @return The translated string if a key-based translation is found; otherwise, the default string resource value.
155+
*/
156+
fun tStyled(context: Context, @StringRes id: Int): CharSequence {
157+
return getKeyFromResources(context, id)?.let { key ->
158+
t(key)
159+
} ?: context.getText(id)
160+
}
161+
142162
/**
143163
* Preloads the required languages and their translations for the current Tolgee instance.
144164
*

core/src/androidMain/kotlin/io/tolgee/TolgeeResources.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package io.tolgee
22

33
import android.content.Context
44
import android.content.res.Resources
5-
import android.util.Log
65
import io.tolgee.common.getQuantityStringT
76
import io.tolgee.common.getStringArrayT
87
import io.tolgee.common.getStringT
8+
import io.tolgee.common.getTextT
99

1010
/**
1111
* Ignore Deprecation: Resources constructor is not really deprecated, apps should just not create
@@ -40,6 +40,6 @@ internal class TolgeeResources(
4040
}
4141

4242
override fun getText(id: Int): CharSequence {
43-
return baseContext.getStringT(tolgee, id)
43+
return baseContext.getTextT(tolgee, id)
4444
}
4545
}

core/src/androidMain/kotlin/io/tolgee/common/ExtendTolgee.android.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ fun Resources.getStringArrayT(tolgee: Tolgee, @ArrayRes resId: Int): Array<Strin
149149
return list?.ifEmpty { null }?.toTypedArray() ?: this.getStringArray(resId)
150150
}
151151

152+
/**
153+
* Returns a localized formatted string from [Tolgee] cache or the application's package's
154+
* default string table, substituting the format arguments as defined in
155+
* [java.util.Formatter] and [java.lang.String.format].
156+
*
157+
* This function will fall back to the Android `getText` method if no translation is found - preserving formatting.
158+
* If translation is found, no style information is preserved and the method acts the same as [getStringT].
159+
*
160+
* @param tolgee The [Tolgee] instance to get the cached string from.
161+
* @param resId Resource id for the format string
162+
* @return The string data associated with the resource, formatted and
163+
* stripped of styled text information.
164+
*/
165+
fun Context.getTextT(tolgee: Tolgee, @StringRes resId: Int): CharSequence {
166+
return (tolgee as? TolgeeAndroid)?.tStyled(this, resId)
167+
?: TolgeeAndroid.getKeyFromResources(this, resId)?.let {
168+
tolgee.t(key = it, parameters = TolgeeMessageParams.None)
169+
} ?: this.getText(resId)
170+
}
171+
152172
internal actual val platformStorage: TolgeeStorageProvider?
153173
get() = null
154174

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
package io.tolgee.storage
22

33
import android.content.Context
4+
import dev.datlag.tooling.canReadSafely
5+
import dev.datlag.tooling.canWriteSafely
6+
import dev.datlag.tooling.existsSafely
7+
import dev.datlag.tooling.mkdirsSafely
8+
import dev.datlag.tooling.scopeCatching
49
import java.io.File
510

611
class TolgeeStorageProviderAndroid(
7-
private val context: Context,
8-
private val versionCode: Int,
9-
private val path: String = "tolgee/localization-cache",
12+
private val context: Context,
13+
private val versionCode: Int,
14+
private val path: String = "tolgee/localization-cache",
1015
) : TolgeeStorageProvider {
11-
private val cacheDir get() = context.filesDir / path / versionCode.toString()
16+
private val cacheDir get() = context.filesDir / path / versionCode.toString()
1217

13-
override fun put(name: String, data: ByteArray) {
14-
val dir = cacheDir
15-
dir.mkdirs()
16-
val file = dir / escape(name)
17-
file.writeBytes(data)
18-
}
18+
override fun put(name: String, data: ByteArray) {
19+
val dir = cacheDir
20+
dir.mkdirsSafely()
21+
val file = dir / escape(name)
22+
if (!file.canWriteSafely()) {
23+
return
24+
}
25+
scopeCatching {
26+
file.writeBytes(data)
27+
}
28+
}
29+
30+
override fun get(name: String): ByteArray? {
31+
val file = cacheDir / escape(name)
32+
if (!file.existsSafely() || !file.canReadSafely()) {
33+
return null
34+
}
1935

20-
override fun get(name: String): ByteArray? {
21-
val file = cacheDir / escape(name)
22-
if (!file.exists()) {
23-
return null
36+
return scopeCatching {
37+
file.readBytes()
38+
}.getOrNull()
2439
}
25-
return file.readBytes()
26-
}
2740

28-
private fun escape(name: String): String {
29-
return name.replace("/", "_")
30-
}
41+
private fun escape(name: String): String {
42+
return name.replace("/", "_")
43+
}
3144

32-
internal companion object {
33-
private infix operator fun File.div(other: String) = File(this, other)
34-
}
45+
internal companion object {
46+
private infix operator fun File.div(other: String) = File(this, other)
47+
}
3548
}

core/src/appleMain/kotlin/io/tolgee/TolgeeApple.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ data class TolgeeApple internal constructor(
171171
table: String?,
172172
vararg args: Any
173173
): String? {
174-
// FIXME: why not just expand the args?? is there some weird limitation with calling the c functions?
175174
return (getLocalizedStringFromBundle(res, key, default, table) ?: default?.ifBlank { null })?.let { format ->
176175
when (args.size) {
177176
0 -> NSString.localizedStringWithFormat(format)

core/src/commonMain/kotlin/io/tolgee/Tolgee.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ open class Tolgee(
744744
*/
745745
@JvmStatic
746746
val instance: PlatformTolgee
747-
get() = _instance.value ?: throw IllegalStateException("Tolgee instance not initialized")
747+
get() = _instance.value ?: throw IllegalStateException("Tolgee instance not initialized")
748748

749749
/**
750750
* Initializes the Tolgee framework with the specified configuration and sets it as the global instance.

core/src/commonMain/kotlin/io/tolgee/common/ExtendParser.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import kotlinx.serialization.json.*
1313
* @return The string representation of the [JsonElement], or an empty string if it cannot be resolved.
1414
*/
1515
internal fun JsonElement.stringValue(): String = when (this) {
16-
// FIXME: Whyyy; either its primitive or its an error/non-existent key
1716
is JsonPrimitive -> contentOrNull ?: ""
1817
is JsonArray -> firstOrNull()?.stringValue() ?: ""
1918
is JsonObject -> this.values.firstOrNull()?.stringValue() ?: ""
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.tolgee.storage
22

33
interface TolgeeStorageProvider {
4-
fun put(name: String, data: ByteArray)
5-
fun get(name: String): ByteArray?
4+
fun put(name: String, data: ByteArray)
5+
fun get(name: String): ByteArray?
66
}

demo/exampleandroid/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
android:icon="@mipmap/ic_launcher"
1010
android:label="@string/app_name"
1111
android:roundIcon="@mipmap/ic_launcher_round"
12-
android:supportsRtl="true">
12+
android:supportsRtl="true"
13+
android:networkSecurityConfig="@xml/network_security">
1314
<activity
1415
android:name=".MainActivity"
1516
android:exported="true">
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config xmlns:android="http://schemas.android.com/apk/res/android">
3+
<domain-config>
4+
<domain includeSubdomains="true">tolgee.io</domain>
5+
<domain includeSubdomains="true">tolg.ee</domain>
6+
</domain-config>
7+
</network-security-config>

0 commit comments

Comments
 (0)