-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unblock lingui and allow for SSR on almost all pages (#3999)
## What does this PR do and why? The big kahuna - this breaks lingui out into SSR and will not block for client side compilation/rendering ## Screenshots or screen recordings _If applicable, provide screenshots or screen recordings to demonstrate the changes._ ## Acceptance checklist - [ ] I have evaluated the [Approval Guidelines](https://github.com/jbx-protocol/juice-interface/blob/main/CONTRIBUTING.md#approval-guidelines) for this PR. - [ ] (if relevant) I have tested this PR in [all supported browsers](https://github.com/jbx-protocol/juice-interface/blob/main/CONTRIBUTING.md#supported-browsers). - [ ] (if relevant) I have tested this PR in dark mode and light mode (if applicable).
- Loading branch information
1 parent
f776a49
commit dc43f7f
Showing
40 changed files
with
424 additions
and
142 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { formatter } = require('@lingui/format-po') | ||
|
||
const locales = ['en', 'zh'] | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
locales.push('pseudo') | ||
} | ||
|
||
/** @type {import('@lingui/conf').LinguiConfig} */ | ||
module.exports = { | ||
locales: locales, | ||
sourceLocale: 'en', | ||
pseudoLocale: 'pseudo', | ||
catalogs: [ | ||
{ | ||
path: 'src/locales/{locale}/messages', | ||
include: ['src'], | ||
}, | ||
], | ||
format: formatter({ origins: false, lineNumbers: false }), | ||
orderBy: 'messageId', | ||
fallbackLocales: { | ||
default: 'en', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,33 @@ | ||
import { i18n } from '@lingui/core' | ||
import { | ||
detect, | ||
fromNavigator, | ||
fromStorage, | ||
fromUrl, | ||
} from '@lingui/detect-locale' | ||
import { Messages } from '@lingui/core' | ||
import { I18nProvider } from '@lingui/react' | ||
import defaultLocale from 'locales/en/messages' | ||
import { ReactNode } from 'react' | ||
import { useLingUiInit } from 'hooks/useLinguiInit' | ||
import React from 'react' | ||
|
||
import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from 'constants/locale' | ||
|
||
const getLocale = (): string => { | ||
if (typeof window === 'undefined') return DEFAULT_LOCALE | ||
|
||
let locale = | ||
detect(fromUrl('lang'), fromStorage('lang'), fromNavigator()) ?? | ||
DEFAULT_LOCALE | ||
|
||
if (!SUPPORTED_LOCALES.includes(locale)) { | ||
locale = DEFAULT_LOCALE | ||
} | ||
|
||
return locale | ||
} | ||
|
||
const activateDefaultLocale = () => { | ||
const { messages } = defaultLocale | ||
i18n.load(DEFAULT_LOCALE, messages) | ||
i18n.activate(DEFAULT_LOCALE) | ||
export type I18nProviderProps = { | ||
children: React.ReactNode | ||
i18n?: { messages: Messages; locale: string } | ||
} | ||
|
||
const dynamicActivate = async (locale: string) => { | ||
try { | ||
const { messages } = await import(`../../locales/${locale}/messages`) | ||
let i18nSingleton: { messages: Messages; locale: string } | undefined | ||
|
||
i18n.load(locale, messages) | ||
i18n.activate(locale) | ||
} catch (e) { | ||
console.error(`Error loading locale "${locale}:"`, e) | ||
// fall back to default locale | ||
activateDefaultLocale() | ||
export const LanguageProvider: React.FC<I18nProviderProps> = ({ | ||
children, | ||
i18n: _i18n, | ||
}) => { | ||
if (_i18n) { | ||
i18nSingleton = _i18n | ||
} else { | ||
_i18n = i18nSingleton | ||
} | ||
} | ||
|
||
const locale = getLocale() | ||
if (locale === DEFAULT_LOCALE) { | ||
activateDefaultLocale() | ||
} else { | ||
dynamicActivate(locale) | ||
} | ||
if (!_i18n) | ||
throw new Error( | ||
'i18n must be provided at least once. This is usually done in _app.tsx', | ||
) | ||
|
||
const messages = _i18n?.messages ?? [] | ||
const locale = _i18n?.locale ?? 'en' | ||
const i18n = useLingUiInit(messages, locale) | ||
|
||
export default function LanguageProvider({ | ||
children, | ||
}: { | ||
children: ReactNode | ||
}) { | ||
return <I18nProvider i18n={i18n}>{children}</I18nProvider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// generate mock for LanguageProvider | ||
|
||
import { I18nProviderProps } from '../LanguageProvider' | ||
|
||
export const LanguageProvider: React.FC<I18nProviderProps> = ({ children }) => { | ||
return <div>{children}</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// https://github.com/lingui/js-lingui/pull/1541 | ||
|
||
import { i18n, Messages } from '@lingui/core' | ||
import { useEffect } from 'react' | ||
|
||
export const useLingUiInit = (messages: Messages, locale: string) => { | ||
const isClient = typeof window !== 'undefined' | ||
|
||
if (!isClient && locale !== i18n.locale) { | ||
// there is single instance of i18n on the server | ||
i18n.loadAndActivate({ locale, messages }) | ||
} | ||
if (isClient && i18n.locale === undefined) { | ||
// first client render | ||
i18n.loadAndActivate({ locale, messages }) | ||
} | ||
|
||
useEffect(() => { | ||
const localeDidChange = locale !== i18n.locale | ||
if (localeDidChange) { | ||
i18n.loadAndActivate({ locale, messages }) | ||
} | ||
}, [locale, messages]) | ||
|
||
return i18n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export async function loadCatalog(locale: string) { | ||
const { messages } = await import(`@lingui/loader!./${locale}/messages.po`) | ||
|
||
return messages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
dc43f7f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
juice-interface-goerli – ./
juice-interface-goerli.vercel.app
juice-interface-goerli-peel.vercel.app
juice-interface-goerli-git-main-peel.vercel.app
goerli.juicebox.money