Skip to content

Commit

Permalink
feat: Handle multiple ":" in translation keys
Browse files Browse the repository at this point in the history
closes #73
  • Loading branch information
freakzlike committed Dec 3, 2024
1 parent b7b4100 commit 5ca8ec5
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion bin/extract.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ if (!args.length || !args[0]) {
}

const options = require(path.resolve(process.cwd(), args[0]))
i18nExtract(options)
i18nExtract(options).then(result => {
if (!result) {
process.exit(1)
}
})
5 changes: 5 additions & 0 deletions examples/namespaces/src/typescript-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ $t('context.key_2')
$t('context.nested.key')
$t('new_key')

/**
* Invalid usages
*/
$t('common:invalid:nested:key')

/**
* Not parse this
*/
Expand Down
8 changes: 6 additions & 2 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ export const i18nCheck = async (

const checkUntranslated = async (translationResults: TranslationMapWrite): Promise<boolean> => {
let hasUntranslated = false
let hasOtherError = false
for (const languageTranslations of Object.values(translationResults)) {
for (const { filePath, untranslatedCount} of Object.values(languageTranslations)) {
for (const { filePath, untranslatedCount, otherError } of Object.values(languageTranslations)) {
if (untranslatedCount) {
hasUntranslated = true
console.log(`${filePath} has untranslated messages!`)
}
if (otherError) {
hasOtherError = true
}
}
}
return !hasUntranslated
return !hasUntranslated && !hasOtherError
}
2 changes: 2 additions & 0 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const i18nExtract = async (

const translationResults = await generateNewTranslations(translationKeys, existingTranslations, options)
await writeTranslations(translationResults)

return true
}

export const writeTranslations = async (
Expand Down
5 changes: 3 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const parseFiles = async (
await Promise.all(files.map(async filePath => {
const fileResults = await parseFile(options, filePath)
fileResults.forEach(fullKey => {
const [namespace, key] = fullKey.includes(':')
? fullKey.split(':', 2)
const namespaceIndex = fullKey.indexOf(':')
const [namespace, key] = namespaceIndex > 0
? [fullKey.substring(0, namespaceIndex), fullKey.substring(namespaceIndex + 1)]
: [defaultNamespace, fullKey]

if (!results[namespace]) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface TranslationResult {

export interface TranslationResultWrite extends TranslationResult {
untranslatedCount: number
otherError?: boolean
}

export type TranslationMap <T extends TranslationResult = TranslationResult> = Record<Language, Record<Namespace, T & { filePath: string}>>
Expand Down
2 changes: 2 additions & 0 deletions tests/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('i18nExtract', () => {
key: 'Nested Key DE'
}
},
'invalid:nested:key': '__MISSING_TRANSLATION__',
key_1: 'Key 1 DE',
key_2: 'Key 2 DE',
key_3: 'Key 3 DE',
Expand All @@ -60,6 +61,7 @@ describe('i18nExtract', () => {
key: 'Nested Key EN'
}
},
'invalid:nested:key': '__MISSING_TRANSLATION__',
key_1: 'Key 1 EN',
key_2: 'Key 2 EN',
key_3: 'Key 3 EN',
Expand Down
3 changes: 3 additions & 0 deletions tests/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('parseFiles', () => {
'context.key_1',
'context.key_2',
'context.nested.key',
'invalid:nested:key',
'key_1',
'key_2',
'key_3',
Expand Down Expand Up @@ -56,6 +57,7 @@ describe('parseFiles', () => {
'new_key'
])
expect(results.common).toStrictEqual([
'invalid:nested:key',
'key_2'
])
expect(results.other).toStrictEqual([
Expand Down Expand Up @@ -153,6 +155,7 @@ describe('parseFile', () => {
it('should parse translations of typescript file', async () => {
const filePath = path.join(__dirname, '../examples/namespaces/src/typescript-file.ts')
expect(await parseFile(options, filePath)).toStrictEqual(new Set([
'common:invalid:nested:key',
'key_1',
'common:key_2',
'other:key_1',
Expand Down

0 comments on commit 5ca8ec5

Please sign in to comment.