-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#41] Formatting-Options like lowercase, uppercase, capitalize
- Loading branch information
Showing
5 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
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
3 changes: 2 additions & 1 deletion
3
...ages/formatter/MessageNumberFormatters.kt → ...ormatter/types/MessageNumberFormatters.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
88 changes: 88 additions & 0 deletions
88
...commonMain/kotlin/de/comahe/i18n4k/messages/formatter/types/MessageTransformFormatters.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,88 @@ | ||
package de.comahe.i18n4k.messages.formatter.types | ||
|
||
import de.comahe.i18n4k.Locale | ||
import de.comahe.i18n4k.messages.formatter.MessageValueFormatter | ||
import de.comahe.i18n4k.messages.formatter.parsing.MessageFormatContext | ||
import de.comahe.i18n4k.messages.formatter.parsing.StylePart | ||
import de.comahe.i18n4k.messages.formatter.parsing.toSimple | ||
import kotlinx.collections.immutable.persistentListOf | ||
|
||
/** | ||
* Provides the possibility to transform the letters to uppercase, | ||
* lowercase or capitalize (uppercase the first letter, rest lowercase). | ||
* | ||
* Format: | ||
* | ||
* {~, FORMAT_OPTION, TEXT_WITH_PATTERNS} | ||
* - FORMAT_OPTION | ||
* - uppercase: transform all letters to uppercase | ||
* - lowercase: transform all letters to lowercase | ||
* - capitalize: transform the first letter to uppercase | ||
* - TEXT_WITH_PATTERNS: Any text including patterns. | ||
* | ||
* Example: | ||
* | ||
* {~, capitalize, {0} } is the best! | ||
* | ||
* Result for "ice": Ice is the best! | ||
*/ | ||
object MessageTransformFormatters { | ||
val all = persistentListOf( | ||
UppercaseFormatter, LowercaseFormatter, CapitalizeFormatter | ||
) | ||
|
||
abstract class TransformFormatter(override val typeId: String) : MessageValueFormatter { | ||
|
||
override fun format( | ||
result: StringBuilder, | ||
value: Any?, | ||
style: StylePart?, | ||
parameters: List<Any>, | ||
locale: Locale, | ||
context: MessageFormatContext, | ||
) { | ||
val textToTransform = style?.toSimple()?.format(parameters, locale, context) | ||
?: return | ||
result.ensureCapacity(result.length + textToTransform.length) | ||
formatString(result, textToTransform) | ||
} | ||
|
||
protected abstract fun formatString( | ||
result: StringBuilder, | ||
value: CharSequence, | ||
) | ||
} | ||
|
||
object UppercaseFormatter : TransformFormatter("uppercase") { | ||
override fun formatString( | ||
result: StringBuilder, | ||
value: CharSequence, | ||
) { | ||
for (c in value) | ||
result.append(c.uppercaseChar()) | ||
} | ||
} | ||
|
||
object LowercaseFormatter : TransformFormatter("lowercase") { | ||
override fun formatString( | ||
result: StringBuilder, | ||
value: CharSequence, | ||
) { | ||
for (c in value) | ||
result.append(c.lowercaseChar()) | ||
} | ||
} | ||
|
||
object CapitalizeFormatter : TransformFormatter("capitalize") { | ||
override fun formatString( | ||
result: StringBuilder, | ||
value: CharSequence, | ||
) { | ||
if (value.isEmpty()) | ||
return | ||
result.append(value[0].uppercaseChar()) | ||
for (i in 1 until value.length) | ||
result.append(value[i]) | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
i18n4k-core/src/commonTest/kotlin/da/comahe/i18n4k/MessageNumberFormattersTest.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,58 @@ | ||
package da.comahe.i18n4k | ||
|
||
import de.comahe.i18n4k.Locale | ||
import de.comahe.i18n4k.config.I18n4kConfigDefault | ||
import de.comahe.i18n4k.i18n4k | ||
import de.comahe.i18n4k.messages.formatter.MessageFormatterDefault.format | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class MessageNumberFormattersTest { | ||
|
||
private var i18n4kConfig = I18n4kConfigDefault() | ||
|
||
@BeforeTest | ||
fun init() { | ||
i18n4k = i18n4kConfig | ||
i18n4kConfig.restoreDefaultSettings() | ||
} | ||
|
||
/** | ||
* Test formats of | ||
* [de.comahe.i18n4k.messages.formatter.types.MessageNumberFormatters] | ||
*/ | ||
@Test | ||
fun format_parameterNumberTest() { | ||
val locale = Locale("en") | ||
val params = listOf(12345.6789) | ||
|
||
assertEquals("12,345.6789 #", format("{0,number} #", params, locale)) | ||
assertEquals("12,345.68 #", format("{0,number, %.2} #", params, locale)) | ||
assertEquals("12,345.6789 #", format("{0,number, %.6} #", params, locale)) | ||
assertEquals("12,345.678900 #", format("{0,number, %.06} #", params, locale)) | ||
assertEquals("12.3456789 km #", format("{0,length} #", params, locale)) | ||
assertEquals("12.35 km #", format("{0,length, %.2} #", params, locale)) | ||
assertEquals("12.3456789 km #", format("{0,length, %.9} #", params, locale)) | ||
assertEquals("12.345678900 km #", format("{0,length, %.09} #", params, locale)) | ||
assertEquals("12,345.6789 m² #", format("{0,area} #", params, locale)) | ||
assertEquals("12,345.68 m² #", format("{0,area, %.2} #", params, locale)) | ||
assertEquals("12,345.6789 m² #", format("{0,area, %.6} #", params, locale)) | ||
assertEquals("12,345.678900 m² #", format("{0,area, %.06} #", params, locale)) | ||
assertEquals("12.3456789 kg #", format("{0,weight} #", params, locale)) | ||
assertEquals("12.35 kg #", format("{0,weight, %.2} #", params, locale)) | ||
assertEquals("12.3456789 kg #", format("{0,weight, %.9} #", params, locale)) | ||
assertEquals("12.345678900 kg #", format("{0,weight, %.09} #", params, locale)) | ||
assertEquals("3:25:46 h #", format("{0,timespan} #", params, locale)) | ||
|
||
|
||
} | ||
|
||
@Test | ||
fun format_parameterNumberTest_de() { | ||
val locale = Locale("de") | ||
val params = listOf(12345.6789) | ||
|
||
assertEquals("12.345,6789 #", format("{0,number} #", params, locale)) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
i18n4k-core/src/commonTest/kotlin/da/comahe/i18n4k/MessageTransformFormattersTest.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,46 @@ | ||
package da.comahe.i18n4k | ||
|
||
import de.comahe.i18n4k.Locale | ||
import de.comahe.i18n4k.config.I18n4kConfigDefault | ||
import de.comahe.i18n4k.i18n4k | ||
import de.comahe.i18n4k.messages.formatter.MessageFormatterDefault.format | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class MessageTransformFormattersTest { | ||
|
||
private var i18n4kConfig = I18n4kConfigDefault() | ||
|
||
@BeforeTest | ||
fun init() { | ||
i18n4k = i18n4kConfig | ||
i18n4kConfig.restoreDefaultSettings() | ||
} | ||
|
||
/** | ||
* Test formats of | ||
* [de.comahe.i18n4k.messages.formatter.types.MessageTransformFormatters] | ||
*/ | ||
@Test | ||
fun test_transform() { | ||
val locale = Locale("en") | ||
val params = listOf("hello WORLD") | ||
|
||
assertEquals("HELLO WORLD!", format("{~, uppercase, {0} }!", params, locale)) | ||
assertEquals("hello world!", format("{~, lowercase, {0} }!", params, locale)) | ||
assertEquals("Hello WORLD!", format("{~, capitalize, {0} }!", params, locale)) | ||
|
||
assertEquals("@hello WORLD!", format("{~, capitalize, @{0} }!", params, locale)) | ||
} | ||
|
||
@Test | ||
fun test_invalid() { | ||
val locale = Locale("en") | ||
val params = listOf("hello WORLD") | ||
|
||
assertEquals("!", format("{~, uppercase }!", params, locale)) | ||
assertEquals("{1}!", format("{~, lowercase, {1} }!", params, locale)) | ||
assertEquals("{1}!", format("{~, capitalize, {1} | {0} }!", params, locale)) | ||
} | ||
} |