-
-
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.
🔀 merge pull request #1 from onRuntime/antoinekm/path-detection
Antoinekm/path detection
- Loading branch information
Showing
11 changed files
with
101 additions
and
20 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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,3 @@ | ||
{ | ||
"welcome": "Bienvenue" | ||
} |
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,11 +1,13 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
/** @type {import('next').NextConfig} */ | ||
|
||
const config = { | ||
reactStrictMode: true, | ||
swcMinify: true, | ||
i18n: { | ||
locales: ["en", "fr"], | ||
defaultLocale: "en", | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
module.exports = config | ||
module.exports = config; |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { useRouter } from "next/router"; | ||
|
||
const useTranslation = (namespace = "common", keySplit = true) => { | ||
const router = useRouter(); | ||
const { locale, locales } = router; | ||
let translation: any; | ||
|
||
if (!translation) { | ||
try { | ||
translation = require(`../../../../locales/${locale}/${namespace}.json`); | ||
} catch (error) { | ||
// An error occurred while loading the translation file without src | ||
} | ||
} | ||
|
||
if (!translation) { | ||
try { | ||
translation = require(`../../../../src/locales/${locale}/${namespace}.json`); | ||
} catch (error) { | ||
// An error occurred while loading the translation file with src | ||
} | ||
} | ||
|
||
// Try to get the fallback translation | ||
|
||
if (!translation) { | ||
try { | ||
translation = require(`../../../../locales/${ | ||
locales && locales.length > 0 ? locales[0] : "en" | ||
}/${namespace}.json`); | ||
} catch (err) { | ||
// An error occurred while loading the fallback translation file without src | ||
} | ||
} | ||
|
||
if (!translation) { | ||
try { | ||
translation = require(`../../../../src/locales/${ | ||
locales && locales.length > 0 ? locales[0] : "en" | ||
}/${namespace}.json`); | ||
} catch (err) { | ||
// An error occurred while loading the fallback translation file with src | ||
} | ||
} | ||
|
||
// If no translation was found, console an error | ||
|
||
if (!translation) { | ||
console.error( | ||
`An error occurred while loading the translation file for namespace ${namespace} and locale ${locale}` | ||
); | ||
} | ||
|
||
const translate = (key: string, variables: string[] = []) => { | ||
const keyList = keySplit ? key.split(".") : [key]; | ||
let parent = translation; | ||
keyList.forEach((k) => (parent = parent[k] ?? key)); | ||
|
||
/* | ||
* parent can countain %s1, %s2, ... | ||
* variables can contain "value1", "value2", ... | ||
* we replace the %s1, %s2, ... with the corresponding value | ||
*/ | ||
if (parent && variables.length > 0) { | ||
parent = parent.replace( | ||
/%s(\d+)/g, | ||
(_: any, index: any) => variables[parseInt(index) - 1] ?? undefined | ||
); | ||
} | ||
return parent || key; | ||
}; | ||
|
||
return { t: translate, locale }; | ||
}; | ||
|
||
export default useTranslation; |
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,3 @@ | ||
import useTranslation from "./hooks/useTranslations"; | ||
|
||
export default useTranslation; |
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
077330f
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:
next-usetranslation-with-typescript – ./
next-usetranslation-with-typescript.vercel.app
next-usetranslation-with-typescript-git-master-onruntime.vercel.app
next-usetranslation-with-typescript-onruntime.vercel.app