forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update with-react-intl example (vercel#40999)
## Changes see commits ## Additional Info Since this example is quite complex I tried to remove additional noise (unused type inference). ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm lint` - [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) ## Related Closes: vercel#40975
- Loading branch information
Showing
11 changed files
with
65 additions
and
72 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -35,3 +35,6 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
# i18n | ||
compiled-lang |
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 @@ | ||
enable-pre-post-scripts=true |
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
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,35 +1,21 @@ | ||
import fs from 'fs/promises' | ||
import path from 'path' | ||
export type MessageConfig = Record<string, string> | ||
|
||
type LoadI18nMessagesProps = { | ||
locale: string | ||
defaultLocale: string | ||
} | ||
|
||
type MessageConfig = { [key: string]: string } | ||
|
||
export default async function loadI18nMessages({ | ||
locale, | ||
defaultLocale, | ||
}: LoadI18nMessagesProps): Promise<MessageConfig> { | ||
export default async function loadI18nMessages( | ||
locale: string, | ||
defaultLocale = 'en' | ||
) { | ||
// If the default locale is being used we can skip it | ||
if (locale === defaultLocale) { | ||
return {} | ||
} | ||
|
||
if (locale !== defaultLocale) { | ||
const languagePath = path.join( | ||
process.cwd(), | ||
`compiled-lang/${locale}.json` | ||
try { | ||
return import(`../compiled-lang/${locale}.json`).then( | ||
(module) => module.default | ||
) | ||
} catch (error) { | ||
throw new Error( | ||
'Could not load compiled language files. Please run "npm run i18n:compile" first"' | ||
) | ||
try { | ||
const contents = await fs.readFile(languagePath, 'utf-8') | ||
return JSON.parse(contents) | ||
} catch (error) { | ||
console.info( | ||
'Could not load compiled language files. Please run "npm run i18n:compile" first"' | ||
) | ||
console.error(error) | ||
} | ||
} | ||
} |
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