-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
core/src/main/kotlin/eu/iamgio/quarkdown/document/locale/JVMLocale.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/kotlin/eu/iamgio/quarkdown/document/locale/JVMLocaleLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
core/src/main/kotlin/eu/iamgio/quarkdown/document/locale/Locale.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
18 changes: 18 additions & 0 deletions
18
core/src/main/kotlin/eu/iamgio/quarkdown/document/locale/LocaleLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |