Skip to content

Commit

Permalink
Set up locales
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Aug 1, 2024
1 parent f03dd6c commit d464155
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package eu.iamgio.quarkdown.document.locale

/**
* [Locale] implementation using [java.util.Locale].
*/
internal class JVMLocale(private val jvmLocale: java.util.Locale) : Locale {
override val code: String
get() = jvmLocale.language

override val countryCode: String?
get() = jvmLocale.country.takeIf { it.isNotBlank() }

override val localizedName: String
get() = jvmLocale.getDisplayLanguage(jvmLocale)

override val localizedCountryName: String?
get() = jvmLocale.getDisplayCountry(jvmLocale).takeIf { it.isNotBlank() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eu.iamgio.quarkdown.document.locale

/**
* Loader of [JVMLocale]s.
*/
internal object JVMLocaleLoader : LocaleLoader {
override val all: Iterable<Locale>
get() = java.util.Locale.getAvailableLocales().map(::JVMLocale)

override fun fromTag(tag: String): Locale? =
java.util.Locale.forLanguageTag(tag)
?.let(::JVMLocale)
?.takeIf { it.code.isNotBlank() }
}
29 changes: 29 additions & 0 deletions core/src/main/kotlin/eu/iamgio/quarkdown/document/locale/Locale.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package eu.iamgio.quarkdown.document.locale

/**
* Represents a locale, which defines the document language.
*/
interface Locale {
/**
* Language code of the locale.
* For instance, `en` for English and `it` for Italian.
*/
val code: String

/**
* Country code of the locale.
*/
val countryCode: String?

/**
* Name of the locale, possibly in the locale's language itself.
* For instance, `English` for English and `italiano` for Italian.
*/
val localizedName: String

/**
* Name of the country of the locale, possibly in the locale's language itself.
* For instance, `United States` for `en-US` and `Italia` for `it`.
*/
val localizedCountryName: String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package eu.iamgio.quarkdown.document.locale

/**
* Loader of [Locale]s.
*/
interface LocaleLoader {
/**
* All available locales.
*/
val all: Iterable<Locale>

/**
* @param tag language code of the locale and optionally the country code, separated by a hyphen.
* Example: `en`, `en-US`, `it`, `fr-CA`
* @return [Locale] with the given tag, or `null` if not found
*/
fun fromTag(tag: String): Locale?
}
54 changes: 54 additions & 0 deletions core/src/test/kotlin/eu/iamgio/quarkdown/MiscTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package eu.iamgio.quarkdown

import eu.iamgio.quarkdown.document.locale.JVMLocaleLoader
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull

/**
* Tests for miscellaneous classes.
*/
class MiscTest {
@Test
fun locale() {
val retriever = JVMLocaleLoader

with(retriever.fromTag("en")) {
assertNotNull(this)
assertEquals("en", code)
assertEquals("English", localizedName)
assertNull(countryCode)
assertNull(localizedCountryName)
}

with(retriever.fromTag("it")) {
assertNotNull(this)
assertEquals("it", code)
assertEquals("italiano", localizedName)
assertNull(countryCode)
assertNull(localizedCountryName)
}

with(retriever.fromTag("en-US")) {
assertNotNull(this)
assertEquals("en", code)
assertEquals("English", localizedName)
assertEquals("US", countryCode)
assertEquals("United States", localizedCountryName)
}

with(retriever.fromTag("fr-CA")) {
assertNotNull(this)
assertEquals("fr", code)
assertEquals("français", localizedName)
assertEquals("CA", countryCode)
assertEquals("Canada", localizedCountryName)
}

assertNull(retriever.fromTag("nonexistent"))

assertTrue(retriever.all.iterator().hasNext())
}
}

0 comments on commit d464155

Please sign in to comment.