-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react-i18n - add
from-dictionary-index
mode + generator
- Loading branch information
1 parent
3c4a704
commit a8f418f
Showing
18 changed files
with
360 additions
and
52 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { | ||
generateTranslationDictionaries, | ||
} from './dist/src/babel-plugin/generate-dictionaries'; | ||
export * from './dist/src/babel-plugin/generate-dictionaries'; |
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 @@ | ||
module.exports = require('./dist/src/babel-plugin/generate-dictionaries'); |
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
74 changes: 74 additions & 0 deletions
74
packages/react-i18n/src/babel-plugin/generate-dictionaries.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,74 @@ | ||
import {join} from 'path'; | ||
|
||
import fs from 'fs-extra'; | ||
import cloneDeep from 'lodash.clonedeep'; | ||
import merge from 'lodash.merge'; | ||
|
||
import {DEFAULT_FALLBACK_LOCALE, findTranslationBuckets} from './shared'; | ||
|
||
export interface Options { | ||
fallbackLocale: string; | ||
rootDir: string; | ||
} | ||
|
||
export async function generateTranslationDictionaries( | ||
locales: string[], | ||
{ | ||
fallbackLocale = DEFAULT_FALLBACK_LOCALE, | ||
rootDir = process.cwd(), | ||
}: Partial<Options> = {}, | ||
) { | ||
if (!locales || locales.length === 0) { | ||
throw new Error( | ||
'generateTranslationDictionary must be called with at least one locale.', | ||
); | ||
} | ||
|
||
const translationBuckets = findTranslationBuckets(rootDir); | ||
|
||
await Promise.all( | ||
Object.entries(translationBuckets).map( | ||
async ([translationsDir, translationFilePaths]) => { | ||
const fallbackTranslations = await readLocaleTranslations( | ||
fallbackLocale, | ||
translationFilePaths, | ||
); | ||
|
||
const dictionary = await locales.reduce(async (accPromise, locale) => { | ||
const localeTranslations = await readLocaleTranslations( | ||
locale, | ||
translationFilePaths, | ||
); | ||
const acc = await accPromise; | ||
acc[locale] = merge( | ||
cloneDeep(fallbackTranslations), | ||
localeTranslations, | ||
); | ||
return acc; | ||
}, {}); | ||
|
||
const contentStr = JSON.stringify(dictionary); | ||
|
||
// Writing the content out as a JSON.parse-wrapped string seems | ||
// counter-intuitive, but browsers can parse this faster than they | ||
// can parse JavaScript ‾\_(ツ)_/‾ | ||
// https://www.youtube.com/watch?v=ff4fgQxPaO0 | ||
await fs.writeFile( | ||
join(translationsDir, 'index.js'), | ||
`export default JSON.parse(${JSON.stringify(contentStr)})`, | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
|
||
async function readLocaleTranslations( | ||
locale: string, | ||
translationFilePaths: string[], | ||
) { | ||
const translationPath = translationFilePaths.find(filePath => | ||
filePath.endsWith(`/${locale}.json`), | ||
); | ||
|
||
return translationPath ? fs.readJson(translationPath) : {}; | ||
} |
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
Oops, something went wrong.