Skip to content

Commit

Permalink
Release 1.0.7 (#614)
Browse files Browse the repository at this point in the history
* replace quotes only for the special $' pattern specific to .replace() (#529)

* replace quotes only for the special $' pattern specific to .replace() instead of replacing quoutes everywhere

* use .replace() callback to avoid parsing special string patterns

* write tests to verify that templateWithHoc and templateWithLoader correctly replaces special string cases. Update snapshots

* Update package version

* fix(transCore): when no suffix don't match spaces (#534)

* Update package version

* _one works (#541)

* Update package version

* Update README.md (#552)

* Update package version

* Update dependencies (#554)

* Update dependencies

* Update example deps

* Update Trans text after change lang (#566)

* Ignore api.(ts|js...) file (#567)

* Add useMemo to useTranslation (#574)

* Update deps (#582)

* Update version of package.json

* Adding tests (#585)

* Add new property `extensionsRgx` to config (#589)

* Add new property `extensionsRgx` to config

To be able to use custom Next.js page extensions (vercel/next.js#8454 (comment)), a new property `extensionsRgx` is added to the i18n configuration.

# How to use

// i18n.js

```
module.exports = {
  extensionsRgx: /\.(page|api)\.(tsx|ts|js|mjs|jsx)$/,
  ...
}
```

* docs: add `extensionsRgx` configuration property

* Update package.json

* Listen for `namespaces` changes and load necessary namespaces (#592)

* Update package.json version

* Revert "Add useMemo to useTranslation" (#605)

This reverts commit 8abc458.

# Conflicts:
#	package.json

* Remove console.warn because is already solved on Next.js 10.2.1-canary.4 (#609)

Already fixed in [Next.js canary 10.2.1-canary.4](https://github.com/vercel/next.js/releases/tag/v10.2.1-canary.4)

Co-authored-by: AndrewB <[email protected]>
Co-authored-by: slevy85 <[email protected]>
Co-authored-by: Justin <[email protected]>
Co-authored-by: Bernd Artmüller <[email protected]>
Co-authored-by: Rihards Ščeredins <[email protected]>
  • Loading branch information
6 people authored May 21, 2021
1 parent 21701d3 commit 8c86d10
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ In the configuration file you can use both the configuration that we specified h
| `loader` | If you wish to disable the webpack loader and manually load the namespaces on each page, we give you the opportunity to do so by disabling this option. | `Boolean` | `true` |
| `interpolation` | Change the delimeter that is used for interpolation. | `{prefix: string; suffix: string}` | `{prefix: '{{', suffix: '}}'}`
| `staticsHoc` | The HOCs we have in our API ([appWithI18n](#appwithi18n)), do not use [hoist-non-react-statics](https://github.com/mridgway/hoist-non-react-statics) in order not to include more kb than necessary _(static values different than getInitialProps in the pages are rarely used)_. If you have any conflict with statics, you can add hoist-non-react-statics (or any other alternative) here. [See an example](docs/hoist-non-react-statics.md). | `Function` | `null`
| `extensionsRgx` | Change the regex used by the webpack loader to find Next.js pages. | `Regex` | `/\.(tsx\|ts\|js\|mjs\|jsx)$/`


## 4. API

Expand Down
38 changes: 38 additions & 0 deletions __tests__/__snapshots__/templateWithLoader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,44 @@ export async function getStaticProps(ctx) {
"
`;

exports[`templateWithLoader loader as a wrapper page: /index | loader: getStaticProps | hasLoader: true 1`] = `
"import __i18nConfig from \\"@next-translate-root/i18n\\";
import __loadNamespaces from \\"next-translate/loadNamespaces\\";

(\\"$SomeString$\\");
$$SomeString$$ & $SomeString$ & \`$SomeString$\`;
(\\".cssClass{content:' ';\\");
('Hello \\"world\\"');

export default function Page() {
return <div>Hello world</div>;
}

const _getStaticProps = wrapper.getStaticProps(() => ({ props: {} }));

export async function getStaticProps(ctx) {
let res = _getStaticProps(ctx);
if (typeof res.then === \\"function\\") res = await res;
return {
...res,
props: {
...(res.props || {}),
...(await __loadNamespaces({
...ctx,
pathname: \\"/index\\",
loaderName: \\"getStaticProps\\",
...__i18nConfig,
loadLocaleFrom: (l, n) =>
import(\`@next-translate-root/locales/\${l}/\${n}\`).then(
(m) => m.default
),
})),
},
};
}
"
`;

exports[`templateWithLoader loader imported to another place page: /index | loader: getStaticProps | hasLoader: true 1`] = `
"import __i18nConfig from \\"@next-translate-root/i18n\\";
import __loadNamespaces from \\"next-translate/loadNamespaces\\";
Expand Down
12 changes: 12 additions & 0 deletions __tests__/hasHOC.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ describe('hasHOC', () => {
`)
).toBe(false)
})
test('with -> it has getStaticProps in a wrapper', () => {
expect(
hasHOC(`
import withWrapper from 'somewhere'
function Page() {
return <div>Hello world</div>
}
export const getStaticProps = wrapper()`)
).toBe(false)
})
test('with -> it has getStaticProps', () => {
expect(
hasHOC(`
Expand Down
18 changes: 18 additions & 0 deletions __tests__/templateWithLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ const tests = [
},
],
},
{
describe: 'loader as a wrapper',
code: `
export default function Page() {
return <div>Hello world</div>
}
const getStaticProps = wrapper.getStaticProps(() => ({ props: {} }))
export { getStaticProps }
`,
cases: [
{
page: '/index',
loader: 'getStaticProps',
hasLoader: true,
},
],
},
{
describe: 'loader imported to another place',
code: `
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "1.0.6",
"version": "1.0.7-canary.3",
"description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.",
"license": "MIT",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicNamespaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function DynamicNamespaces({

useEffect(() => {
loadNamespaces()
}, [])
}, [namespaces.join()])

if (!loaded) return fallback || null

Expand Down
2 changes: 1 addition & 1 deletion src/I18nProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useContext, useMemo } from 'react'
import React, { createContext, useContext } from 'react'
import { useRouter } from 'next/router'
import I18nContext from './_context'
import transCore from './transCore'
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function nextTranslate(nextConfig: any = {}) {
use: {
loader: 'next-translate/plugin/loader',
options: {
extensionsRgx: test,
extensionsRgx: restI18n.extensionsRgx || test,
hasGetInitialPropsOnAppJs,
hasAppJs: !!app,
pagesPath: path.join(pagesPath, '/'),
Expand Down
3 changes: 0 additions & 3 deletions src/plugin/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ export default function loader(rawCode: string) {
isGetStaticProps || isGetServerSideProps || isGetInitialProps

if (isGetInitialProps || (!hasLoader && isWrapperWithExternalHOC)) {
console.warn(
`🚨 [next-translate] In Next 10.x.x there is an issue related to i18n and getInitialProps. We recommend to replace getInitialProps to getServerSideProps on ${page}. Issue: https://github.com/vercel/next.js/issues/18396`
)
return templateWithHoc(rawCode, { typescript, hasLoadLocaleFrom })
}

Expand Down
7 changes: 2 additions & 5 deletions src/useTranslation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useMemo } from 'react'
import { useContext } from 'react'
import { I18n } from '.'
import wrapTWithDefaultNs from './wrapTWithDefaultNs'
import I18nContext from './_context'
Expand All @@ -7,9 +7,6 @@ export default function useTranslation(defaultNs?: string): I18n {
const ctx = useContext(I18nContext)
return {
...ctx,
t: useMemo(() => wrapTWithDefaultNs(ctx.t, defaultNs), [
ctx.lang,
defaultNs,
]),
t: wrapTWithDefaultNs(ctx.t, defaultNs),
}
}

0 comments on commit 8c86d10

Please sign in to comment.