Skip to content
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

make build logging optional #107

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ In order to use each translation in the project, use the _translation id_ compos
| `localesPath` | A string with the directory of JSONs locales. It doesn't work with a custom server, instead you should use the `loadLocaleFrom` config. | `string` | `"locales"` |
| `loadLocaleFrom` | A function to return the dynamic import of each locale. IT ONLY WORKS with a custom server. If you don't use a custom server, you should use the `localesPath` instead. [See an example](#use-translations-in-your-pages) | `Function` | `null` |
| `pages` | An object that defines the namespaces used in each page. Example of object: `{"/": ["home", "example"]}`. To add namespaces to all pages you should use the key `"*"`, ex: `{"*": ["common"]}`. It's also possible to use regex using `rgx:` on front: `{"rgx:/form$": ["form"]}`. In case of using a custom server, you can also use a function instead of an array, to provide some namespaces depending on some rules, ex: `{ "/": ({ req, query }) => query.type === 'example' ? ['example'] : []}` | `Object<Array<string>/Function` | `{}` |
| `logBuild` | Configure if the build result should be logged to the console | `Boolean` | `true` |

## 5. API

Expand Down Expand Up @@ -347,7 +348,7 @@ export default function ExampleWithDynamicNamespace() {
return (
<DynamicNamespaces
dynamic={(lang, ns) =>
import(`../../locales/${lang}/${ns}.json`).then((m) => m.default)
import(`../../locales/${lang}/${ns}.json`).then(m => m.default)
}
namespaces={['dynamic']}
fallback="Loading..."
Expand Down Expand Up @@ -484,7 +485,7 @@ function PluralExample() {

useEffect(() => {
const interval = setInterval(() => {
setCount((v) => (v === 5 ? 0 : v + 1))
setCount(v => (v === 5 ? 0 : v + 1))
}, 1000)

return () => clearInterval(interval)
Expand Down Expand Up @@ -574,7 +575,7 @@ const { allLanguages } = i18nConfig
function ChangeLanguage() {
const { t, lang } = useTranslation()

return allLanguages.map((lng) => {
return allLanguages.map(lng => {
if (lng === lang) return null

// Or you can attach the current pathame at the end
Expand Down
15 changes: 9 additions & 6 deletions cli/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ const {
localesPath = 'locales',
pages = {},
redirectToDefaultLang = false,
logBuild = true,
} = require(process.cwd() + '/i18n.json') || {}

function readDirR(dir) {
return fs.statSync(dir).isDirectory()
? Array.prototype.concat(
...fs
.readdirSync(dir)
.map((f) => readDirR(path.join(dir, f).replace('\\', '/')))
.map(f => readDirR(path.join(dir, f).replace('\\', '/')))
)
: dir.replace('\\', '/')
}
Expand All @@ -35,7 +36,7 @@ async function createPagesDir(langs = []) {
execSync(`rm -rf ${finalPagesDir}`)
fs.mkdirSync(finalPagesDir)

langs.forEach(async (lang) => {
langs.forEach(async lang => {
fs.mkdirSync(`${finalPagesDir}/${lang}`)
})

Expand All @@ -44,7 +45,9 @@ async function createPagesDir(langs = []) {
fs.writeFileSync(`${finalPagesDir}/index.js`, getIndexRedirectTemplate())
}

console.log(`Building pages | from ${currentPagesDir} to ${finalPagesDir}`)
if (logBuild) {
console.log(`Building pages | from ${currentPagesDir} to ${finalPagesDir}`)
}
readPageNamespaces(langs)
}

Expand All @@ -65,11 +68,11 @@ function clearPageExt(page) {
* STEP 2: Read each page namespaces
*/
function readPageNamespaces(langs) {
readDirR(currentPagesDir).forEach(async (page) => {
readDirR(currentPagesDir).forEach(async page => {
const pageId = clearPageExt(page.replace(currentPagesDir, '')) || '/'
const namespaces = await getPageNamespaces({ pages }, pageId)

if (!isNextInternal(page)) {
if (!isNextInternal(page) && logBuild) {
console.log(`🔨 ${pageId}`, namespaces)
}

Expand Down Expand Up @@ -167,7 +170,7 @@ function buildPageInAllLocales(pagePath, namespaces, langs) {
}

// For each lang
langs.forEach((lang) => {
langs.forEach(lang => {
buildPageLocale({
lang,
namespaces,
Expand Down