Effortlessly internationalize your app with our npm i18n package. Easily manage translations for diverse audiences, ensuring a user-friendly, global experience.
# npm
npm install @drawilet/i18n
# yarn
yarn add @drawilet/i18n
As a first step, you must choose a translation strategy.
Avaiable strategies
Name | api key | limits | Proxy agent |
---|---|---|---|
❌ | ✔️ | ✅ |
-
Execute the following command to create the configuration file.
npx i18n init
This should generate a configuration file i18n.config.js similar to this:
const GoogleStrategy = require("@drawilet/i18n/strategies/google").default; module.exports = { strategy: new GoogleStrategy(), locales: ["es", "en"], defaultLocale: "en", output_path: __dirname + "/src/locales/", cache_path: __dirname + "/cache/i18n.json", output_mode: "separated", inputs: [__dirname + "/src/routes"], };
Configure it according your project requirements.
-
In each file (page, route) of your project, you should export
_i18n
in this way:export const _i18n = { key: "value", };
-
Run
npx i18n generate
to generate the files. -
Use the i18n client to get the translations.
Use our clients for a native-like experience. 🌟
import I18nClient from "@drawilet/i18n/Client"; const I18N = new I18nClient(); const I18nRoutes = I18N.instance() // type (optional) // You can get the translations using the "get" method I18nRoutes.get("locale", "path", "key"); // or creating a controller (ideal for pages or routes) const i18n = I18nRoutes.Controller("locale", "path"); // and use it infite times i18n.get("title"); i18n.get("description");
new GoogleStrategy("proxy_url");
proxy_url (optional)
: It is the proxy that will be used to avoid rate limits.
You can obtain one here .
WARNING: Use this only for personal projects.
- nextjs-i18n - A client for Next.js
More clients are being created.