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

Inject lang into getStaticProps and company #93

Merged
merged 2 commits into from
Mar 24, 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
20 changes: 20 additions & 0 deletions __tests__/builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,25 @@ describe('builder', () => {
expect(pages.toString()).toContain('I18nProvider')
expect(pages.toString()).toContain('Page = Object.assign(Page, { ...C })')
})

test('Should inject lang to getStaticProps', () => {
const deflt = fs.readFileSync('examples/static-site/pages/index.js')
const es = fs.readFileSync('examples/static-site/pages/es/index.js')
const ca = fs.readFileSync('examples/static-site/pages/ca/index.js')
const en = fs.readFileSync('examples/static-site/pages/en/index.js')

expect(deflt.toString()).toContain(
`export const getStaticProps = ctx => _rest.getStaticProps({ ...ctx, lang: 'en' })`
)
expect(es.toString()).toContain(
`export const getStaticProps = ctx => _rest.getStaticProps({ ...ctx, lang: 'es' })`
)
expect(ca.toString()).toContain(
`export const getStaticProps = ctx => _rest.getStaticProps({ ...ctx, lang: 'ca' })`
)
expect(en.toString()).toContain(
`export const getStaticProps = ctx => _rest.getStaticProps({ ...ctx, lang: 'en' })`
)
})
})
})
27 changes: 26 additions & 1 deletion cli/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,35 @@ function readPageNamespaces(langs) {
})
}

function hasSpecialMethod(data, name) {
return data.match(new RegExp(`export (const|var|let|function) ${name}`))
}

function specialMethod(name, lang) {
return `export const ${name} = ctx => _rest.${name}({ ...ctx, lang: '${lang}' })`
}

/**
* STEP 3: Build page in each lang path
*/
function getPageTemplate(prefix, page, lang, namespaces) {
const clearCommentsRgx = /\/\*[\s\S]*?\*\/|\/\/.*/g
const pageData = fs
.readFileSync(page)
.toString('UTF-8')
.replace(clearCommentsRgx, '')
const isGetStaticProps = hasSpecialMethod(pageData, 'getStaticProps')
const isGetStaticPaths = hasSpecialMethod(pageData, 'getStaticPaths')
const isGetServerSideProps = hasSpecialMethod(pageData, 'getServerSideProps')
const hasSomeSpecialMethod =
isGetStaticProps || isGetStaticPaths || isGetServerSideProps

return `// @ts-nocheck
import I18nProvider from 'next-translate/I18nProvider'
import React from 'react'
import C from '${prefix}/${clearPageExt(page)}'
import C${
hasSomeSpecialMethod ? ', * as _rest' : ''
} from '${prefix}/${clearPageExt(page)}'
${namespaces
.map(
(ns, i) =>
Expand All @@ -103,6 +124,10 @@ export default function Page(p){

Page = Object.assign(Page, { ...C })

${isGetStaticProps ? specialMethod('getStaticProps', lang) : ''}
${isGetStaticPaths ? specialMethod('getStaticPaths', lang) : ''}
${isGetServerSideProps ? specialMethod('getServerSideProps', lang) : ''}

export * from '${prefix}/${clearPageExt(page)}'
`
}
Expand Down
7 changes: 4 additions & 3 deletions examples/static-site/pages_/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Header from '../components/header'

// @ts-ignore
export default function Home(props) {
const { t, lang } = useTranslation()
const { t } = useTranslation()
const description = t('home:description')
const linkName = t('home:more-examples')

Expand All @@ -27,6 +27,7 @@ export default function Home(props) {
)
}

export const getStaticProps = async () => ({
props: { getStaticPropsWorks: true },
// @ts-ignore
export const getStaticProps = async ({ lang }) => ({
props: { getStaticPropsWorks: true, lang },
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.12.0",
"version": "0.13.0-canary.1",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -49,7 +49,7 @@
"husky": "4.2.3",
"jest": "25.1.0",
"next": "9.3.1",
"prettier": "1.19.1",
"prettier": "2.0.2",
"pretty-quick": "2.0.1",
"react": "16.13.1",
"react-dom": "16.13.1",
Expand Down
Loading