Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #39 from lsafer-meemer/main
Browse files Browse the repository at this point in the history
refactor: updated i18n support 2
  • Loading branch information
LSafer authored Nov 26, 2023
2 parents a458533 + 7c53aa2 commit b2ca315
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
16 changes: 8 additions & 8 deletions bson/src/main/kotlin/BsonDocumentLike.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ fun mutableBsonMapOf(vararg pairs: Pair<String, BsonElement>) =
* @see Locale.LanguageRange.parse
*/
@OptIn(ExperimentalBsonApi::class)
operator fun BsonDocumentLike.get(name: String, lang: String): Pair<BsonElement?, String> {
operator fun BsonDocumentLike.get(name: String, lang: String): Localized<BsonElement?> {
val rangeList = Locale.LanguageRange.parse(lang)
val langTagList = getLangList(name)
val langTag = Locale.lookupTag(rangeList, langTagList)
langTag ?: return get(name) to ""
if (langTag.isEmpty()) return get(name) to ""
return get("$name#$langTag") to langTag
langTag ?: return Localized(get(name), lang = "")
if (langTag.isEmpty()) return Localized(get(name), lang = "")
return Localized(get("$name#$langTag"), lang = langTag)
}

/**
Expand All @@ -202,13 +202,13 @@ operator fun BsonDocumentLike.get(name: String, lang: String): Pair<BsonElement?
* @see Locale.LanguageRange
*/
@OptIn(ExperimentalBsonApi::class)
operator fun BsonDocumentLike.get(name: String, lang: List<String>): Pair<BsonElement?, String> {
operator fun BsonDocumentLike.get(name: String, lang: List<String>): Localized<BsonElement?> {
val rangeList = lang.map { Locale.LanguageRange(it) }
val langTagList = getLangList(name)
val langTag = Locale.lookupTag(rangeList, langTagList)
langTag ?: return get(name) to ""
if (langTag.isEmpty()) return get(name) to ""
return get("$name#$langTag") to langTag
langTag ?: return Localized(get(name), lang = "")
if (langTag.isEmpty()) return Localized(get(name), lang = "")
return Localized(get("$name#$langTag"), lang = langTag)
}

/**
Expand Down
19 changes: 19 additions & 0 deletions bson/src/main/kotlin/Localized.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.cufy.bson

/**
* A data class encapsulated an element with its language.
*/
data class Localized<T>(
val element: T,
val lang: String,
)

/**
* Return a string derived from this one with
* its content tagged with the given language [tag].
*/
@BsonKeywordMarker
infix fun String.lang(tag: String): String {
if (tag.isEmpty()) return this
return "$this#$tag"
}
8 changes: 4 additions & 4 deletions codec/src/main/kotlin/BsonFieldCodec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, out BsonElement>): I
* given ranges is ill-formed
* @see Locale.LanguageRange.parse
*/
operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, out BsonElement>, lang: String): Pair<I, String> {
operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, out BsonElement>, lang: String): Localized<I> {
val (element, langTag) = this[codec.name, lang]
return decodeAny(element, codec) to langTag
return Localized(decodeAny(element, codec), lang = langTag)
}

/**
Expand All @@ -207,9 +207,9 @@ operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, out BsonElement>, lan
* in RFC 4647
* @see Locale.LanguageRange
*/
operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, out BsonElement>, lang: List<String>): Pair<I, String> {
operator fun <I> BsonDocumentLike.get(codec: FieldCodec<I, out BsonElement>, lang: List<String>): Localized<I> {
val (element, langTag) = this[codec.name, lang]
return decodeAny(element, codec) to langTag
return Localized(decodeAny(element, codec), lang = langTag)
}

/**
Expand Down
2 changes: 2 additions & 0 deletions codec/src/test/kotlin/example.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.cufy.bson.BsonDocument
import org.cufy.bson.Id
import org.cufy.bson.bson
import org.cufy.bson.java.java
import org.cufy.bson.lang
import org.cufy.codec.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
Expand All @@ -23,6 +24,7 @@ class ExampleTest {
val (automatic, automaticLang) = doc[Document1.Name, preference]
val manual = doc[Document1.Name.Nullable lang "ar-SA"]

assertEquals("name#en-US", "name" lang "en-US")
assertEquals(automatic, "EN-US")
assertEquals(automaticLang, "en-US")
assertEquals(manual, "AR-SA")
Expand Down

0 comments on commit b2ca315

Please sign in to comment.