From d51200deeb6d63c8f9c4af70bc8aa66ad55b2585 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Wed, 17 Aug 2022 12:47:44 +0200 Subject: [PATCH 01/18] =?UTF-8?q?=F0=9F=9A=A7=20add=20work=20on=20path=20u?= =?UTF-8?q?sage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../with-typescript/locales/en/common.json | 1 + .../with-typescript/locales/fr/common.json | 1 + examples/with-typescript/next.config.js | 10 ++-- examples/with-typescript/package.json | 1 + examples/with-typescript/yarn.lock | 5 ++ package.json | 4 ++ src/index.tsx | 48 +++++++++++++++++-- yarn.lock | 30 ++++++++++++ 8 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 examples/with-typescript/locales/en/common.json create mode 100644 examples/with-typescript/locales/fr/common.json diff --git a/examples/with-typescript/locales/en/common.json b/examples/with-typescript/locales/en/common.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/examples/with-typescript/locales/en/common.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/examples/with-typescript/locales/fr/common.json b/examples/with-typescript/locales/fr/common.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/examples/with-typescript/locales/fr/common.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/examples/with-typescript/next.config.js b/examples/with-typescript/next.config.js index 39a3fe4..9d23084 100644 --- a/examples/with-typescript/next.config.js +++ b/examples/with-typescript/next.config.js @@ -1,11 +1,15 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ /** @type {import('next').NextConfig} */ + +const { withTranslation } = require("@onruntime/next-usetranslation"); + const config = { reactStrictMode: true, swcMinify: true, i18n: { locales: ["en", "fr"], defaultLocale: "en", - } -} + }, +}; -module.exports = config +module.exports = withTranslation(config); diff --git a/examples/with-typescript/package.json b/examples/with-typescript/package.json index 02386ce..6536c6f 100644 --- a/examples/with-typescript/package.json +++ b/examples/with-typescript/package.json @@ -9,6 +9,7 @@ "lint": "next lint" }, "dependencies": { + "fs": "^0.0.1-security", "next": "12.2.5", "react": "18.2.0", "react-dom": "18.2.0" diff --git a/examples/with-typescript/yarn.lock b/examples/with-typescript/yarn.lock index 620d12c..e1dcf45 100644 --- a/examples/with-typescript/yarn.lock +++ b/examples/with-typescript/yarn.lock @@ -814,6 +814,11 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== +fs@^0.0.1-security: + version "0.0.1-security" + resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" + integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w== + function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" diff --git a/package.json b/package.json index 6b30277..ec6c445 100644 --- a/package.json +++ b/package.json @@ -69,5 +69,9 @@ "tsdx": "^0.14.1", "tslib": "^2.4.0", "typescript": "^4.7.4" + }, + "dependencies": { + "fs": "^0.0.1-security", + "path": "^0.12.7" } } diff --git a/src/index.tsx b/src/index.tsx index 4aa7635..93b3cc2 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,9 +1,51 @@ +import { NextConfig } from "next"; +import { NextJsWebpackConfig } from "next/dist/server/config-shared"; import { useRouter } from "next/router"; +import path from "path"; +import fs from "fs"; -const useTranslation = () => { +const useTranslation = ( + namespace = "common", + keySplit = true + ) => { const router = useRouter(); - const { locale } = router; + const { locale, locales } = router; + + fs; + path; + namespace; + keySplit; + locales; + + // check if project has /src/locale our /src folder + // const localePathWithoutSrc = path.join(process.cwd(), "locale"); + // const localePathWithSrc = path.join(process.cwd(), "src", "locale"); + // if (!fs.existsSync(localePathWithoutSrc) || !fs.existsSync(localePathWithSrc)) { + // return console.error("You need to create a locale folder in your project root or src folder"); + // } + + // const localePath = fs.existsSync(localePathWithoutSrc) ? localePathWithoutSrc : localePathWithSrc; + + // require the translation file in {project_directory}/src/locales/{locale}/{namespace}.json + console.log(locale); + return { locale }; }; -export default useTranslation; +export const withTranslation = (config: NextConfig): NextConfig => { + const webpack: NextJsWebpackConfig = (config) => { + config.resolve.fallback = { + fs: false, + path: false + } + + return config; + }; + + return { + ...config, + webpack, + } +} + +export default useTranslation; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 4bd9b9e..07d62aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3626,6 +3626,11 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== +fs@^0.0.1-security: + version "0.0.1-security" + resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" + integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w== + fsevents@^2.1.2, fsevents@~2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" @@ -3990,6 +3995,11 @@ inherits@2: resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== +inherits@2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + integrity sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw== + inquirer@^7.0.0: version "7.3.3" resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-7.3.3.tgz#04d176b2af04afc157a83fd7c100e98ee0aad003" @@ -5608,6 +5618,14 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== +path@^0.12.7: + version "0.12.7" + resolved "https://registry.yarnpkg.com/path/-/path-0.12.7.tgz#d4dc2a506c4ce2197eb481ebfcd5b36c0140b10f" + integrity sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q== + dependencies: + process "^0.11.1" + util "^0.10.3" + performance-now@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" @@ -5700,6 +5718,11 @@ pretty-format@^27.0.2: ansi-styles "^5.0.0" react-is "^17.0.1" +process@^0.11.1: + version "0.11.10" + resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" + integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A== + progress-estimator@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/progress-estimator/-/progress-estimator-0.2.2.tgz#1c3947a5782ea56e40c8fccc290ac7ceeb1b91cb" @@ -7038,6 +7061,13 @@ use@^3.1.0: resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== +util@^0.10.3: + version "0.10.4" + resolved "https://registry.yarnpkg.com/util/-/util-0.10.4.tgz#3aa0125bfe668a4672de58857d3ace27ecb76901" + integrity sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A== + dependencies: + inherits "2.0.3" + uuid@^3.3.2: version "3.4.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" From 4b670100cab1306c879cdc194cc2fe2c344e1217 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Wed, 17 Aug 2022 15:39:46 +0200 Subject: [PATCH 02/18] =?UTF-8?q?=F0=9F=9A=A7=20add=20work=20on=20not=20wo?= =?UTF-8?q?rking=20require?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../with-typescript/locales/fr/common.json | 4 +- examples/with-typescript/pages/index.tsx | 5 +- package.json | 2 +- src/hoc/withTranslation.ts | 20 ++++++++ src/index.ts | 45 ++++++++++++++++ src/index.tsx | 51 ------------------- yarn.lock | 7 +-- 7 files changed, 72 insertions(+), 62 deletions(-) create mode 100644 src/hoc/withTranslation.ts create mode 100644 src/index.ts delete mode 100644 src/index.tsx diff --git a/examples/with-typescript/locales/fr/common.json b/examples/with-typescript/locales/fr/common.json index 9e26dfe..77c7250 100644 --- a/examples/with-typescript/locales/fr/common.json +++ b/examples/with-typescript/locales/fr/common.json @@ -1 +1,3 @@ -{} \ No newline at end of file +{ + "welcome": "Bienvenue" +} \ No newline at end of file diff --git a/examples/with-typescript/pages/index.tsx b/examples/with-typescript/pages/index.tsx index 010da36..d301c5f 100644 --- a/examples/with-typescript/pages/index.tsx +++ b/examples/with-typescript/pages/index.tsx @@ -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); + useTranslation(); return (
diff --git a/package.json b/package.json index ec6c445..8fb4e66 100644 --- a/package.json +++ b/package.json @@ -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", @@ -71,7 +72,6 @@ "typescript": "^4.7.4" }, "dependencies": { - "fs": "^0.0.1-security", "path": "^0.12.7" } } diff --git a/src/hoc/withTranslation.ts b/src/hoc/withTranslation.ts new file mode 100644 index 0000000..9cfcc69 --- /dev/null +++ b/src/hoc/withTranslation.ts @@ -0,0 +1,20 @@ +import { NextConfig } from "next"; +import { NextJsWebpackConfig } from "next/dist/server/config-shared"; + +const withTranslation = (config: NextConfig): NextConfig => { + const webpack: NextJsWebpackConfig = (config) => { + config.resolve.fallback = { + fs: false, + path: false, + }; + + return config; + }; + + return { + ...config, + webpack, + }; +}; + +export default withTranslation; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..5719142 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,45 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +import { useRouter } from "next/router"; +import withTranslation from "./hoc/withTranslation"; +import path from "path"; + +const useTranslation = (namespace = "common", keySplit = true) => { + const router = useRouter(); + const { locale, locales } = router; + + path; + namespace; + keySplit; + locales; + + // check if project has /src/locales our /src folder + const localePathWithoutSrc = path.join( + process.cwd(), + "locales", + locale || "en", + `${namespace}.json` + ); + const localePathWithSrc = path.join( + process.cwd(), + "src", + "locales", + locale || "en", + `${namespace}.json` + ); + + const translationsWithoutSrc = require(localePathWithoutSrc); + const translationsWithSrc = require(localePathWithSrc); + + const translations = translationsWithSrc || translationsWithoutSrc; + + console.log("translations", translations); + + // require the translation file in {project_directory}/src/locales/{locale}/{namespace}.json + + console.log(locale); + + return { locale }; +}; + +export { withTranslation }; +export default useTranslation; diff --git a/src/index.tsx b/src/index.tsx deleted file mode 100644 index 93b3cc2..0000000 --- a/src/index.tsx +++ /dev/null @@ -1,51 +0,0 @@ -import { NextConfig } from "next"; -import { NextJsWebpackConfig } from "next/dist/server/config-shared"; -import { useRouter } from "next/router"; -import path from "path"; -import fs from "fs"; - -const useTranslation = ( - namespace = "common", - keySplit = true - ) => { - const router = useRouter(); - const { locale, locales } = router; - - fs; - path; - namespace; - keySplit; - locales; - - // check if project has /src/locale our /src folder - // const localePathWithoutSrc = path.join(process.cwd(), "locale"); - // const localePathWithSrc = path.join(process.cwd(), "src", "locale"); - // if (!fs.existsSync(localePathWithoutSrc) || !fs.existsSync(localePathWithSrc)) { - // return console.error("You need to create a locale folder in your project root or src folder"); - // } - - // const localePath = fs.existsSync(localePathWithoutSrc) ? localePathWithoutSrc : localePathWithSrc; - - // require the translation file in {project_directory}/src/locales/{locale}/{namespace}.json - console.log(locale); - - return { locale }; -}; - -export const withTranslation = (config: NextConfig): NextConfig => { - const webpack: NextJsWebpackConfig = (config) => { - config.resolve.fallback = { - fs: false, - path: false - } - - return config; - }; - - return { - ...config, - webpack, - } -} - -export default useTranslation; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 07d62aa..56af8b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== @@ -3626,11 +3626,6 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== -fs@^0.0.1-security: - version "0.0.1-security" - resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" - integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w== - fsevents@^2.1.2, fsevents@~2.3.2: version "2.3.2" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a" From 7d251840c931f16450cde97cad598496f09fc816 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 10:10:31 +0200 Subject: [PATCH 03/18] v0.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8fb4e66..141d5c9 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 74704dbf0599ff39bec897dbe326248bf198667e Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 10:19:01 +0200 Subject: [PATCH 04/18] v0.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 141d5c9..f45afe5 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.1", + "version": "0.1.2", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 2dc1ee4485a25bb4aee5aaa8f955141c2af4a0ac Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 10:28:24 +0200 Subject: [PATCH 05/18] v0.1.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f45afe5..00d554a 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.2", + "version": "0.1.3", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 7d9db86798ef730c41f99257a123498d68d4f09e Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 10:38:35 +0200 Subject: [PATCH 06/18] v0.1.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 00d554a..78dcc6c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.3", + "version": "0.1.4", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 80b57b0dcd068bcc9ccb030dfd6062251749221b Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 10:44:13 +0200 Subject: [PATCH 07/18] v0.1.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 78dcc6c..ccf6cc2 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.4", + "version": "0.1.5", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 90fb7b2849d02718119c52ea81194ca8d3992d6c Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 10:55:58 +0200 Subject: [PATCH 08/18] v0.1.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ccf6cc2..dda3836 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.5", + "version": "0.1.6", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From fcdfe6c12703d07c5f8f1c7d7fd048ddf9eb2d69 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:11:36 +0200 Subject: [PATCH 09/18] v0.1.7 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index dda3836..18d3c67 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.6", + "version": "0.1.7", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", @@ -72,6 +72,6 @@ "typescript": "^4.7.4" }, "dependencies": { - "path": "^0.12.7" + "fs": "^0.0.1-security" } } From 9e2bc73dd74f3659bf3fbe9247c5a13dca6612e2 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:17:09 +0200 Subject: [PATCH 10/18] v0.1.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 18d3c67..da0f8ca 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.7", + "version": "0.1.8", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 9f46b4926ebbcf2850fc2a22d3e10f16757745f6 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:18:57 +0200 Subject: [PATCH 11/18] v0.1.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index da0f8ca..ef0a1af 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.8", + "version": "0.1.9", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 0ad301fc35fd298a96b6a15211c9f7c4e3602830 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:23:34 +0200 Subject: [PATCH 12/18] =?UTF-8?q?=E2=9E=96=20remove=20fs=20from=20examples?= =?UTF-8?q?/with-typescript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/with-typescript/package.json | 2 +- examples/with-typescript/yarn.lock | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/with-typescript/package.json b/examples/with-typescript/package.json index 6536c6f..41fb776 100644 --- a/examples/with-typescript/package.json +++ b/examples/with-typescript/package.json @@ -9,7 +9,7 @@ "lint": "next lint" }, "dependencies": { - "fs": "^0.0.1-security", + "@onruntime/next-usetranslation": "^0.1.9", "next": "12.2.5", "react": "18.2.0", "react-dom": "18.2.0" diff --git a/examples/with-typescript/yarn.lock b/examples/with-typescript/yarn.lock index e1dcf45..220821a 100644 --- a/examples/with-typescript/yarn.lock +++ b/examples/with-typescript/yarn.lock @@ -149,6 +149,13 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" +"@onruntime/next-usetranslation@^0.1.9": + version "0.1.9" + resolved "https://registry.yarnpkg.com/@onruntime/next-usetranslation/-/next-usetranslation-0.1.9.tgz#525f9a7d6c5be4629a4db5e203403685fedadd0f" + integrity sha512-YiwTu3AEEIOY13kvYErbFwHHfjY1ZW5wtr6r51zMwuiS0zbzeNzItsjSQSCEQQj9ZRARU4NXBuyyv5Pfm8RlFQ== + dependencies: + fs "^0.0.1-security" + "@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" From 18e4200524e5032a773762229796f5d3b663bcac Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:24:59 +0200 Subject: [PATCH 13/18] =?UTF-8?q?=E2=9E=96=20remove=20fs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 --- yarn.lock | 25 ------------------------- 2 files changed, 28 deletions(-) diff --git a/package.json b/package.json index ef0a1af..ba99d06 100644 --- a/package.json +++ b/package.json @@ -70,8 +70,5 @@ "tsdx": "^0.14.1", "tslib": "^2.4.0", "typescript": "^4.7.4" - }, - "dependencies": { - "fs": "^0.0.1-security" } } diff --git a/yarn.lock b/yarn.lock index 56af8b4..b2c03d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3990,11 +3990,6 @@ inherits@2: resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== -inherits@2.0.3: - version "2.0.3" - resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" - integrity sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw== - inquirer@^7.0.0: version "7.3.3" resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-7.3.3.tgz#04d176b2af04afc157a83fd7c100e98ee0aad003" @@ -5613,14 +5608,6 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== -path@^0.12.7: - version "0.12.7" - resolved "https://registry.yarnpkg.com/path/-/path-0.12.7.tgz#d4dc2a506c4ce2197eb481ebfcd5b36c0140b10f" - integrity sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q== - dependencies: - process "^0.11.1" - util "^0.10.3" - performance-now@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" @@ -5713,11 +5700,6 @@ pretty-format@^27.0.2: ansi-styles "^5.0.0" react-is "^17.0.1" -process@^0.11.1: - version "0.11.10" - resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" - integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A== - progress-estimator@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/progress-estimator/-/progress-estimator-0.2.2.tgz#1c3947a5782ea56e40c8fccc290ac7ceeb1b91cb" @@ -7056,13 +7038,6 @@ use@^3.1.0: resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== -util@^0.10.3: - version "0.10.4" - resolved "https://registry.yarnpkg.com/util/-/util-0.10.4.tgz#3aa0125bfe668a4672de58857d3ace27ecb76901" - integrity sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A== - dependencies: - inherits "2.0.3" - uuid@^3.3.2: version "3.4.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" From 015de76b21d418e486fad534b5580e0e63985416 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:25:31 +0200 Subject: [PATCH 14/18] =?UTF-8?q?=F0=9F=94=A5=20remove=20withtranslation?= =?UTF-8?q?=20hoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/with-typescript/next.config.js | 4 +--- src/hoc/withTranslation.ts | 20 -------------------- 2 files changed, 1 insertion(+), 23 deletions(-) delete mode 100644 src/hoc/withTranslation.ts diff --git a/examples/with-typescript/next.config.js b/examples/with-typescript/next.config.js index 9d23084..36f5b22 100644 --- a/examples/with-typescript/next.config.js +++ b/examples/with-typescript/next.config.js @@ -1,8 +1,6 @@ /* eslint-disable @typescript-eslint/no-var-requires */ /** @type {import('next').NextConfig} */ -const { withTranslation } = require("@onruntime/next-usetranslation"); - const config = { reactStrictMode: true, swcMinify: true, @@ -12,4 +10,4 @@ const config = { }, }; -module.exports = withTranslation(config); +module.exports = config; diff --git a/src/hoc/withTranslation.ts b/src/hoc/withTranslation.ts deleted file mode 100644 index 9cfcc69..0000000 --- a/src/hoc/withTranslation.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { NextConfig } from "next"; -import { NextJsWebpackConfig } from "next/dist/server/config-shared"; - -const withTranslation = (config: NextConfig): NextConfig => { - const webpack: NextJsWebpackConfig = (config) => { - config.resolve.fallback = { - fs: false, - path: false, - }; - - return config; - }; - - return { - ...config, - webpack, - }; -}; - -export default withTranslation; From a4914136c08e2ccf6b0838a8e9749c439b937f5e Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:26:21 +0200 Subject: [PATCH 15/18] =?UTF-8?q?=E2=9C=A8=20add=20usetranslation=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useTranslations.ts | 77 ++++++++++++++++++++++++++++++++++++ src/index.ts | 44 +-------------------- 2 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 src/hooks/useTranslations.ts diff --git a/src/hooks/useTranslations.ts b/src/hooks/useTranslations.ts new file mode 100644 index 0000000..11691f9 --- /dev/null +++ b/src/hooks/useTranslations.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index 5719142..2a6c479 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,45 +1,3 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -import { useRouter } from "next/router"; -import withTranslation from "./hoc/withTranslation"; -import path from "path"; +import useTranslation from "./hooks/useTranslations"; -const useTranslation = (namespace = "common", keySplit = true) => { - const router = useRouter(); - const { locale, locales } = router; - - path; - namespace; - keySplit; - locales; - - // check if project has /src/locales our /src folder - const localePathWithoutSrc = path.join( - process.cwd(), - "locales", - locale || "en", - `${namespace}.json` - ); - const localePathWithSrc = path.join( - process.cwd(), - "src", - "locales", - locale || "en", - `${namespace}.json` - ); - - const translationsWithoutSrc = require(localePathWithoutSrc); - const translationsWithSrc = require(localePathWithSrc); - - const translations = translationsWithSrc || translationsWithoutSrc; - - console.log("translations", translations); - - // require the translation file in {project_directory}/src/locales/{locale}/{namespace}.json - - console.log(locale); - - return { locale }; -}; - -export { withTranslation }; export default useTranslation; From 9fa4a3c3812bc4a70a5af50c45ec58d804757c1c Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:28:04 +0200 Subject: [PATCH 16/18] =?UTF-8?q?=F0=9F=8E=A8=20add=20an=20example=20of=20?= =?UTF-8?q?the=20usetranslation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/with-typescript/pages/index.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/with-typescript/pages/index.tsx b/examples/with-typescript/pages/index.tsx index d301c5f..4bb2d7e 100644 --- a/examples/with-typescript/pages/index.tsx +++ b/examples/with-typescript/pages/index.tsx @@ -5,7 +5,7 @@ import styles from "../styles/Home.module.css"; import useTranslation from "@onruntime/next-usetranslation"; const Home: NextPage = () => { - useTranslation(); + const { t } = useTranslation(); return (
@@ -15,9 +15,7 @@ const Home: NextPage = () => {
-

- Welcome to Next.js! -

+

{t("welcome")}

{"Get started by editing "} From 141e51206e3489549e61b70375a7b0ff5cbe7e97 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:29:00 +0200 Subject: [PATCH 17/18] v0.1.10 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ba99d06..48dc944 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.9", + "version": "0.1.10", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 611b6b90da16b7ec98578f12d51eab7975d3b851 Mon Sep 17 00:00:00 2001 From: Antoine Kingue Date: Mon, 29 Aug 2022 11:30:25 +0200 Subject: [PATCH 18/18] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20upgrade=20@onruntime?= =?UTF-8?q?/next-usetranslation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/with-typescript/package.json | 2 +- examples/with-typescript/yarn.lock | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/examples/with-typescript/package.json b/examples/with-typescript/package.json index 41fb776..af08078 100644 --- a/examples/with-typescript/package.json +++ b/examples/with-typescript/package.json @@ -9,7 +9,7 @@ "lint": "next lint" }, "dependencies": { - "@onruntime/next-usetranslation": "^0.1.9", + "@onruntime/next-usetranslation": "^0.1.10", "next": "12.2.5", "react": "18.2.0", "react-dom": "18.2.0" diff --git a/examples/with-typescript/yarn.lock b/examples/with-typescript/yarn.lock index 220821a..d862697 100644 --- a/examples/with-typescript/yarn.lock +++ b/examples/with-typescript/yarn.lock @@ -149,12 +149,10 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@onruntime/next-usetranslation@^0.1.9": - version "0.1.9" - resolved "https://registry.yarnpkg.com/@onruntime/next-usetranslation/-/next-usetranslation-0.1.9.tgz#525f9a7d6c5be4629a4db5e203403685fedadd0f" - integrity sha512-YiwTu3AEEIOY13kvYErbFwHHfjY1ZW5wtr6r51zMwuiS0zbzeNzItsjSQSCEQQj9ZRARU4NXBuyyv5Pfm8RlFQ== - dependencies: - fs "^0.0.1-security" +"@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" @@ -821,11 +819,6 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw== -fs@^0.0.1-security: - version "0.0.1-security" - resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4" - integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w== - function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"