@graphcommerce/lingui-next 1.2.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,38 +1,52 @@
1
1
  # Change Log
2
2
 
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
3
+ ## 2.0.0
5
4
 
6
- ## [1.2.1](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/lingui-next@1.2.0...@graphcommerce/lingui-next@1.2.1) (2021-12-03)
5
+ ### Major Changes
7
6
 
7
+ - [#1258](https://github.com/ho-nl/m2-pwa/pull/1258)
8
+ [`ad36382a4`](https://github.com/ho-nl/m2-pwa/commit/ad36382a4d55d83d9e47b7eb6a02671a2a631a05)
9
+ Thanks [@paales](https://github.com/paales)! - Upgraded to Material UI 5
8
10
 
9
- ### Bug Fixes
11
+ All notable changes to this project will be documented in this file. See
12
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
10
13
 
11
- * make sure we're not throwing a generic 'No locales specified' when there is a generic error ([fd3dd23](https://github.com/ho-nl/m2-pwa/commit/fd3dd233fb293f6afc08636316a34d2a2db4bbc3))
14
+ ## [1.3.1](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/lingui-next@1.3.0...@graphcommerce/lingui-next@1.3.1) (2022-01-04)
12
15
 
16
+ ### Bug Fixes
13
17
 
18
+ - pages would be blank because the locale couldn't properly be loaded
19
+ ([5fe9ecd](https://github.com/ho-nl/m2-pwa/commit/5fe9ecd204c3f9efddcf95d54464b1b931ef682e))
14
20
 
21
+ # [1.3.0](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/lingui-next@1.2.4...@graphcommerce/lingui-next@1.3.0) (2022-01-03)
15
22
 
23
+ ### Features
16
24
 
17
- # [1.2.0](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/lingui-next@1.1.0...@graphcommerce/lingui-next@1.2.0) (2021-11-12)
25
+ - **framer-next-pages:** reduce rerenders when navigating to a new page
26
+ ([5cf3301](https://github.com/ho-nl/m2-pwa/commit/5cf330130bb3527057da015e3c4a6fa295d7262e))
18
27
 
28
+ ## [1.2.1](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/lingui-next@1.2.0...@graphcommerce/lingui-next@1.2.1) (2021-12-03)
19
29
 
20
30
  ### Bug Fixes
21
31
 
22
- * error in cli about missing plurals ([fedde70](https://github.com/ho-nl/m2-pwa/commit/fedde70012b0b5694114a0f5c3ab0f7d5c8cd276))
23
-
32
+ - make sure we're not throwing a generic 'No locales specified' when there is a generic error
33
+ ([fd3dd23](https://github.com/ho-nl/m2-pwa/commit/fd3dd233fb293f6afc08636316a34d2a2db4bbc3))
24
34
 
25
- ### Features
26
-
27
- * added tons of translations ([9bb0ac7](https://github.com/ho-nl/m2-pwa/commit/9bb0ac709b58df6ea6141e92e4923a5ca9ae2963))
35
+ # [1.2.0](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/lingui-next@1.1.0...@graphcommerce/lingui-next@1.2.0) (2021-11-12)
28
36
 
37
+ ### Bug Fixes
29
38
 
39
+ - error in cli about missing plurals
40
+ ([fedde70](https://github.com/ho-nl/m2-pwa/commit/fedde70012b0b5694114a0f5c3ab0f7d5c8cd276))
30
41
 
42
+ ### Features
31
43
 
44
+ - added tons of translations
45
+ ([9bb0ac7](https://github.com/ho-nl/m2-pwa/commit/9bb0ac709b58df6ea6141e92e4923a5ca9ae2963))
32
46
 
33
47
  # 1.1.0 (2021-11-11)
34
48
 
35
-
36
49
  ### Features
37
50
 
38
- * lingui configuration and integration greatly simplified and fixed ssr ([d8ec22a](https://github.com/ho-nl/m2-pwa/commit/d8ec22a80295af854a4cf6f357c4fb137c5b550d))
51
+ - lingui configuration and integration greatly simplified and fixed ssr
52
+ ([d8ec22a](https://github.com/ho-nl/m2-pwa/commit/d8ec22a80295af854a4cf6f357c4fb137c5b550d))
@@ -1,29 +1,28 @@
1
1
  import { i18n, Messages } from '@lingui/core'
2
2
  import { I18nProvider, I18nProviderProps } from '@lingui/react'
3
3
  import { nl, en, fr } from 'make-plural/plurals'
4
- import { useRouter } from 'next/router'
5
4
  import React, { useMemo } from 'react'
6
- import { MessageLoader } from '../types'
5
+ import { MessageLoader, SyncMessageLoader } from '../types'
7
6
 
8
7
  type LinguiProviderProps = Omit<I18nProviderProps, 'i18n'> & {
9
8
  children: React.ReactNode
10
9
  loader: MessageLoader
10
+ ssrLoader: SyncMessageLoader
11
+ locale: string
11
12
  }
12
13
 
14
+ // todo: Load these plurals with a loader, however dynamic imports doesn't support tree shaking so loading them dynamically will load all locales.
13
15
  i18n.loadLocaleData({
14
16
  nl: { plurals: nl },
15
17
  fr: { plurals: fr },
16
18
  en: { plurals: en },
17
19
  })
18
20
 
19
- export default function LinguiProvider(props: LinguiProviderProps) {
20
- const { loader, ...i18nProviderPRops } = props
21
+ export function LinguiProvider(props: LinguiProviderProps) {
22
+ const { loader, ssrLoader, locale, ...i18nProviderPRops } = props
21
23
 
22
- const { locale = 'en' } = useRouter()
23
24
  useMemo(() => {
24
- const localeOnly = locale?.split('-')?.[0]
25
-
26
- if (!localeOnly) return
25
+ const localeOnly = locale?.split('-')[0]
27
26
  const data = globalThis.document?.getElementById('lingui')
28
27
 
29
28
  if (data?.lang === localeOnly && data.textContent) {
@@ -31,24 +30,32 @@ export default function LinguiProvider(props: LinguiProviderProps) {
31
30
  i18n.load(localeOnly, JSON.parse(data.textContent) as Messages)
32
31
  i18n.activate(localeOnly)
33
32
  } else if (i18n.locale !== locale) {
34
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
35
- ;(async () => {
36
- try {
37
- const { messages } = await loader(localeOnly)
38
- i18n.load(localeOnly, messages)
39
- i18n.activate(localeOnly)
40
- } catch (e) {
41
- if (process.env.NODE_ENV !== 'production')
42
- throw new Error(
43
- `Can not find the .po file for the locale '${localeOnly}'. Possible reasons:
44
- - If you've added a new locale, please run 'yarn lingui:extact' to generate the .po file.
45
- - Make sure the passed load function is correct.`,
46
- )
47
- if (process.env.NODE_ENV === 'production') console.error(e)
48
- }
49
- })()
33
+ if (typeof window === 'undefined') {
34
+ const { messages } = ssrLoader(localeOnly)
35
+ i18n.load(localeOnly, messages)
36
+ i18n.activate(localeOnly)
37
+ } else {
38
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
39
+ ;(async () => {
40
+ try {
41
+ const { messages } = await loader(localeOnly)
42
+ i18n.load(localeOnly, messages)
43
+ i18n.activate(localeOnly)
44
+ } catch (e) {
45
+ if (process.env.NODE_ENV !== 'production')
46
+ throw new Error(
47
+ `Could not load locale. Can't find the .po file for the locale '${localeOnly}'. Possible reasons:
48
+ - You have configured a new locale in the .env and there is no .po file for it. Please run 'yarn lingui:extact' to generate the .po file or duplicate an existing one.
49
+ - The load function you passed to is incorrect.`,
50
+ )
51
+ if (process.env.NODE_ENV === 'production') console.error(e)
52
+ }
53
+ })()
54
+ }
50
55
  }
51
- }, [locale, loader])
56
+ // We dont want to call this when the loader/ssrLoader changes, because it will cause a re-render.
57
+ // eslint-disable-next-line react-hooks/exhaustive-deps
58
+ }, [locale])
52
59
 
53
60
  return <I18nProvider i18n={i18n} {...i18nProviderPRops} />
54
61
  }
package/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { default as LinguiProvider } from './components/LinguiProvider'
2
- export * from './utils/linguiWrapGetInitialProps'
1
+ export { LinguiProvider } from './components/LinguiProvider'
2
+ export * from './utils/withLingui'
3
3
  export * from './types'
package/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "@graphcommerce/lingui-next",
3
- "version": "1.2.4",
3
+ "version": "2.0.0",
4
4
  "sideEffects": false,
5
5
  "scripts": {
6
6
  "dev": "tsc -W"
7
7
  },
8
8
  "prettier": "@graphcommerce/prettier-config-pwa",
9
- "browserslist": [
10
- "extends @graphcommerce/browserslist-config-pwa"
11
- ],
12
9
  "eslintConfig": {
13
10
  "extends": "@graphcommerce/eslint-config-pwa",
14
11
  "parserOptions": {
@@ -16,21 +13,19 @@
16
13
  }
17
14
  },
18
15
  "dependencies": {
19
- "@lingui/core": "^3.13.0",
20
- "@lingui/macro": "^3.13.0",
21
- "@lingui/react": "^3.13.0"
16
+ "@lingui/core": "^3.13.2",
17
+ "@lingui/macro": "^3.13.2",
18
+ "@lingui/react": "^3.13.2"
22
19
  },
23
20
  "devDependencies": {
24
- "@graphcommerce/browserslist-config-pwa": "^3.0.3",
25
- "@graphcommerce/eslint-config-pwa": "^3.1.9",
26
- "@graphcommerce/prettier-config-pwa": "^3.0.5",
27
- "@graphcommerce/typescript-config-pwa": "^3.1.2",
28
- "@playwright/test": "^1.17.1"
21
+ "@graphcommerce/eslint-config-pwa": "^4.0.0",
22
+ "@graphcommerce/prettier-config-pwa": "^4.0.0",
23
+ "@graphcommerce/typescript-config-pwa": "^4.0.0",
24
+ "@playwright/test": "^1.18.1"
29
25
  },
30
26
  "peerDependencies": {
31
- "next": "^12.0.7",
27
+ "next": "^12.0.10",
32
28
  "react": "^17.0.2",
33
29
  "react-dom": "^17.0.2"
34
- },
35
- "gitHead": "06b4426d199de9ec2a9d2ac86d42ab047e59e7e7"
30
+ }
36
31
  }
package/types.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { Messages } from '@lingui/core'
2
2
 
3
3
  export type MessageLoader = (locale: string) => Promise<{ messages: Messages }>
4
+ export type SyncMessageLoader = (locale: string) => { messages: Messages }
@@ -0,0 +1,46 @@
1
+ import { i18n } from '@lingui/core'
2
+ // eslint-disable-next-line @next/next/no-document-import-in-page
3
+ import { DocumentContext } from 'next/document'
4
+ // eslint-disable-next-line @next/next/no-document-import-in-page
5
+ import type NextDocument from 'next/document'
6
+ import React from 'react'
7
+ import { MessageLoader } from '../types'
8
+
9
+ export function withLingui(
10
+ Document: typeof NextDocument,
11
+ load: MessageLoader,
12
+ ): typeof NextDocument {
13
+ return class DocumentWithEmotionCache extends Document {
14
+ static async getInitialProps(ctx: DocumentContext) {
15
+ const initial = await Document.getInitialProps(ctx)
16
+
17
+ const locale = ctx.locale?.split('-')?.[0]
18
+
19
+ if (!locale) return initial
20
+ try {
21
+ const { messages } = await load(locale)
22
+
23
+ i18n.load(locale, messages)
24
+ i18n.activate(locale)
25
+
26
+ return {
27
+ ...initial,
28
+ head: [
29
+ ...(React.Children.toArray(initial.head) as Array<JSX.Element | null>),
30
+ <script
31
+ key='lingui'
32
+ type='application/json'
33
+ id='lingui'
34
+ lang={locale}
35
+ // eslint-disable-next-line react/no-danger
36
+ dangerouslySetInnerHTML={{ __html: JSON.stringify(messages) }}
37
+ />,
38
+ ],
39
+ }
40
+ } catch (e) {
41
+ if (process.env.NODE_ENV !== 'production') console.error(e)
42
+ return initial
43
+ }
44
+ }
45
+ }
46
+ }
@@ -1,42 +0,0 @@
1
- import { i18n } from '@lingui/core'
2
- // eslint-disable-next-line @next/next/no-document-import-in-page
3
- import { DocumentContext, DocumentInitialProps } from 'next/document'
4
- import React from 'react'
5
- import { MessageLoader } from '../types'
6
-
7
- export function linguiWrapGetInitialProps(
8
- getInitialProps: (ctx: DocumentContext) => Promise<DocumentInitialProps>,
9
- load: MessageLoader,
10
- ): (ctx: DocumentContext) => Promise<DocumentInitialProps> {
11
- return async (ctx: DocumentContext) => {
12
- const initial = await getInitialProps(ctx)
13
-
14
- const locale = ctx.locale?.split('-')?.[0]
15
-
16
- if (!locale) return initial
17
- try {
18
- const { messages } = await load(locale)
19
-
20
- i18n.load(locale, messages)
21
- i18n.activate(locale)
22
-
23
- return {
24
- ...initial,
25
- head: [
26
- ...(React.Children.toArray(initial.head) as Array<JSX.Element | null>),
27
- <script
28
- key='lingui'
29
- type='application/json'
30
- id='lingui'
31
- lang={locale}
32
- // eslint-disable-next-line react/no-danger
33
- dangerouslySetInnerHTML={{ __html: JSON.stringify(messages) }}
34
- />,
35
- ],
36
- }
37
- } catch (e) {
38
- if (process.env.NODE_ENV !== 'production') console.error(e)
39
- return initial
40
- }
41
- }
42
- }