Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 29 - Handle multiple YML files for translations #88

Closed
wants to merge 7 commits into from

Conversation

amerharb
Copy link
Collaborator

No description provided.

@amerharb amerharb changed the title refactor type TranslationMap Issue 29 - Handle multiple YML files for translations May 26, 2024
Comment on lines +43 to 46
const output: MessageMap = {};
Object.entries(language).forEach(([key, value]) => {
output[key] = value.text;
output[key] = value;
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cloning is no longer necessary as language is already a MessageMap.

Comment on lines 20 to +22
de: {
'greeting.headline': {
sourceFile: '',
sourceFile: 'de-with_extra_text.yaml',
Copy link
Collaborator

@WULCAN WULCAN Jun 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A sourceFile under 'de' with a value not starting with 'de.' is unrealistic.

The first dot-separated segment of the last portion of the file's path is used as language identifier by the real YamlTranslationAdapter.

See also the test can update translations before getTranslations() below.

Comment on lines -67 to +74
expect(before).toEqual({
'greeting.headline': 'Hallo',
});
expect(before).toEqual('Hallo');

expect(after).toEqual({
'greeting.headline': 'Hallo!',
});
expect(after).toEqual('Hallo!');
Copy link
Collaborator

@WULCAN WULCAN Jun 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test also covered that updateTranslation does not add any other properties to the objects in the maps returned by getTranslations. TypeScript already enforces that so there is no problem with losing that coverage here.

This is just a note from my review that I thought might be interesting. You can resolve the conversation when you have read it.

Comment on lines +78 to +79
const sourceFileDotCount = sourceFile.replace('/', '.').split('.').length - 1;
const msgIdArray = msgId.split('.').splice(sourceFileDotCount);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code overestimates how much of the message id comes from the path. The filename --- the last segment of the path, typically contains a dot, for example in 'sv.yml' but can in theory contain more dots too, for example 'sv.skånska.yaml'.

For example getShortId('a.b.c', 'a/sv.yml') should return 'b.c' but this implementation will return just 'c'.

}

function getShortId(msgId: string, sourceFile: string): string {
const sourceFileDotCount = sourceFile.replace('/', '.').split('.').length - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.prototype.replace only replaces the first occurrence if pattern is a string.

For example getShortId('a.b.c.d', 'a/b/c/sv.yml') should return just 'd' but this implementation returns 'c.d'.

Fortunately, there is another function replaceAll that works as expected.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

Comment on lines +77 to +81
function getShortId(msgId: string, sourceFile: string): string {
const sourceFileDotCount = sourceFile.replace('/', '.').split('.').length - 1;
const msgIdArray = msgId.split('.').splice(sourceFileDotCount);
return msgIdArray.join('.');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is complex enough that it should have a somewhat extensive test suite.

@@ -53,32 +56,30 @@ describe('ProjectStore', () => {
getTranslations: async () => ({
de: {
'greeting.headline': {
sourceFile: '',
sourceFile: 'anything',
Copy link
Collaborator

@WULCAN WULCAN Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A sourceFile under locale identifier de with a value not starting with de. is unrealistic.

The first dot-separated segment of the last portion of the file's path is used as locale identifier by the real YamlTranslationAdapter.

Suggested change
sourceFile: 'anything',
sourceFile: 'de.yaml',

@amerharb amerharb closed this Aug 17, 2024
@WULCAN WULCAN deleted the issue-29-multi-yaml branch September 29, 2024 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

language file yml or yaml Handle multiple YML files for translations when writing
2 participants