remix-i18next
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,2 @@ | ||
export * from "./fetch"; | ||
export * from "./fs"; |
@@ -13,2 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./fetch"), exports); | ||
__exportStar(require("./fs"), exports); |
import type { Backend, Language } from "./backend"; | ||
interface RemixI18NextOptions { | ||
/** | ||
* Define the list of supported languages, this is used to determine if one of | ||
* the languages requested by the user is supported by the application. | ||
* This should be be same as the supportedLngs in the i18next options. | ||
*/ | ||
supportedLanguages: string[]; | ||
/** | ||
* Define the fallback language that it's going to be used in the case user | ||
* expected language is not supported. | ||
* This should be be same as the fallbackLng in the i18next options. | ||
*/ | ||
fallbackLng: string; | ||
/** | ||
* A class that implements the Cache interface and is used to store the | ||
* languages, in production, between requests to avoid loading them multiple | ||
* times, this is used so the user doesn't have to wait for the backend to | ||
* retrieve the translations every time. | ||
* By default, remix-i18next uses an in memory cache based on an ES Map | ||
* instance. | ||
*/ | ||
cache?: Cache; | ||
/** | ||
* If enabled, the cache will be used even in development mode. | ||
* This is disabled by default so while you code the languages are going to | ||
* be requested again on every request and be up-to-date. | ||
* Enabling may be useful if you request your translations from a server and | ||
* have a quote or rate limit on the number of requests. | ||
*/ | ||
cacheInDevelopment?: boolean; | ||
} | ||
export interface CacheKey { | ||
namespace: string; | ||
locale: string; | ||
} | ||
export interface Cache { | ||
get(key: CacheKey): Promise<Language | null>; | ||
set(key: CacheKey, value: Language): Promise<void>; | ||
has(key: CacheKey): Promise<boolean>; | ||
} | ||
export declare class RemixI18Next { | ||
private backend; | ||
private options; | ||
private cache; | ||
constructor(backend: Backend, options: RemixI18NextOptions); | ||
@@ -13,3 +50,5 @@ getTranslations(request: Request, namespaces: string | string[]): Promise<Record<string, Language>>; | ||
private getFromSupported; | ||
private getTranslation; | ||
private get cacheEnabled(); | ||
} | ||
export {}; |
39
index.js
@@ -8,5 +8,7 @@ "use strict"; | ||
options; | ||
cache; | ||
constructor(backend, options) { | ||
this.backend = backend; | ||
this.options = options; | ||
this.cache = options.cache ?? new InMemoryCache(); | ||
} | ||
@@ -16,7 +18,10 @@ async getTranslations(request, namespaces) { | ||
if (Array.isArray(namespaces)) { | ||
let messages = await Promise.all(namespaces.map((namespace) => this.backend.getTranslations(namespace, locale))); | ||
let messages = await Promise.all(namespaces.map((namespace) => this.getTranslation({ namespace, locale }))); | ||
return Object.fromEntries(messages.map((message, index) => [namespaces[index], message])); | ||
} | ||
return { | ||
[namespaces]: await this.backend.getTranslations(namespaces, locale), | ||
[namespaces]: await this.getTranslation({ | ||
namespace: namespaces, | ||
locale, | ||
}), | ||
}; | ||
@@ -46,3 +51,33 @@ } | ||
} | ||
async getTranslation(key) { | ||
if (this.cacheEnabled) { | ||
let cached = await this.cache.get(key); | ||
if (cached) | ||
return cached; | ||
} | ||
let translations = await this.backend.getTranslations(key.namespace, key.locale); | ||
if (this.cacheEnabled) { | ||
await this.cache.set(key, translations); | ||
} | ||
return translations; | ||
} | ||
get cacheEnabled() { | ||
return (this.options.cacheInDevelopment && process.env.NODE_ENV === "development"); | ||
} | ||
} | ||
exports.RemixI18Next = RemixI18Next; | ||
class InMemoryCache { | ||
cache = new Map(); | ||
async set(key, value) { | ||
this.cache.set(this.serialize(key), value); | ||
} | ||
async get(key) { | ||
return this.cache.get(this.serialize(key)) ?? null; | ||
} | ||
async has(key) { | ||
return this.cache.has(this.serialize(key)); | ||
} | ||
serialize(cacheKey) { | ||
return `${cacheKey.locale}/${cacheKey.namespace}`; | ||
} | ||
} |
{ | ||
"name": "remix-i18next", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "The easiest way to translate your Remix apps", | ||
@@ -39,4 +39,5 @@ "license": "MIT", | ||
"i18next": "^21.3.3", | ||
"react": "^17.0.2", | ||
"react-i18next": "^11.13.0", | ||
"react": "^17.0.2", | ||
"react-router-dom": "^6.0.0-beta.6", | ||
"remix": "^0.19.2" | ||
@@ -43,0 +44,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23507
16
362
9
2
1