-
Notifications
You must be signed in to change notification settings - Fork 220
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
react-i18n - add from-dictionary-index
plugin mode
#1197
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dist/src/babel-plugin/generate-dictionaries'); |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a pretty great mental image |
||
|
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we have to use lodash merge here because this needs to merge deeply and whatnot? Does make me wonder if we want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmmmm, I'm conflicted about this because I hate adding lodash to anything 😸 The merge/clone things were unavoidable, but in for a penny... I've created an issue to track this enhancement: #1200 |
||
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 | ||
GoodForOneFare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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) : {}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Travis keeps timing out because the build takes >10m with no output. Making it verbose works around Travis' behaviour 🤷🏻♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh it also works around my behaviour, I get a gut feeling that it's broken every time since we changed it from the old setup :P