Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Chore: introduce compound text keys #116

Draft
wants to merge 1 commit into
base: release/3.3.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/ccl/ccl-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ const mapEntryToTextDescriptor = languageCode => (it, idx, arr) => {
}

const name = it.attributes.name
const [textType] = commentEntry.value.trim().split(':')
const descriptor = {
name
name,
textType
}
if (it.tag === 'string') {
descriptor.type = 'string'
Expand All @@ -71,6 +73,25 @@ const mapEntryToTextDescriptor = languageCode => (it, idx, arr) => {
}
}

/*
* Resolves placeholders in text keys of text type `NOTR`.
* The placeholder must be in the format `${OTHER_TEXT_KEY}`.
*/
const resolveCompoundTexts = languageCode => (it, idx, arr) => {
if (it.descriptor?.textType !== 'NOTR') return it
const text = it.descriptor.localizedText[languageCode]
const newText = text.replace(/\${[^}]+}/mg, match => {
const textKey = match.replace('${', '').replace('}', '')
const textDescriptor = arr.find(it => it.descriptor?.name === textKey)
if (!textDescriptor) {
throw new Error(`Cannot resolve text key ${textKey} in compound text ${it.descriptor.name}`)
}
return textDescriptor.descriptor.localizedText[languageCode]
})
it.descriptor.localizedText[languageCode] = newText
return it
}

const sortByName = (a, b) => a.name < b.name ? -1 : 1

const getTextDescriptorsFromXml = (xmlData, languageCode) => {
Expand All @@ -89,6 +110,7 @@ const getTextDescriptorsFromXml = (xmlData, languageCode) => {

const textDescriptors = entries
.map(mapEntryToTextDescriptor(languageCode))
.map(resolveCompoundTexts(languageCode))
.filter(it => it.tag !== xmlCommentPropName)

return textDescriptors
Expand Down