Skip to content

Commit

Permalink
Merge branch 'global-key-support' into global-key-push-translations
Browse files Browse the repository at this point in the history
# Conflicts:
#	fixtures/phrase/src/mytranslations.vocab/translations.json
  • Loading branch information
adriankencana committed Oct 20, 2023
2 parents 0730293 + 41532d9 commit e7de704
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 32 deletions.
15 changes: 15 additions & 0 deletions .changeset/nasty-seahorses-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@vocab/phrase': minor
'@vocab/core': minor
---

Vocab pull: support custom key

```json
{
"thanks": {
"message": "Thanks",
"global-key": "app.thanks.label"
}
}
```
6 changes: 3 additions & 3 deletions fixtures/phrase/src/mytranslations.vocab/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"world": {
"message": "world"
},
"profile": {
"message": "profile",
"global-key": "app.common.profile"
"thanks": {
"message": "Thanks",
"global-key": "app.thanks.label"
}
}
2 changes: 2 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,5 @@ export function resolveConfigSync(

return null;
}

export const GLOBAL_KEY = 'global-key';
12 changes: 12 additions & 0 deletions packages/core/src/load-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getDevTranslationFileGlob,
} from './utils';
import { generateLanguageFromTranslations } from './generate-language';
import { GLOBAL_KEY } from './config';

export function getUniqueKey(key: string, namespace: string) {
return `${key}.${namespace}`;
Expand Down Expand Up @@ -410,6 +411,17 @@ export async function loadAllTranslations(
);
}
keys.add(uniqueKey);

const globalKey =
loadedTranslation.languages[config.devLanguage][key][GLOBAL_KEY];
if (globalKey) {
if (keys.has(globalKey)) {
throw new Error(
`Duplicate keys found. Global key ${key} was found multiple times`,
);
}
keys.add(globalKey);
}
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ export interface TranslationFileMetadata {
}

export interface TranslationData {
'global-key'?: string;
message: TranslationMessage;
description?: string;
tags?: Tags;
'global-key'?: string;
}

export type TranslationsByKey<Key extends TranslationKey = string> = Record<
Expand Down
1 change: 1 addition & 0 deletions packages/phrase/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GLOBAL_KEY = 'global-key';
2 changes: 0 additions & 2 deletions packages/phrase/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export { pull } from './pull-translations';
export { push } from './push-translations';

export const GLOBAL_KEY = 'global-key';
17 changes: 17 additions & 0 deletions packages/phrase/src/pull-translations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ describe('pull translations', () => {
'hello.mytranslations': {
message: 'Hi there',
},
'app.thanks.label': {
message: 'Thank you.',
},
},
fr: {
'hello.mytranslations': {
message: 'merci',
},
'app.thanks.label': {
message: 'Merci.',
},
},
}),
);
Expand Down Expand Up @@ -98,6 +104,10 @@ describe('pull translations', () => {
"greeting",
],
},
"thanks": {
"global-key": "app.thanks.label",
"message": "Thank you.",
},
"world": {
"message": "world",
},
Expand All @@ -106,6 +116,9 @@ describe('pull translations', () => {
"hello": {
"message": "merci",
},
"thanks": {
"message": "Merci.",
},
"world": {
"message": "monde",
},
Expand Down Expand Up @@ -182,6 +195,10 @@ describe('pull translations', () => {
"greeting",
],
},
"thanks": {
"global-key": "app.thanks.label",
"message": "Thanks",
},
"world": {
"message": "world",
},
Expand Down
8 changes: 6 additions & 2 deletions packages/phrase/src/pull-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { TranslationFileContents, UserConfig } from '@vocab/core';

import { pullAllTranslations, ensureBranch } from './phrase-api';
import { trace } from './logger';
import { GLOBAL_KEY } from './config';

interface PullOptions {
branch?: string;
Expand Down Expand Up @@ -61,7 +62,8 @@ export async function pull(
defaultValues[key] = {
...defaultValues[key],
...allPhraseTranslations[config.devLanguage][
getUniqueKey(key, loadedTranslation.namespace)
defaultValues[key][GLOBAL_KEY] ??
getUniqueKey(key, loadedTranslation.namespace)
],
};
}
Expand All @@ -85,7 +87,9 @@ export async function pull(
allPhraseTranslations[alternativeLanguage];

for (const key of localKeys) {
const phraseKey = getUniqueKey(key, loadedTranslation.namespace);
const phraseKey =
defaultValues[key][GLOBAL_KEY] ??
getUniqueKey(key, loadedTranslation.namespace);
const phraseTranslationMessage =
phraseAltTranslations[phraseKey]?.message;

Expand Down
36 changes: 14 additions & 22 deletions packages/phrase/src/push-translations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ describe('push', () => {
.toMatchInlineSnapshot(`
{
"en": {
"app.common.profile": {
"global-key": "app.common.profile",
"message": "profile",
"hello.mytranslations": {
"message": "Hello",
"tags": [
"only for this key",
"greeting",
"every",
"key",
"gets",
"these",
"tags",
],
},
"hello.mytranslations": {
"message": "Hello",
"thanks.mytranslations": {
"global-key": "app.thanks.label",
"message": "Thanks",
"tags": [
"only for this key",
"greeting",
"every",
"key",
"gets",
Expand All @@ -98,10 +98,6 @@ describe('push', () => {
},
},
"fr": {
"app.common.profile": {
"description": undefined,
"message": "profil",
},
"hello.mytranslations": {
"description": undefined,
"message": "Bonjour",
Expand Down Expand Up @@ -149,22 +145,22 @@ describe('push', () => {
.toMatchInlineSnapshot(`
{
"en": {
"app.common.profile": {
"global-key": "app.common.profile",
"message": "profile",
"hello.mytranslations": {
"message": "Hello",
"tags": [
"only for this key",
"greeting",
"every",
"key",
"gets",
"these",
"tags",
],
},
"hello.mytranslations": {
"message": "Hello",
"thanks.mytranslations": {
"global-key": "app.thanks.label",
"message": "Thanks",
"tags": [
"only for this key",
"greeting",
"every",
"key",
"gets",
Expand All @@ -184,10 +180,6 @@ describe('push', () => {
},
},
"fr": {
"app.common.profile": {
"description": undefined,
"message": "profil",
},
"hello.mytranslations": {
"description": undefined,
"message": "Bonjour",
Expand Down
3 changes: 1 addition & 2 deletions packages/phrase/src/push-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
pushTranslations,
} from './phrase-api';
import { trace } from './logger';
import { loadAllTranslations, getUniqueKey } from '@vocab/core';
import { GLOBAL_KEY } from '.';
import { loadAllTranslations, getUniqueKey, GLOBAL_KEY } from '@vocab/core';

Check failure on line 13 in packages/phrase/src/push-translations.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

Module '"@vocab/core"' has no exported member 'GLOBAL_KEY'.

Check failure on line 13 in packages/phrase/src/push-translations.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

Module '"@vocab/core"' has no exported member 'GLOBAL_KEY'.

interface PushOptions {
branch: string;
Expand Down

0 comments on commit e7de704

Please sign in to comment.