diff --git a/cli/builder.js b/cli/builder.js index a5e8c07d..908790ee 100755 --- a/cli/builder.js +++ b/cli/builder.js @@ -42,15 +42,18 @@ function isNextInternal(pagePath) { return pagePath.startsWith(`${currentPagesDir}/_`) } +function clearPageExt(page) { + const rgx = /(\/index.jsx)|(\/index.js)|(\/index.tsx)|(\/index.ts)|(\.jsx)|(\.js)|(\.tsx)|(\.ts)/gm + + return page.replace(rgx, '') +} + /** * STEP 2: Read each page namespaces */ function readPageNamespaces(langs) { readDirR(currentPagesDir).forEach(page => { - const pageId = - page - .replace(currentPagesDir, '') - .replace(/(\/index.js)|(\/index.jsx)|(\.js)|(\.jsx)/gm, '') || '/' + const pageId = clearPageExt(page.replace(currentPagesDir, '')) || '/' const namespaces = [...(pages['*'] || []), ...(pages[pageId] || [])] @@ -66,11 +69,16 @@ function readPageNamespaces(langs) { * STEP 3: Build page in each lang path */ function getPageTemplate(prefix, page, lang, namespaces) { + const isTypeScript = page.endsWith('.ts') || page.endsWith('.tsx') + return `import I18nProvider from 'next-translate/I18nProvider' import React from 'react' -import C from '${prefix}/${page}' +import C from '${prefix}/${clearPageExt(page)}' ${namespaces - .map((ns, i) => `import ns${i} from '${prefix}/${localesPath}/${lang}/${ns}'`) + .map( + (ns, i) => + `import ns${i} from '${prefix}/${localesPath}/${lang}/${ns}.json'` + ) .join('\n')} const namespaces = { ${namespaces @@ -85,7 +93,7 @@ export default function Page(p){ ) } -Page.getInitialProps = C.getInitialProps +Page.getInitialProps = ${isTypeScript ? '(C as any)' : 'C'}.getInitialProps ` } diff --git a/package.json b/package.json index 05acbf26..d05afcc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-translate", - "version": "0.5.0", + "version": "0.6.0", "description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).", "license": "MIT", "keywords": [