-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07d38da
commit 5d82381
Showing
7 changed files
with
158 additions
and
10 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
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
56 changes: 56 additions & 0 deletions
56
src/plugins/piral-translate/src/flatten-translations.test.ts
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,56 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { describe, it, expect } from 'vitest'; | ||
import { flattenTranslations } from './flatten-translations'; | ||
|
||
describe('Flatten translations', () => { | ||
it('flattenTranslations can handle flat keys', () => { | ||
const messages = { | ||
en: { | ||
key: 'value' | ||
}, | ||
fr: { | ||
key: 'value (fr)' | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.fr.key).toEqual('value (fr)'); | ||
}); | ||
|
||
it('flattenTranslations can handle flat dot keys', () => { | ||
const messages = { | ||
en: { | ||
'header.title': 'Hello world' | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.en['header.title']).toBe('Hello world'); | ||
}); | ||
|
||
it('flattenTranslations can handle nested keys', () => { | ||
const messages = { | ||
en: { | ||
header: { | ||
title: 'Hello world' | ||
} | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.en['header.title']).toBe('Hello world'); | ||
}); | ||
|
||
it('flattenTranslations can handle nested keys with multiple depth levels', () => { | ||
const messages = { | ||
en: { | ||
header: { | ||
title: { | ||
subtitle: 'Hello world' | ||
} | ||
} | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.en['header.title.subtitle']).toBe('Hello world'); | ||
}); | ||
}); |
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 @@ | ||
import { LocalizationMessages, NestedLocalizationMessages } from './types'; | ||
|
||
export function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages { | ||
return Object.fromEntries( | ||
Object | ||
.entries(messages) | ||
.map(([ language, translations ]) => { | ||
return [ | ||
language, | ||
flat(translations) | ||
] | ||
}) | ||
); | ||
} | ||
|
||
function flat(source: Record<string, unknown>): Record<string, string> { | ||
const target: Record<string, string> = {}; | ||
|
||
flatten(source, target); | ||
|
||
return target; | ||
} | ||
|
||
function flatten(source: any, target: Record<string, string>, prop = '') { | ||
if (typeof source === 'string') { | ||
target[prop] = source; | ||
|
||
return; | ||
} | ||
|
||
if (typeof source === 'object' && source !== null) { | ||
Object | ||
.keys(source) | ||
.forEach((key) => { | ||
flatten( | ||
source[key], | ||
target, | ||
prop | ||
? `${prop}.${key}` | ||
: key | ||
); | ||
}); | ||
|
||
return; | ||
} | ||
} |
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
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
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