Skip to content

Commit

Permalink
icerockdev#329 move locale dir logic to single place
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed Jul 24, 2022
1 parent 8a16392 commit a3997c5
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ import java.util.Locale as JvmLocale

sealed interface LanguageType {

fun toAndroidResourcesString(): String
val androidResourcesDir: String
val appleResourcesDir: String
val jsResourcesSuffix: String
val jvmResourcesSuffix: String

object Base : LanguageType {
override fun toAndroidResourcesString(): String = "values"
override val androidResourcesDir: String = "values"
override val appleResourcesDir: String = "Base.lproj"
override val jsResourcesSuffix: String = ""
override val jvmResourcesSuffix: String = ""
}

class Locale(languageTag: String) : LanguageType {

private val locale: JvmLocale = JvmLocale.forLanguageTag(languageTag)

fun toBcpString(): String = locale.toLanguageTag()
fun toLocaleString(): String = locale.toString()
override fun toAndroidResourcesString(): String = buildString {
private fun toLocaleString(): String = locale.toString()

override val androidResourcesDir: String = buildString {
append("values")
append("-")
append(locale.language)
Expand All @@ -30,6 +37,13 @@ sealed interface LanguageType {
}
}

override val appleResourcesDir: String = "${toBcpString()}.lproj"
override val jsResourcesSuffix: String = "_${toBcpString()}"

// JVM ResourceBundle uses locale format, eg `en_US`, instead of BCP format
// like `en-US`.
override val jvmResourcesSuffix: String = "_${toLocaleString()}"

/**
* Throw an error here so that we safeguard ourselves from implcitly calling `Local.toString`.
* You should always use the more explicit methods defined above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AndroidPluralsGenerator(
language: LanguageType,
strings: Map<KeyType, PluralMap>
) {
val valuesDir = File(resourcesGenerationDir, language.toAndroidResourcesString())
val valuesDir = File(resourcesGenerationDir, language.androidResourcesDir())
val stringsFile = File(valuesDir, "multiplatform_plurals.xml")
valuesDir.mkdirs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import dev.icerock.gradle.generator.*
import org.apache.commons.codec.language.bm.Lang
import org.gradle.api.file.FileTree
import java.io.File
import org.apache.commons.text.StringEscapeUtils
Expand Down Expand Up @@ -36,7 +35,7 @@ class AndroidStringsGenerator(
language: LanguageType,
strings: Map<KeyType, String>
) {
val valuesDir = File(resourcesGenerationDir, language.toAndroidResourcesString())
val valuesDir = File(resourcesGenerationDir, language.androidResourcesDir())
val stringsFile = File(valuesDir, "multiplatform_strings.xml")
valuesDir.mkdirs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ package dev.icerock.gradle.generator.apple

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import dev.icerock.gradle.generator.*
import dev.icerock.gradle.generator.KeyType
import dev.icerock.gradle.generator.LanguageType
import dev.icerock.gradle.generator.ObjectBodyExtendable
import dev.icerock.gradle.generator.PluralMap
import dev.icerock.gradle.generator.PluralsGenerator
import org.gradle.api.file.FileTree
import java.io.File

Expand Down Expand Up @@ -73,12 +77,7 @@ class ApplePluralsGenerator(
language: LanguageType,
strings: Map<KeyType, PluralMap>
) {
val resDirName = when (language) {
LanguageType.Base -> "Base.lproj"
is LanguageType.Locale -> "${language.toBcpString()}.lproj"
}

val resDir = File(resourcesGenerationDir, resDirName)
val resDir = File(resourcesGenerationDir, language.appleResourcesDir)
val localizableFile = File(resDir, "Localizable.stringsdict")
resDir.mkdirs()
writeStringsFile(localizableFile, strings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ class AppleStringsGenerator(
language: LanguageType,
strings: Map<KeyType, String>
) {
val resDirName = when (language) {
LanguageType.Base -> "Base.lproj"
is LanguageType.Locale -> "${language.toBcpString()}.lproj"
}

val resDir = File(resourcesGenerationDir, resDirName)
val resDir = File(resourcesGenerationDir, language.appleResourcesDir)
val localizableFile = File(resDir, "Localizable.strings")
resDir.mkdirs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class JsPluralsGenerator(
fallbackFile = "${flattenClassPackage}_${JsMRGenerator.PLURALS_JSON_NAME}.json",
supportedLocalesPropertyName = JsMRGenerator.SUPPORTED_LOCALES_PROPERTY_NAME,
getFileNameForLanguage = { language ->
"${flattenClassPackage}_${JsMRGenerator.PLURALS_JSON_NAME}_${language.toBcpString()}.json"
"${flattenClassPackage}_${JsMRGenerator.PLURALS_JSON_NAME}${language.jsResourcesSuffix}.json"
}
)
val languageKeys = languageMap[LanguageType.Base].orEmpty().keys
Expand All @@ -72,10 +72,8 @@ class JsPluralsGenerator(
language: LanguageType,
strings: Map<KeyType, PluralMap>
) {
val fileDirName = when (language) {
LanguageType.Base -> "${flattenClassPackage}_${JsMRGenerator.PLURALS_JSON_NAME}"
is LanguageType.Locale -> "${flattenClassPackage}_${JsMRGenerator.PLURALS_JSON_NAME}_${language.toBcpString()}"
}
val fileDirName =
"${flattenClassPackage}_${JsMRGenerator.PLURALS_JSON_NAME}${language.jsResourcesSuffix}"

val localizationDir = File(resourcesGenerationDir, JsMRGenerator.LOCALIZATION_DIR).apply {
mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class JsStringsGenerator(
fallbackFile = "${flattenClassPackage}_${JsMRGenerator.STRINGS_JSON_NAME}.json",
supportedLocalesPropertyName = SUPPORTED_LOCALES_PROPERTY_NAME,
getFileNameForLanguage = { language ->
"${flattenClassPackage}_${JsMRGenerator.STRINGS_JSON_NAME}_${language.toBcpString()}.json"
"${flattenClassPackage}_${JsMRGenerator.STRINGS_JSON_NAME}${language.jsResourcesSuffix}.json"
}
)
val languageKeys = languageMap[LanguageType.Base].orEmpty().keys
Expand All @@ -73,10 +73,8 @@ class JsStringsGenerator(
language: LanguageType,
strings: Map<KeyType, String>
) {
val fileDirName = when (language) {
LanguageType.Base -> "${flattenClassPackage}_${JsMRGenerator.STRINGS_JSON_NAME}"
is LanguageType.Locale -> "${flattenClassPackage}_${JsMRGenerator.STRINGS_JSON_NAME}_${language.toBcpString()}"
}
val fileDirName =
"${flattenClassPackage}_${JsMRGenerator.STRINGS_JSON_NAME}${language.jsResourcesSuffix}"

val localizationDir = File(resourcesGenerationDir, JsMRGenerator.LOCALIZATION_DIR).apply {
mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ class JvmPluralsGenerator(
language: LanguageType,
strings: Map<KeyType, PluralMap>
) {
val fileDirName = when (language) {
LanguageType.Base -> "${flattenClassPackage}_${JvmMRGenerator.PLURALS_BUNDLE_NAME}"
is LanguageType.Locale -> {
// JVM ResourceBundle uses locale format, eg `en_US`, instead of BCP format
// like `en-US`.
"${flattenClassPackage}_${JvmMRGenerator.PLURALS_BUNDLE_NAME}_${language.toLocaleString()}"
}
}
val fileDirName =
"${flattenClassPackage}_${JvmMRGenerator.PLURALS_BUNDLE_NAME}${language.jvmResourcesSuffix}"

val localizationDir =
File(resourcesGenerationDir, JvmMRGenerator.LOCALIZATION_DIR).apply { mkdirs() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,8 @@ class JvmStringsGenerator(
language: LanguageType,
strings: Map<KeyType, String>
) {
val fileDirName = when (language) {
LanguageType.Base -> "${flattenClassPackage}_${JvmMRGenerator.STRINGS_BUNDLE_NAME}"
is LanguageType.Locale -> {
// JVM ResourceBundle uses locale format, eg `en_US`, instead of BCP format
// like `en-US`.
"${flattenClassPackage}_${JvmMRGenerator.STRINGS_BUNDLE_NAME}_${language.toLocaleString()}"
}
}
val fileDirName =
"${flattenClassPackage}_${JvmMRGenerator.STRINGS_BUNDLE_NAME}${language.jvmResourcesSuffix}"

val localizationDir = File(resourcesGenerationDir, JvmMRGenerator.LOCALIZATION_DIR).apply {
mkdirs()
Expand Down

0 comments on commit a3997c5

Please sign in to comment.