Skip to content

Commit

Permalink
🔀 merge pull request #1 from onRuntime/antoinekm/path-detection
Browse files Browse the repository at this point in the history
Antoinekm/path detection
  • Loading branch information
AntoineKM authored Aug 29, 2022
2 parents d34f37a + 611b6b9 commit 077330f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 20 deletions.
1 change: 1 addition & 0 deletions examples/with-typescript/locales/en/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions examples/with-typescript/locales/fr/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"welcome": "Bienvenue"
}
8 changes: 5 additions & 3 deletions examples/with-typescript/next.config.js
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;
1 change: 1 addition & 0 deletions examples/with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@onruntime/next-usetranslation": "^0.1.10",
"next": "12.2.5",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
9 changes: 3 additions & 6 deletions examples/with-typescript/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import type { NextPage } from "next";
import Head from "next/head";
import Image from "next/image";
import styles from "../styles/Home.module.css";
import useTranslation from "../../../src";
import useTranslation from "@onruntime/next-usetranslation";

const Home: NextPage = () => {
const { locale } = useTranslation();
console.log("locale", locale);
const { t } = useTranslation();
return (
<div className={styles.container}>
<Head>
Expand All @@ -16,9 +15,7 @@ const Home: NextPage = () => {
</Head>

<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<h1 className={styles.title}>{t("welcome")}</h1>

<p className={styles.description}>
{"Get started by editing "}
Expand Down
5 changes: 5 additions & 0 deletions examples/with-typescript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@onruntime/next-usetranslation@^0.1.10":
version "0.1.10"
resolved "https://registry.yarnpkg.com/@onruntime/next-usetranslation/-/next-usetranslation-0.1.10.tgz#806a1f71dc348ed8e1a954f8aa2074ad33e55057"
integrity sha512-FM6rHl+wEhS8acZ+CHTfl3gV/dswrDjXobaYGG1Pzl1uldZyMORPsy3FJIjgWukTNS5l87SsaxdmsvOiVzngHA==

"@rushstack/eslint-patch@^1.1.3":
version "1.1.4"
resolved "https://registry.yarnpkg.com/@rushstack/eslint-patch/-/eslint-patch-1.1.4.tgz#0c8b74c50f29ee44f423f7416829c0bf8bb5eb27"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0",
"version": "0.1.10",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -60,6 +60,7 @@
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"husky": "^8.0.1",
"next": "^12.2.5",
"prettier": "^2.7.1",
Expand Down
77 changes: 77 additions & 0 deletions src/hooks/useTranslations.ts
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;
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import useTranslation from "./hooks/useTranslations";

export default useTranslation;
9 changes: 0 additions & 9 deletions src/index.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3082,7 +3082,7 @@ eslint-plugin-react-hooks@^2.2.0:
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-2.5.1.tgz#4ef5930592588ce171abeb26f400c7fbcbc23cd0"
integrity sha512-Y2c4b55R+6ZzwtTppKwSmK/Kar8AdLiC2f9NADCuxbcTgPPg41Gyqa6b9GppgXSvCtkRw43ZE86CT5sejKC6/g==

eslint-plugin-react-hooks@^4.5.0:
eslint-plugin-react-hooks@^4.5.0, eslint-plugin-react-hooks@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz#4c3e697ad95b77e93f8646aaa1630c1ba607edd3"
integrity sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==
Expand Down

1 comment on commit 077330f

@vercel
Copy link

@vercel vercel bot commented on 077330f Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.