@graphcommerce/lingui-next 1.3.0 → 2.0.1

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,49 +1,60 @@
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.
5
-
6
- # [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)
3
+ ## 2.0.1
7
4
 
5
+ ### Patch Changes
8
6
 
9
- ### Features
7
+ - [`0cbaa878b`](https://github.com/ho-nl/m2-pwa/commit/0cbaa878b8a844d5abbeb1797b625a33130e6514)
8
+ Thanks [@paales](https://github.com/paales)! - Added homepage and repository package.json files,
9
+ so that the packages link to back to the website and repository
10
10
 
11
- * **framer-next-pages:** reduce rerenders when navigating to a new page ([5cf3301](https://github.com/ho-nl/m2-pwa/commit/5cf330130bb3527057da015e3c4a6fa295d7262e))
11
+ ## 2.0.0
12
12
 
13
+ ### Major Changes
13
14
 
15
+ - [#1258](https://github.com/ho-nl/m2-pwa/pull/1258)
16
+ [`ad36382a4`](https://github.com/ho-nl/m2-pwa/commit/ad36382a4d55d83d9e47b7eb6a02671a2a631a05)
17
+ Thanks [@paales](https://github.com/paales)! - Upgraded to Material UI 5
14
18
 
19
+ All notable changes to this project will be documented in this file. See
20
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
15
21
 
22
+ ## [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)
16
23
 
17
- ## [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)
24
+ ### Bug Fixes
18
25
 
26
+ - pages would be blank because the locale couldn't properly be loaded
27
+ ([5fe9ecd](https://github.com/ho-nl/m2-pwa/commit/5fe9ecd204c3f9efddcf95d54464b1b931ef682e))
19
28
 
20
- ### Bug Fixes
29
+ # [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)
21
30
 
22
- * 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))
31
+ ### Features
23
32
 
33
+ - **framer-next-pages:** reduce rerenders when navigating to a new page
34
+ ([5cf3301](https://github.com/ho-nl/m2-pwa/commit/5cf330130bb3527057da015e3c4a6fa295d7262e))
24
35
 
36
+ ## [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)
25
37
 
38
+ ### Bug Fixes
26
39
 
40
+ - make sure we're not throwing a generic 'No locales specified' when there is a generic error
41
+ ([fd3dd23](https://github.com/ho-nl/m2-pwa/commit/fd3dd233fb293f6afc08636316a34d2a2db4bbc3))
27
42
 
28
43
  # [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)
29
44
 
30
-
31
45
  ### Bug Fixes
32
46
 
33
- * error in cli about missing plurals ([fedde70](https://github.com/ho-nl/m2-pwa/commit/fedde70012b0b5694114a0f5c3ab0f7d5c8cd276))
34
-
47
+ - error in cli about missing plurals
48
+ ([fedde70](https://github.com/ho-nl/m2-pwa/commit/fedde70012b0b5694114a0f5c3ab0f7d5c8cd276))
35
49
 
36
50
  ### Features
37
51
 
38
- * added tons of translations ([9bb0ac7](https://github.com/ho-nl/m2-pwa/commit/9bb0ac709b58df6ea6141e92e4923a5ca9ae2963))
39
-
40
-
41
-
42
-
52
+ - added tons of translations
53
+ ([9bb0ac7](https://github.com/ho-nl/m2-pwa/commit/9bb0ac709b58df6ea6141e92e4923a5ca9ae2963))
43
54
 
44
55
  # 1.1.0 (2021-11-11)
45
56
 
46
-
47
57
  ### Features
48
58
 
49
- * lingui configuration and integration greatly simplified and fixed ssr ([d8ec22a](https://github.com/ho-nl/m2-pwa/commit/d8ec22a80295af854a4cf6f357c4fb137c5b550d))
59
+ - lingui configuration and integration greatly simplified and fixed ssr
60
+ ([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
11
- locale?: string
10
+ ssrLoader: SyncMessageLoader
11
+ locale: string
12
12
  }
13
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.
14
15
  i18n.loadLocaleData({
15
16
  nl: { plurals: nl },
16
17
  fr: { plurals: fr },
17
18
  en: { plurals: en },
18
19
  })
19
20
 
20
- export default function LinguiProvider(props: LinguiProviderProps) {
21
- const { loader, locale = 'en', ...i18nProviderPRops } = props
21
+ export function LinguiProvider(props: LinguiProviderProps) {
22
+ const { loader, ssrLoader, locale, ...i18nProviderPRops } = props
22
23
 
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,13 @@
1
1
  {
2
2
  "name": "@graphcommerce/lingui-next",
3
- "version": "1.3.0",
3
+ "homepage": "https://www.graphcommerce.org/",
4
+ "repository": "github:graphcommerce-org/graphcommerce",
5
+ "version": "2.0.1",
4
6
  "sideEffects": false,
5
7
  "scripts": {
6
8
  "dev": "tsc -W"
7
9
  },
8
10
  "prettier": "@graphcommerce/prettier-config-pwa",
9
- "browserslist": [
10
- "extends @graphcommerce/browserslist-config-pwa"
11
- ],
12
11
  "eslintConfig": {
13
12
  "extends": "@graphcommerce/eslint-config-pwa",
14
13
  "parserOptions": {
@@ -16,21 +15,19 @@
16
15
  }
17
16
  },
18
17
  "dependencies": {
19
- "@lingui/core": "^3.13.0",
20
- "@lingui/macro": "^3.13.0",
21
- "@lingui/react": "^3.13.0"
18
+ "@lingui/core": "^3.13.2",
19
+ "@lingui/macro": "^3.13.2",
20
+ "@lingui/react": "^3.13.2"
22
21
  },
23
22
  "devDependencies": {
24
- "@graphcommerce/browserslist-config-pwa": "^3.0.3",
25
- "@graphcommerce/eslint-config-pwa": "^3.1.10",
26
- "@graphcommerce/prettier-config-pwa": "^3.0.5",
27
- "@graphcommerce/typescript-config-pwa": "^3.1.2",
28
- "@playwright/test": "^1.17.1"
23
+ "@graphcommerce/eslint-config-pwa": "^4.0.1",
24
+ "@graphcommerce/prettier-config-pwa": "^4.0.1",
25
+ "@graphcommerce/typescript-config-pwa": "^4.0.1",
26
+ "@playwright/test": "^1.18.1"
29
27
  },
30
28
  "peerDependencies": {
31
- "next": "^12.0.7",
29
+ "next": "^12.0.10",
32
30
  "react": "^17.0.2",
33
31
  "react-dom": "^17.0.2"
34
- },
35
- "gitHead": "bc5423d7547f8685db4cd8fc6d8f7a2a51ebed05"
32
+ }
36
33
  }
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
- }