Skip to content

Commit

Permalink
Add special error message for lang file decoding edge case, improve l…
Browse files Browse the repository at this point in the history
…ang file encoder constructor
  • Loading branch information
floscher committed May 15, 2022
1 parent dda1246 commit a0def29
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package org.openstreetmap.josm.gradle.plugin.i18n.io
public class LangFileDecoder(baseLanguageBytes: ByteArray): I18nFileDecoder {

public companion object {
private const val ERROR_MESSAGE_NONBASE_UNTRANSLATED = "This is not a base language!" +
private const val ERROR_MESSAGE_NONBASE_UNTRANSLATED = "This is not a base language! " +
"There is a string in this supposed base language that indicates it is not translated from the base language (which does not make sense)."
private const val ERROR_MESSAGE_NONBASE_SAME_AS_BASE = "This is not a base language!" +
private const val ERROR_MESSAGE_NONBASE_SAME_AS_BASE = "This is not a base language! " +
"There is a string in this supposed base language that indicates it is the same as in the base language (which does not make sense)."

/**
Expand Down Expand Up @@ -102,6 +102,9 @@ public class LangFileDecoder(baseLanguageBytes: ByteArray): I18nFileDecoder {
resultList.add(transformToListElement(currentBaseString, null))
}
else -> {
require(currentByteIndex + stringLength <= bytes.size) {
"String length is too long! $stringLength bytes starting at offset $currentByteIndex does not fit in a file of ${bytes.size} bytes"
}
val msgstr = MsgStr(bytes.sliceArray(currentByteIndex until currentByteIndex + stringLength).decodeToString())
if (currentBaseString == null) {
resultList.add(transformToListElement(MsgId(msgstr), null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ package org.openstreetmap.josm.gradle.plugin.i18n.io
* Encoder for the *.lang file format used by JOSM.
* @param baseMsgIds the [MsgId]s in the base language for which translations will be encoded
*/
public class LangFileEncoder(baseMsgIds: List<MsgId>): I18nFileEncoder {
public class LangFileEncoder(
singularMsgIds: List<MsgId>,
private val pluralMsgIds: List<MsgId>
): I18nFileEncoder {
private val singularMsgIds = singularMsgIds.minus(GETTEXT_HEADER_MSGID)
public companion object {
/**
* The language code `en` for English, which is considered to be the base language by default,
Expand All @@ -15,14 +19,13 @@ public class LangFileEncoder(baseMsgIds: List<MsgId>): I18nFileEncoder {
private val SINGULAR_PLURAL_SEPARATOR: List<Byte> = listOf(0xFF, 0xFF).map { it.toByte() }
}

private val singularMsgIds: List<MsgId>
private val pluralMsgIds: List<MsgId>

init {
val baseMsgIdPartitions = baseMsgIds.distinct().minus(GETTEXT_HEADER_MSGID).sorted().partition { it.id.strings.size <= 1 }
singularMsgIds = baseMsgIdPartitions.first
pluralMsgIds = baseMsgIdPartitions.second
}
private constructor(singularPluralMsgIds: Pair<List<MsgId>, List<MsgId>>): this(
singularPluralMsgIds.first,
singularPluralMsgIds.second
)
public constructor(baseMsgIds: Collection<MsgId>): this(
baseMsgIds.distinct().sorted().partition { it.id.strings.size <= 1 }
)

/**
* Converts the [MsgId]s of the base language (see constructor parameter) to the *.lang file format.
Expand Down

0 comments on commit a0def29

Please sign in to comment.