@vocab/phrase
Advanced tools
Comparing version 0.0.3 to 0.0.4
# @vocab/phrase | ||
## 0.0.4 | ||
### Patch Changes | ||
- [`5f5c581`](https://github.com/seek-oss/vocab/commit/5f5c581a65bff28729ee19e1ec0bdea488a9d6c2) [#19](https://github.com/seek-oss/vocab/pull/19) Thanks [@jahredhope](https://github.com/jahredhope)! - Compile useable TypeScript importable files with `vocab compile`. | ||
The new `vocab compile` step replaces `vocab generate-types` in creating a fully functional **translations.ts** file. | ||
This allows vocab to be used **without the Webpack Plugin**, however use of the plugin is still heavily advised to ensure optimal loading of translation content on the web. | ||
Support for unit testing is now better than ever! The newly created **translations.ts** means your unit test code will see the same code as available while rendering. | ||
See the [documentation](https://github.com/seek-oss/vocab) for further usage details. | ||
* [`02f943c`](https://github.com/seek-oss/vocab/commit/02f943ca892913b41f9e4720a72400777cf14b3d) [#17](https://github.com/seek-oss/vocab/pull/17) Thanks [@jahredhope](https://github.com/jahredhope)! - Add additional debug traces | ||
* Updated dependencies [[`5f5c581`](https://github.com/seek-oss/vocab/commit/5f5c581a65bff28729ee19e1ec0bdea488a9d6c2), [`02f943c`](https://github.com/seek-oss/vocab/commit/02f943ca892913b41f9e4720a72400777cf14b3d)]: | ||
- @vocab/core@0.0.4 | ||
- @vocab/types@0.0.5 | ||
## 0.0.3 | ||
@@ -4,0 +24,0 @@ |
@@ -0,3 +1,7 @@ | ||
import { TranslationsByKey } from './../../types/src/index'; | ||
import type { TranslationsByLanguage } from '@vocab/types'; | ||
import fetch from 'node-fetch'; | ||
export declare function callPhrase(relativePath: string, options?: Parameters<typeof fetch>[1]): Promise<any>; | ||
export declare function pullAllTranslations(branch: string): Promise<TranslationsByLanguage>; | ||
export declare function pushTranslationsByLocale(contents: TranslationsByKey, locale: string, branch: string): Promise<void>; | ||
export declare function ensureBranch(branch: string): Promise<void>; |
@@ -8,6 +8,6 @@ 'use strict'; | ||
var core = require('@vocab/core'); | ||
var FormData = require('form-data'); | ||
var fetch = require('node-fetch'); | ||
var chalk = require('chalk'); | ||
var debug = require('debug'); | ||
var FormData = require('form-data'); | ||
@@ -17,7 +17,10 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } | ||
var path__default = /*#__PURE__*/_interopDefault(path); | ||
var FormData__default = /*#__PURE__*/_interopDefault(FormData); | ||
var fetch__default = /*#__PURE__*/_interopDefault(fetch); | ||
var chalk__default = /*#__PURE__*/_interopDefault(chalk); | ||
var debug__default = /*#__PURE__*/_interopDefault(debug); | ||
var FormData__default = /*#__PURE__*/_interopDefault(FormData); | ||
const mkdir = fs.promises.mkdir; | ||
const writeFile = fs.promises.writeFile; | ||
const trace = debug__default['default'](`vocab:phrase`); | ||
@@ -29,4 +32,2 @@ const log = (...params) => { | ||
/* eslint-disable no-console */ | ||
function _callPhrase(path, options = {}) { | ||
@@ -99,16 +100,3 @@ const phraseApiToken = process.env.PHRASE_API_TOKEN; | ||
} | ||
async function ensureBranch(branch) { | ||
await callPhrase(`branches`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
name: branch | ||
}) | ||
}); | ||
trace('Created branch:', branch); | ||
} | ||
async function getAllTranslationsFromPhrase(branch) { | ||
async function pullAllTranslations(branch) { | ||
const phraseResult = await callPhrase(`translations?branch=${branch}&per_page=100`); | ||
@@ -129,2 +117,32 @@ const translations = {}; | ||
} | ||
async function pushTranslationsByLocale(contents, locale, branch) { | ||
const formData = new FormData__default['default'](); | ||
const fileContents = Buffer.from(JSON.stringify(contents)); | ||
formData.append('file', fileContents, { | ||
contentType: 'application/json', | ||
filename: `${locale}.json` | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData | ||
}); | ||
log('Successfully Uploaded:', locale, '\n'); | ||
} | ||
async function ensureBranch(branch) { | ||
await callPhrase(`branches`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
name: branch | ||
}) | ||
}); | ||
trace('Created branch:', branch); | ||
} | ||
@@ -134,5 +152,7 @@ async function pull({ | ||
}, config) { | ||
trace(`Pulling translations from branch ${branch}`); | ||
await ensureBranch(branch); | ||
const alternativeLanguages = core.getAltLanguages(config); | ||
const allPhraseTranslations = await getAllTranslationsFromPhrase(branch); | ||
const allPhraseTranslations = await pullAllTranslations(branch); | ||
trace(`Pulling translations from Phrase for languages ${config.devLanguage} and ${alternativeLanguages.join(', ')}`); | ||
const allVocabTranslations = await core.loadAllTranslations({ | ||
@@ -143,3 +163,3 @@ fallbacks: 'none' | ||
for (const loadedTranslation of allVocabTranslations) { | ||
const devTranslations = loadedTranslation.languages.get(config.devLanguage); | ||
const devTranslations = loadedTranslation.languages[config.devLanguage]; | ||
@@ -160,6 +180,6 @@ if (!devTranslations) { | ||
await fs.promises.writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`); | ||
await writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`); | ||
for (const alternativeLanguage of alternativeLanguages) { | ||
const altTranslations = { ...loadedTranslation.languages.get(alternativeLanguage) | ||
const altTranslations = { ...loadedTranslation.languages[alternativeLanguage] | ||
}; | ||
@@ -184,7 +204,7 @@ const phraseAltTranslations = allPhraseTranslations[alternativeLanguage]; | ||
const altTranslationFilePath = core.getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage, config); | ||
await fs.promises.mkdir(path__default['default'].dirname(altTranslationFilePath), { | ||
const altTranslationFilePath = core.getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage); | ||
await mkdir(path__default['default'].dirname(altTranslationFilePath), { | ||
recursive: true | ||
}); | ||
await fs.promises.writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`); | ||
await writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`); | ||
} | ||
@@ -194,20 +214,2 @@ } | ||
async function uploadFile(contents, locale_id, branch) { | ||
const formData = new FormData__default['default'](); | ||
formData.append('file', Buffer.from(JSON.stringify(contents)), { | ||
contentType: 'application/json', | ||
filename: `${locale_id}.json` | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale_id); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale_id); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData | ||
}); | ||
log('Successfully Uploaded:', locale_id, '\n'); | ||
} | ||
/** | ||
@@ -225,2 +227,3 @@ * Uploading to the Phrase API for each language. Adding a unique namespace to each key using file path they key came from | ||
await ensureBranch(branch); | ||
trace(`Pushing translations to phrase for languages ${allLanguages.join(', ')}`); | ||
const phraseTranslations = {}; | ||
@@ -230,3 +233,3 @@ | ||
for (const language of allLanguages) { | ||
const localTranslations = loadedTranslation.languages.get(language); | ||
const localTranslations = loadedTranslation.languages[language]; | ||
@@ -250,3 +253,3 @@ if (!localTranslations) { | ||
if (phraseTranslations[language]) { | ||
await uploadFile(phraseTranslations[language], language, branch); | ||
await pushTranslationsByLocale(phraseTranslations[language], language, branch); | ||
} | ||
@@ -253,0 +256,0 @@ } |
@@ -8,6 +8,6 @@ 'use strict'; | ||
var core = require('@vocab/core'); | ||
var FormData = require('form-data'); | ||
var fetch = require('node-fetch'); | ||
var chalk = require('chalk'); | ||
var debug = require('debug'); | ||
var FormData = require('form-data'); | ||
@@ -17,7 +17,10 @@ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } | ||
var path__default = /*#__PURE__*/_interopDefault(path); | ||
var FormData__default = /*#__PURE__*/_interopDefault(FormData); | ||
var fetch__default = /*#__PURE__*/_interopDefault(fetch); | ||
var chalk__default = /*#__PURE__*/_interopDefault(chalk); | ||
var debug__default = /*#__PURE__*/_interopDefault(debug); | ||
var FormData__default = /*#__PURE__*/_interopDefault(FormData); | ||
const mkdir = fs.promises.mkdir; | ||
const writeFile = fs.promises.writeFile; | ||
const trace = debug__default['default'](`vocab:phrase`); | ||
@@ -29,4 +32,2 @@ const log = (...params) => { | ||
/* eslint-disable no-console */ | ||
function _callPhrase(path, options = {}) { | ||
@@ -99,16 +100,3 @@ const phraseApiToken = process.env.PHRASE_API_TOKEN; | ||
} | ||
async function ensureBranch(branch) { | ||
await callPhrase(`branches`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
name: branch | ||
}) | ||
}); | ||
trace('Created branch:', branch); | ||
} | ||
async function getAllTranslationsFromPhrase(branch) { | ||
async function pullAllTranslations(branch) { | ||
const phraseResult = await callPhrase(`translations?branch=${branch}&per_page=100`); | ||
@@ -129,2 +117,32 @@ const translations = {}; | ||
} | ||
async function pushTranslationsByLocale(contents, locale, branch) { | ||
const formData = new FormData__default['default'](); | ||
const fileContents = Buffer.from(JSON.stringify(contents)); | ||
formData.append('file', fileContents, { | ||
contentType: 'application/json', | ||
filename: `${locale}.json` | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData | ||
}); | ||
log('Successfully Uploaded:', locale, '\n'); | ||
} | ||
async function ensureBranch(branch) { | ||
await callPhrase(`branches`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
name: branch | ||
}) | ||
}); | ||
trace('Created branch:', branch); | ||
} | ||
@@ -134,5 +152,7 @@ async function pull({ | ||
}, config) { | ||
trace(`Pulling translations from branch ${branch}`); | ||
await ensureBranch(branch); | ||
const alternativeLanguages = core.getAltLanguages(config); | ||
const allPhraseTranslations = await getAllTranslationsFromPhrase(branch); | ||
const allPhraseTranslations = await pullAllTranslations(branch); | ||
trace(`Pulling translations from Phrase for languages ${config.devLanguage} and ${alternativeLanguages.join(', ')}`); | ||
const allVocabTranslations = await core.loadAllTranslations({ | ||
@@ -143,3 +163,3 @@ fallbacks: 'none' | ||
for (const loadedTranslation of allVocabTranslations) { | ||
const devTranslations = loadedTranslation.languages.get(config.devLanguage); | ||
const devTranslations = loadedTranslation.languages[config.devLanguage]; | ||
@@ -160,6 +180,6 @@ if (!devTranslations) { | ||
await fs.promises.writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`); | ||
await writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`); | ||
for (const alternativeLanguage of alternativeLanguages) { | ||
const altTranslations = { ...loadedTranslation.languages.get(alternativeLanguage) | ||
const altTranslations = { ...loadedTranslation.languages[alternativeLanguage] | ||
}; | ||
@@ -184,7 +204,7 @@ const phraseAltTranslations = allPhraseTranslations[alternativeLanguage]; | ||
const altTranslationFilePath = core.getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage, config); | ||
await fs.promises.mkdir(path__default['default'].dirname(altTranslationFilePath), { | ||
const altTranslationFilePath = core.getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage); | ||
await mkdir(path__default['default'].dirname(altTranslationFilePath), { | ||
recursive: true | ||
}); | ||
await fs.promises.writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`); | ||
await writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`); | ||
} | ||
@@ -194,20 +214,2 @@ } | ||
async function uploadFile(contents, locale_id, branch) { | ||
const formData = new FormData__default['default'](); | ||
formData.append('file', Buffer.from(JSON.stringify(contents)), { | ||
contentType: 'application/json', | ||
filename: `${locale_id}.json` | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale_id); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale_id); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData | ||
}); | ||
log('Successfully Uploaded:', locale_id, '\n'); | ||
} | ||
/** | ||
@@ -225,2 +227,3 @@ * Uploading to the Phrase API for each language. Adding a unique namespace to each key using file path they key came from | ||
await ensureBranch(branch); | ||
trace(`Pushing translations to phrase for languages ${allLanguages.join(', ')}`); | ||
const phraseTranslations = {}; | ||
@@ -230,3 +233,3 @@ | ||
for (const language of allLanguages) { | ||
const localTranslations = loadedTranslation.languages.get(language); | ||
const localTranslations = loadedTranslation.languages[language]; | ||
@@ -250,3 +253,3 @@ if (!localTranslations) { | ||
if (phraseTranslations[language]) { | ||
await uploadFile(phraseTranslations[language], language, branch); | ||
await pushTranslationsByLocale(phraseTranslations[language], language, branch); | ||
} | ||
@@ -253,0 +256,0 @@ } |
import { promises } from 'fs'; | ||
import path from 'path'; | ||
import { getAltLanguages, loadAllTranslations, getUniqueKey, getAltLanguageFilePath } from '@vocab/core'; | ||
import FormData from 'form-data'; | ||
import fetch from 'node-fetch'; | ||
import chalk from 'chalk'; | ||
import debug from 'debug'; | ||
import FormData from 'form-data'; | ||
const mkdir = promises.mkdir; | ||
const writeFile = promises.writeFile; | ||
const trace = debug(`vocab:phrase`); | ||
@@ -15,4 +18,2 @@ const log = (...params) => { | ||
/* eslint-disable no-console */ | ||
function _callPhrase(path, options = {}) { | ||
@@ -85,16 +86,3 @@ const phraseApiToken = process.env.PHRASE_API_TOKEN; | ||
} | ||
async function ensureBranch(branch) { | ||
await callPhrase(`branches`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
name: branch | ||
}) | ||
}); | ||
trace('Created branch:', branch); | ||
} | ||
async function getAllTranslationsFromPhrase(branch) { | ||
async function pullAllTranslations(branch) { | ||
const phraseResult = await callPhrase(`translations?branch=${branch}&per_page=100`); | ||
@@ -115,2 +103,32 @@ const translations = {}; | ||
} | ||
async function pushTranslationsByLocale(contents, locale, branch) { | ||
const formData = new FormData(); | ||
const fileContents = Buffer.from(JSON.stringify(contents)); | ||
formData.append('file', fileContents, { | ||
contentType: 'application/json', | ||
filename: `${locale}.json` | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData | ||
}); | ||
log('Successfully Uploaded:', locale, '\n'); | ||
} | ||
async function ensureBranch(branch) { | ||
await callPhrase(`branches`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
name: branch | ||
}) | ||
}); | ||
trace('Created branch:', branch); | ||
} | ||
@@ -120,5 +138,7 @@ async function pull({ | ||
}, config) { | ||
trace(`Pulling translations from branch ${branch}`); | ||
await ensureBranch(branch); | ||
const alternativeLanguages = getAltLanguages(config); | ||
const allPhraseTranslations = await getAllTranslationsFromPhrase(branch); | ||
const allPhraseTranslations = await pullAllTranslations(branch); | ||
trace(`Pulling translations from Phrase for languages ${config.devLanguage} and ${alternativeLanguages.join(', ')}`); | ||
const allVocabTranslations = await loadAllTranslations({ | ||
@@ -129,3 +149,3 @@ fallbacks: 'none' | ||
for (const loadedTranslation of allVocabTranslations) { | ||
const devTranslations = loadedTranslation.languages.get(config.devLanguage); | ||
const devTranslations = loadedTranslation.languages[config.devLanguage]; | ||
@@ -146,6 +166,6 @@ if (!devTranslations) { | ||
await promises.writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`); | ||
await writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`); | ||
for (const alternativeLanguage of alternativeLanguages) { | ||
const altTranslations = { ...loadedTranslation.languages.get(alternativeLanguage) | ||
const altTranslations = { ...loadedTranslation.languages[alternativeLanguage] | ||
}; | ||
@@ -170,7 +190,7 @@ const phraseAltTranslations = allPhraseTranslations[alternativeLanguage]; | ||
const altTranslationFilePath = getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage, config); | ||
await promises.mkdir(path.dirname(altTranslationFilePath), { | ||
const altTranslationFilePath = getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage); | ||
await mkdir(path.dirname(altTranslationFilePath), { | ||
recursive: true | ||
}); | ||
await promises.writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`); | ||
await writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`); | ||
} | ||
@@ -180,20 +200,2 @@ } | ||
async function uploadFile(contents, locale_id, branch) { | ||
const formData = new FormData(); | ||
formData.append('file', Buffer.from(JSON.stringify(contents)), { | ||
contentType: 'application/json', | ||
filename: `${locale_id}.json` | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale_id); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale_id); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData | ||
}); | ||
log('Successfully Uploaded:', locale_id, '\n'); | ||
} | ||
/** | ||
@@ -211,2 +213,3 @@ * Uploading to the Phrase API for each language. Adding a unique namespace to each key using file path they key came from | ||
await ensureBranch(branch); | ||
trace(`Pushing translations to phrase for languages ${allLanguages.join(', ')}`); | ||
const phraseTranslations = {}; | ||
@@ -216,3 +219,3 @@ | ||
for (const language of allLanguages) { | ||
const localTranslations = loadedTranslation.languages.get(language); | ||
const localTranslations = loadedTranslation.languages[language]; | ||
@@ -236,3 +239,3 @@ if (!localTranslations) { | ||
if (phraseTranslations[language]) { | ||
await uploadFile(phraseTranslations[language], language, branch); | ||
await pushTranslationsByLocale(phraseTranslations[language], language, branch); | ||
} | ||
@@ -239,0 +242,0 @@ } |
{ | ||
"name": "@vocab/phrase", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"main": "dist/vocab-phrase.cjs.js", | ||
@@ -9,4 +9,4 @@ "module": "dist/vocab-phrase.esm.js", | ||
"dependencies": { | ||
"@vocab/core": "^0.0.3", | ||
"@vocab/types": "^0.0.4", | ||
"@vocab/core": "^0.0.4", | ||
"@vocab/types": "^0.0.5", | ||
"chalk": "^4.1.0", | ||
@@ -13,0 +13,0 @@ "debug": "^4.3.1", |
@@ -71,4 +71,6 @@ # Vocab | ||
**./translations.json** | ||
**Note:** Currently, to create a new translation it must be placed inside a **`__translations__`** folder, this folder name can be configured with `translationsDirname` configuration. | ||
**`./__translations__/translations.json`** | ||
```json | ||
@@ -83,2 +85,5 @@ { | ||
Then run `vocab compile`. Or `vocab compile --watch`. | ||
This will create new `translation.ts` files for each `translation.json` file. | ||
You can then import these translations into your React components. Translations can be used by calling the `t` function returned by `useTranslation`. | ||
@@ -90,3 +95,3 @@ | ||
import { useTranslation } from '@vocab/react'; | ||
import translations from './translations.json'; | ||
import translations from './translations'; | ||
@@ -156,3 +161,3 @@ function MyComponent({ children }) { | ||
Vocab generates custom `translation.json.d.ts` files that give your React components strongly typed translations to work with. | ||
Vocab generates custom `translation.ts` files that give your React components strongly typed translations to work with. | ||
@@ -162,5 +167,11 @@ To generate these types run: | ||
```bash | ||
$ vocab generate-types | ||
$ vocab compile | ||
``` | ||
Or to rerun the compiler when files change use: | ||
```bash | ||
$ vocab compile --watch | ||
``` | ||
## External translation tooling | ||
@@ -167,0 +178,0 @@ |
@@ -0,4 +1,7 @@ | ||
import FormData from 'form-data'; | ||
import { TranslationsByKey } from './../../types/src/index'; | ||
/* eslint-disable no-console */ | ||
import type { TranslationsByLanguage } from '@vocab/types'; | ||
import fetch from 'node-fetch'; | ||
import { trace } from './logger'; | ||
import { log, trace } from './logger'; | ||
@@ -92,2 +95,46 @@ function _callPhrase(path: string, options: Parameters<typeof fetch>[1] = {}) { | ||
export async function pullAllTranslations( | ||
branch: string, | ||
): Promise<TranslationsByLanguage> { | ||
const phraseResult: Array<{ | ||
key: { name: string }; | ||
locale: { code: string }; | ||
content: string; | ||
}> = await callPhrase(`translations?branch=${branch}&per_page=100`); | ||
const translations: TranslationsByLanguage = {}; | ||
for (const r of phraseResult) { | ||
if (!translations[r.locale.code]) { | ||
translations[r.locale.code] = {}; | ||
} | ||
translations[r.locale.code][r.key.name] = { message: r.content }; | ||
} | ||
return translations; | ||
} | ||
export async function pushTranslationsByLocale( | ||
contents: TranslationsByKey, | ||
locale: string, | ||
branch: string, | ||
) { | ||
const formData = new FormData(); | ||
const fileContents = Buffer.from(JSON.stringify(contents)); | ||
formData.append('file', fileContents, { | ||
contentType: 'application/json', | ||
filename: `${locale}.json`, | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
log('Successfully Uploaded:', locale, '\n'); | ||
} | ||
export async function ensureBranch(branch: string) { | ||
@@ -94,0 +141,0 @@ await callPhrase(`branches`, { |
@@ -1,2 +0,2 @@ | ||
import { promises as fs } from 'fs'; | ||
import { writeFile, mkdir } from './file'; | ||
import path from 'path'; | ||
@@ -10,31 +10,11 @@ | ||
} from '@vocab/core'; | ||
import type { | ||
UserConfig, | ||
LanguageName, | ||
TranslationsByLanguage, | ||
} from '@vocab/types'; | ||
import type { UserConfig } from '@vocab/types'; | ||
import { callPhrase, ensureBranch } from './phrase-api'; | ||
import { pullAllTranslations, ensureBranch } from './phrase-api'; | ||
import { trace } from './logger'; | ||
async function getAllTranslationsFromPhrase( | ||
branch: string, | ||
): Promise<Record<LanguageName, TranslationsByLanguage>> { | ||
const phraseResult: Array<{ | ||
key: { name: string }; | ||
locale: { code: string }; | ||
content: string; | ||
}> = await callPhrase(`translations?branch=${branch}&per_page=100`); | ||
const translations: Record<LanguageName, TranslationsByLanguage> = {}; | ||
for (const r of phraseResult) { | ||
if (!translations[r.locale.code]) { | ||
translations[r.locale.code] = {}; | ||
} | ||
translations[r.locale.code][r.key.name] = { message: r.content }; | ||
} | ||
return translations; | ||
} | ||
interface PullOptions { | ||
branch?: string; | ||
} | ||
export async function pull( | ||
@@ -44,5 +24,11 @@ { branch = 'local-development' }: PullOptions, | ||
) { | ||
trace(`Pulling translations from branch ${branch}`); | ||
await ensureBranch(branch); | ||
const alternativeLanguages = getAltLanguages(config); | ||
const allPhraseTranslations = await getAllTranslationsFromPhrase(branch); | ||
const allPhraseTranslations = await pullAllTranslations(branch); | ||
trace( | ||
`Pulling translations from Phrase for languages ${ | ||
config.devLanguage | ||
} and ${alternativeLanguages.join(', ')}`, | ||
); | ||
@@ -55,3 +41,3 @@ const allVocabTranslations = await loadAllTranslations( | ||
for (const loadedTranslation of allVocabTranslations) { | ||
const devTranslations = loadedTranslation.languages.get(config.devLanguage); | ||
const devTranslations = loadedTranslation.languages[config.devLanguage]; | ||
@@ -73,3 +59,3 @@ if (!devTranslations) { | ||
} | ||
await fs.writeFile( | ||
await writeFile( | ||
loadedTranslation.filePath, | ||
@@ -81,3 +67,3 @@ `${JSON.stringify(defaultValues, null, 2)}\n`, | ||
const altTranslations = { | ||
...loadedTranslation.languages.get(alternativeLanguage), | ||
...loadedTranslation.languages[alternativeLanguage], | ||
}; | ||
@@ -107,9 +93,8 @@ const phraseAltTranslations = allPhraseTranslations[alternativeLanguage]; | ||
alternativeLanguage, | ||
config, | ||
); | ||
await fs.mkdir(path.dirname(altTranslationFilePath), { | ||
await mkdir(path.dirname(altTranslationFilePath), { | ||
recursive: true, | ||
}); | ||
await fs.writeFile( | ||
await writeFile( | ||
altTranslationFilePath, | ||
@@ -116,0 +101,0 @@ `${JSON.stringify(altTranslations, null, 2)}\n`, |
@@ -1,37 +0,8 @@ | ||
import FormData from 'form-data'; | ||
import { TranslationsByLanguage } from './../../types/src/index'; | ||
import { callPhrase, ensureBranch } from './phrase-api'; | ||
import { log, trace } from './logger'; | ||
import { ensureBranch, pushTranslationsByLocale } from './phrase-api'; | ||
import { trace } from './logger'; | ||
import { loadAllTranslations, getUniqueKey } from '@vocab/core'; | ||
import { UserConfig } from '@vocab/types'; | ||
interface TranslationFile { | ||
[k: string]: { message: string; description?: string }; | ||
} | ||
async function uploadFile( | ||
contents: TranslationFile, | ||
locale_id: string, | ||
branch: string, | ||
) { | ||
const formData = new FormData(); | ||
formData.append('file', Buffer.from(JSON.stringify(contents)), { | ||
contentType: 'application/json', | ||
filename: `${locale_id}.json`, | ||
}); | ||
formData.append('file_format', 'json'); | ||
formData.append('locale_id', locale_id); | ||
formData.append('branch', branch); | ||
formData.append('update_translations', 'true'); | ||
trace('Starting to upload:', locale_id); | ||
await callPhrase(`uploads`, { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
log('Successfully Uploaded:', locale_id, '\n'); | ||
} | ||
interface PushOptions { | ||
@@ -53,7 +24,11 @@ branch: string; | ||
const phraseTranslations: Record<string, TranslationFile> = {}; | ||
trace( | ||
`Pushing translations to phrase for languages ${allLanguages.join(', ')}`, | ||
); | ||
const phraseTranslations: TranslationsByLanguage = {}; | ||
for (const loadedTranslation of allLanguageTranslations) { | ||
for (const language of allLanguages) { | ||
const localTranslations = loadedTranslation.languages.get(language); | ||
const localTranslations = loadedTranslation.languages[language]; | ||
if (!localTranslations) { | ||
@@ -74,5 +49,9 @@ continue; | ||
if (phraseTranslations[language]) { | ||
await uploadFile(phraseTranslations[language], language, branch); | ||
await pushTranslationsByLocale( | ||
phraseTranslations[language], | ||
language, | ||
branch, | ||
); | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48402
22
1052
188
+ Added@vocab/core@0.0.4(transitive)
+ Added@vocab/types@0.0.5(transitive)
- Removed@vocab/core@0.0.3(transitive)
- Removed@vocab/types@0.0.4(transitive)
Updated@vocab/core@^0.0.4
Updated@vocab/types@^0.0.5