Skip to content

Commit

Permalink
Merge branch 'master' into add-support-for-vocab-cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Nov 9, 2023
2 parents 4d92177 + f814929 commit 61880a8
Show file tree
Hide file tree
Showing 33 changed files with 3,218 additions and 2,474 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,38 @@ Tags on keys in other languages will be ignored.
[tags]: https://support.phrase.com/hc/en-us/articles/5822598372252-Tags-Strings-
[configuration]: #Configuration

#### Global key

`vocab push` and `vocab pull` can support global keys mapping. When you want certain translations to use a specific/custom key in Phrase, add the `globalKey` to the structure.

```jsonc
// translations.json
{
"Hello": {
"message": "Hello",
"globalKey": "hello"
},
"Goodbye": {
"message": "Goodbye",
"globalKey": "app.goodbye.label"
}
}
```

In the above example,

- `vocab push` will push the `hello` and `app.goodbye.label` keys to Phrase.
- `vocab pull` will pull translations from Phrase and map them to the `hello` and `app.goodbye.label` keys.

##### Error on no translation for global key

By default, `vocab pull` will not error if a translation is missing in Phrase for a translation with a global key.
If you want to throw an error in this situation, pass the `--error-on-no-global-key-translation` flag:

```sh
vocab pull --error-on-no-global-key-translation
```

## Troubleshooting

### Problem: Passed locale is being ignored or using en-US instead
Expand Down
4 changes: 2 additions & 2 deletions fixtures/direct/src/client.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { render } from 'react-dom';
import { createRoot } from 'react-dom/client';

import {
getSyncMessage,
Expand Down Expand Up @@ -74,4 +74,4 @@ const node = document.createElement('div');

document.body.appendChild(node);

render(<App />, node);
createRoot(node).render(<App />);
2 changes: 1 addition & 1 deletion fixtures/direct/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = ({ disableVocabPlugin }) => ({
exclude: [/node_modules/],
use: [
{
loader: 'babel-loader',
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: [
Expand Down
3 changes: 3 additions & 0 deletions fixtures/phrase/src/mytranslations.vocab/fr.translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
},
"world": {
"message": "monde"
},
"profile": {
"message": "profil"
}
}
7 changes: 7 additions & 0 deletions fixtures/phrase/src/mytranslations.vocab/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@
},
"world": {
"message": "world"
},
"thanks": {
"message": "Thanks",
"globalKey": "app.thanks.label"
},
"profile": {
"message": "profil"
}
}
1 change: 1 addition & 0 deletions fixtures/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@types/loadable__component": "^5.13.1",
"@types/loadable__server": "^5.12.3",
"@types/loadable__webpack-plugin": "^5.7.1",
"@types/node": "^18.11.9",
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.4",
"@types/start-server-webpack-plugin": "^2.2.0",
Expand Down
4 changes: 1 addition & 3 deletions fixtures/server/src/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const statsFile = __non_webpack_require__('../dist-client/loadable-stats.json');

import { App } from './App';

import express from 'express';

import type { Request, Response } from 'express';
import express, { type Request, type Response } from 'express';

const app = express();

Expand Down
4 changes: 2 additions & 2 deletions fixtures/server/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = () => [
exclude: [/node_modules/],
use: [
{
loader: 'babel-loader',
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
plugins: ['@loadable/babel-plugin'],
Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = () => [
exclude: [/node_modules/],
use: [
{
loader: 'babel-loader',
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
plugins: ['@loadable/babel-plugin'],
Expand Down
5 changes: 2 additions & 3 deletions fixtures/simple/src/client.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VocabProvider, useTranslations } from '@vocab/react';
import React, { type ReactNode, useState } from 'react';
import { render } from 'react-dom';
import { createRoot } from 'react-dom/client';

import commonTranslations from './.vocab';
import clientTranslations from './client.vocab';
Expand Down Expand Up @@ -67,9 +67,8 @@ const node = document.createElement('div');

document.body.appendChild(node);

render(
createRoot(node).render(
<App>
<Content />
</App>,
node,
);
2 changes: 1 addition & 1 deletion fixtures/simple/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = ({ disableVocabPlugin }) => ({
exclude: [/node_modules/],
use: [
{
loader: 'babel-loader',
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: [
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"lint:tsc": "tsc",
"release": "pnpm build && pnpm copy-readme-to-packages && changeset publish",
"version": "changeset version && pnpm install --lockfile-only",
"test": "jest",
"test:local": "PUPPETEER_EXPERIMENTAL_CHROMIUM_MAC_ARM=1 jest",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"copy-readme-to-packages": "ts-node scripts/copy-readme-to-packages",
"start-fixture": "ts-node test-helpers/src/start-fixture",
"run-server-fixture": "ts-node test-helpers/src/run-server-fixture",
Expand All @@ -46,17 +45,17 @@
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.0",
"@changesets/changelog-github": "^0.2.8",
"@changesets/changelog-github": "^0.4.0",
"@changesets/cli": "^2.26.0",
"@formatjs/intl-locale": "^2.4.47",
"@manypkg/cli": "^0.20.0",
"@manypkg/cli": "^0.21.0",
"@preconstruct/cli": "^2.3.0",
"@types/expect-puppeteer": "^5.0.3",
"@types/jest": "^29.4.0",
"@types/jest-environment-puppeteer": "^5.0.3",
"@types/node": "16.18.46",
"@types/node": "^18.11.9",
"@types/wait-on": "^5.3.1",
"babel-loader": "^9.1.2",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"eslint-config-seek": "^11.1.0",
"expect-puppeteer": "^7.0.1",
Expand All @@ -70,7 +69,7 @@
"typescript": "^4.9.5",
"webpack": "^5.37.0"
},
"packageManager": "pnpm@8.4.0",
"packageManager": "pnpm@8.9.2",
"pnpm": {
"overrides": {
"@vocab-fixtures/direct": "link:./fixtures/direct",
Expand All @@ -89,6 +88,7 @@
}
},
"volta": {
"node": "16.19.0"
"node": "18.18.2",
"pnpm": "8.9.2"
}
}
21 changes: 21 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# @vocab/cli

## 1.4.0

### Minor Changes

- [`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007) [#170](https://github.com/seek-oss/vocab/pull/170) Thanks [@jasoncheng-jora](https://github.com/jasoncheng-jora)! - Error on no translation for global key

By default, `vocab pull` will not error if a translation is missing in Phrase for a translation with a global key.
If you want to throw an error in this situation, pass the `--error-on-no-global-key-translation` flag:

**EXAMPLE USAGE**:

```sh
vocab pull --error-on-no-global-key-translation
```

### Patch Changes

- Updated dependencies [[`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007), [`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007)]:
- @vocab/phrase@1.3.0
- @vocab/core@1.5.0

## 1.3.7

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vocab/cli",
"version": "1.3.7",
"version": "1.4.0",
"main": "dist/vocab-cli.cjs.js",
"module": "dist/vocab-cli.esm.js",
"bin": {
Expand All @@ -14,8 +14,8 @@
"license": "MIT",
"dependencies": {
"@types/env-ci": "^3.1.0",
"@vocab/core": "^1.4.0",
"@vocab/phrase": "^1.2.8",
"@vocab/core": "^1.5.0",
"@vocab/phrase": "^1.3.0",
"env-ci": "^5.0.2",
"fast-glob": "^3.2.4",
"form-data": "^3.0.0",
Expand Down
14 changes: 11 additions & 3 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-console */
import type { UserConfig } from '@vocab/core';
import { pull, push } from '@vocab/phrase';
import { resolveConfig, compile, validate } from '@vocab/core';
import { type UserConfig, resolveConfig, compile, validate } from '@vocab/core';
import yargs from 'yargs';

import envCi from 'env-ci';
Expand Down Expand Up @@ -44,7 +43,16 @@ yargs(process.argv.slice(2))
})
.command({
command: 'pull',
builder: () => yargs.options({ branch: branchDefinition }),
builder: () =>
yargs.options({
branch: branchDefinition,
'error-on-no-global-key-translation': {
type: 'boolean',
describe:
'Throw an error when there is no translation for a global key',
default: false,
},
}),
handler: async (options) => {
await pull(options, config!);
},
Expand Down
27 changes: 27 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# @vocab/core

## 1.5.0

### Minor Changes

- [`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007) [#170](https://github.com/seek-oss/vocab/pull/170) Thanks [@jasoncheng-jora](https://github.com/jasoncheng-jora)! - `vocab push` and `vocab pull` can support global keys mapping. When you want certain translations to use a specific/custom key in Phrase, add the `globalKey` to the structure.

**EXAMPLE USAGE**:

```jsonc
// translations.json
{
"Hello": {
"message": "Hello",
"globalKey": "hello"
},
"Goodbye": {
"message": "Goodbye",
"globalKey": "app.goodbye.label"
}
}
```

In the above example,

- `vocab push` will push the `hello` and `app.goodbye.label` keys to Phrase.
- `vocab pull` will pull translations from Phrase and map them to the `hello` and `app.goodbye.label` keys.

## 1.4.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vocab/core",
"version": "1.4.0",
"version": "1.5.0",
"main": "dist/vocab-core.cjs.js",
"module": "dist/vocab-core.esm.js",
"exports": {
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/load-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ export async function loadAllTranslations(
);
}
keys.add(uniqueKey);

const globalKey =
loadedTranslation.languages[config.devLanguage][key].globalKey;
if (globalKey) {
if (keys.has(globalKey)) {
throw new Error(
`Duplicate keys found. Key with global key ${globalKey} and key ${key} was found multiple times`,
);
}
keys.add(globalKey);
}
}
}
return result;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export interface TranslationData {
message: TranslationMessage;
description?: string;
tags?: Tags;
globalKey?: string;
}

export type TranslationsByKey<Key extends TranslationKey = string> = Record<
Expand Down
50 changes: 50 additions & 0 deletions packages/phrase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
# @vocab/phrase

## 1.3.0

### Minor Changes

- [`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007) [#170](https://github.com/seek-oss/vocab/pull/170) Thanks [@jasoncheng-jora](https://github.com/jasoncheng-jora)! - `vocab push` and `vocab pull` can support global keys mapping. When you want certain translations to use a specific/custom key in Phrase, add the `globalKey` to the structure.

**EXAMPLE USAGE**:

```jsonc
// translations.json
{
"Hello": {
"message": "Hello",
"globalKey": "hello"
},
"Goodbye": {
"message": "Goodbye",
"globalKey": "app.goodbye.label"
}
}
```

In the above example,

- `vocab push` will push the `hello` and `app.goodbye.label` keys to Phrase.
- `vocab pull` will pull translations from Phrase and map them to the `hello` and `app.goodbye.label` keys.

- [`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007) [#170](https://github.com/seek-oss/vocab/pull/170) Thanks [@jasoncheng-jora](https://github.com/jasoncheng-jora)! - Add an optional `errorOnNoGlobalKeyTranslation` flag to `pull` function. If set to `true`, it will error if a translation is missing in Phrase for a translation with a global key.

**EXAMPLE USAGE**:

```js
import { pull } from '@vocab/phrase';

const vocabConfig = {
devLanguage: 'en',
language: ['en', 'fr'],
};

await pull(
{ branch: 'myBranch', errorOnNoGlobalKeyTranslation: true },
vocabConfig,
);
```

### Patch Changes

- Updated dependencies [[`161d698`](https://github.com/seek-oss/vocab/commit/161d698f7fd198f594a765104f02261d2e45f007)]:
- @vocab/core@1.5.0

## 1.2.8

### Patch Changes
Expand Down
Loading

0 comments on commit 61880a8

Please sign in to comment.