New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@itwin/core-i18n

Package Overview
Dependencies
Maintainers
2
Versions
996
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@itwin/core-i18n - npm Package Compare versions

Comparing version 3.0.0-dev.83 to 3.0.0-dev.84

30

lib/cjs/Localization.d.ts
/** @packageDocumentation
* @module Localization
*/
import { TranslationOptions } from "i18next";
import { Localization } from "@itwin/core-common";
import { Callback, TranslationOptions } from "i18next";
/** @public */
interface LocalizationInitOptions {
/** Options for ITwinLocalization
* @public
*/
export interface LocalizationOptions {
urlTemplate: string;
}
/** Supplies Internationalization services.
/** Supplies localizations for iTwin.js
* @note Internally, this class uses the [i18next](https://www.i18next.com/) package.
* @public
*/
export declare class I18N implements Localization {
export declare class ITwinLocalization implements Localization {
private _i18next;
private readonly _namespaceRegistry;
/** Constructor for I18N.
* @param nameSpaces either the name of the default namespace, an array of namespaces, or undefined. If an array, the first entry is the default.
/**
* @param options object with I18NOptions (optional)
* @param renderFunction optional i18next.Callback function
*/
constructor(nameSpaces?: string | string[], options?: LocalizationInitOptions, renderFunction?: Callback);
constructor(options?: LocalizationOptions);
/** Replace all instances of `%{key}` within a string with the translations of those keys.

@@ -72,9 +72,12 @@ * For example:

loadNamespace(name: string, i18nCallback: any): void;
/** Get an already registered Namespace.
/** Get the promise for an already registered Namespace.
* @param name - the name of the namespace
* @public
*/
getNamespace(name: string): Promise<void> | undefined;
getNamespacePromise(name: string): Promise<void> | undefined;
/** @internal */
languageList(): string[];
getLanguageList(): string[];
/** override the language detected in the browser
* @internal */
changeLanguage(language: string): Promise<void>;
/** Register a new Namespace and return it. If the namespace is already registered, it will be returned.

@@ -88,7 +91,6 @@ * @param name - the name of the namespace, which is the base name of the JSON file that contains the localization properties.

*/
registerNamespace(name: string): Promise<void>;
registerNamespace(name: string, setDefault?: true): Promise<void>;
/** @internal */
unregisterNamespace(name: string): void;
}
export {};
//# sourceMappingURL=Localization.d.ts.map

@@ -10,25 +10,24 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.I18N = void 0;
exports.ITwinLocalization = void 0;
const i18next_1 = require("i18next");
const i18nextBrowserLanguageDetector = require("i18next-browser-languagedetector");
const i18next_xhr_backend_1 = require("i18next-xhr-backend");
const HttpApi = require("i18next-http-backend");
const core_bentley_1 = require("@itwin/core-bentley");
/** Supplies Internationalization services.
/** Supplies localizations for iTwin.js
* @note Internally, this class uses the [i18next](https://www.i18next.com/) package.
* @public
*/
class I18N {
/** Constructor for I18N.
* @param nameSpaces either the name of the default namespace, an array of namespaces, or undefined. If an array, the first entry is the default.
class ITwinLocalization {
/**
* @param options object with I18NOptions (optional)
* @param renderFunction optional i18next.Callback function
*/
constructor(nameSpaces, options, renderFunction) {
constructor(options) {
var _a, _b;
this._namespaceRegistry = new Map();
this._i18next = (0, i18next_1.createInstance)();
const backendOptions = {
loadPath: options && options.urlTemplate ? options.urlTemplate : "locales/{{lng}}/{{ns}}.json",
const backend = {
loadPath: (_a = options === null || options === void 0 ? void 0 : options.urlTemplate) !== null && _a !== void 0 ? _a : "locales/{{lng}}/{{ns}}.json",
crossDomain: true,
};
const detectionOptions = {
const detection = {
order: ["querystring", "navigator", "htmlTag"],

@@ -38,33 +37,17 @@ lookupQuerystring: "lng",

};
nameSpaces = nameSpaces ? ("string" === typeof nameSpaces ? [nameSpaces] : nameSpaces) : [""];
const initOptions = {
interpolation: { escapeValue: true },
ns: [],
defaultNS: "",
fallbackLng: "en",
ns: nameSpaces,
defaultNS: nameSpaces[0],
backend: backendOptions,
detection: detectionOptions,
backend,
detection,
};
// if in a development environment, set to pseudo-localize, otherwise detect from browser.
const isDevelopment = process.env.NODE_ENV === "development";
if (isDevelopment) {
// if in a development environment, set debugging
if (process.env.NODE_ENV === "development")
initOptions.debug = true;
}
else {
this._i18next = this._i18next.use(i18nextBrowserLanguageDetector);
}
const initPromise = new Promise((resolve) => {
this._i18next.use(i18next_xhr_backend_1.default)
.use(BentleyLogger)
.init(initOptions, (error, t) => {
if (renderFunction !== undefined)
renderFunction(error, t);
resolve();
})
.changeLanguage(isDevelopment ? "en" : undefined, undefined);
// call the changeLanguage method right away, before any calls to I18NNamespace.register. Otherwise, the call doesn't happen until the deferred load of the default namespace
});
for (const nameSpace of nameSpaces) {
this._namespaceRegistry.set(nameSpace, initPromise);
}
this._i18next.use(i18nextBrowserLanguageDetector)
.use((_b = HttpApi.default) !== null && _b !== void 0 ? _b : HttpApi)
.use(TranslationLogger)
.init(initOptions);
}

@@ -87,3 +70,5 @@ /** Replace all instances of `%{key}` within a string with the translations of those keys.

*/
getLocalizedKeys(line) { return line.replace(/\%\{(.+?)\}/g, (_match, tag) => this.getLocalizedString(tag)); }
getLocalizedKeys(line) {
return line.replace(/\%\{(.+?)\}/g, (_match, tag) => this.getLocalizedString(tag));
}
/** Return the translated value of a key.

@@ -142,12 +127,21 @@ * @param key - the key that matches a property in the JSON localization file.

/** @internal */
loadNamespace(name, i18nCallback) { this._i18next.loadNamespaces(name, i18nCallback); }
/** Get an already registered Namespace.
loadNamespace(name, i18nCallback) {
this._i18next.loadNamespaces(name, i18nCallback);
}
/** Get the promise for an already registered Namespace.
* @param name - the name of the namespace
* @public
*/
getNamespace(name) {
getNamespacePromise(name) {
return this._namespaceRegistry.get(name);
}
/** @internal */
languageList() { return this._i18next.languages; }
getLanguageList() {
return this._i18next.languages;
}
/** override the language detected in the browser
* @internal */
async changeLanguage(language) {
this._i18next.changeLanguage(language);
}
/** Register a new Namespace and return it. If the namespace is already registered, it will be returned.

@@ -161,6 +155,8 @@ * @param name - the name of the namespace, which is the base name of the JSON file that contains the localization properties.

*/
async registerNamespace(name) {
async registerNamespace(name, setDefault) {
const existing = this._namespaceRegistry.get(name);
if (existing !== undefined)
return existing;
if (setDefault)
this._i18next.setDefaultNamespace(name);
const theReadPromise = new Promise((resolve, _reject) => {

@@ -175,6 +171,6 @@ this.loadNamespace(name, (err, _t) => {

// possible locale. For example 'fr-ca' might be the most specific local, in which case 'fr' ) and 'en are fallback locales.
// using i18next-xhr-backend, err will be an array of strings that includes the namespace it tried to read and the locale. There
// using i18next-http-backend, err will be an array of strings that includes the namespace it tried to read and the locale. There
// might be errs for some other namespaces as well as this one. We resolve the promise unless there's an error for each possible language.
const errorList = err;
let locales = this.languageList().map((thisLocale) => `/${thisLocale}/`);
let locales = this.getLanguageList().map((thisLocale) => `/${thisLocale}/`);
for (const thisError of errorList) {

@@ -199,8 +195,4 @@ if (!thisError.includes(name))

}
exports.I18N = I18N;
/** The class that represents a registered Localization Namespace
* @note The readFinished member is a Promise that is resolved when the JSON file for the namespace has been retrieved from the server, or rejected if an error occurs.
* @public
*/
class BentleyLogger {
exports.ITwinLocalization = ITwinLocalization;
class TranslationLogger {
log(args) { core_bentley_1.Logger.logInfo("i18n", this.createLogMessage(args)); }

@@ -214,3 +206,3 @@ warn(args) { core_bentley_1.Logger.logWarning("i18n", this.createLogMessage(args)); }

for (let j = 0; j < i; ++j)
message += " ";
message += " ";
message += args[i];

@@ -221,3 +213,3 @@ }

}
BentleyLogger.type = "logger";
TranslationLogger.type = "logger";
//# sourceMappingURL=Localization.js.map
/** @packageDocumentation
* @module Localization
*/
import { TranslationOptions } from "i18next";
import { Localization } from "@itwin/core-common";
import { Callback, TranslationOptions } from "i18next";
/** @public */
interface LocalizationInitOptions {
/** Options for ITwinLocalization
* @public
*/
export interface LocalizationOptions {
urlTemplate: string;
}
/** Supplies Internationalization services.
/** Supplies localizations for iTwin.js
* @note Internally, this class uses the [i18next](https://www.i18next.com/) package.
* @public
*/
export declare class I18N implements Localization {
export declare class ITwinLocalization implements Localization {
private _i18next;
private readonly _namespaceRegistry;
/** Constructor for I18N.
* @param nameSpaces either the name of the default namespace, an array of namespaces, or undefined. If an array, the first entry is the default.
/**
* @param options object with I18NOptions (optional)
* @param renderFunction optional i18next.Callback function
*/
constructor(nameSpaces?: string | string[], options?: LocalizationInitOptions, renderFunction?: Callback);
constructor(options?: LocalizationOptions);
/** Replace all instances of `%{key}` within a string with the translations of those keys.

@@ -72,9 +72,12 @@ * For example:

loadNamespace(name: string, i18nCallback: any): void;
/** Get an already registered Namespace.
/** Get the promise for an already registered Namespace.
* @param name - the name of the namespace
* @public
*/
getNamespace(name: string): Promise<void> | undefined;
getNamespacePromise(name: string): Promise<void> | undefined;
/** @internal */
languageList(): string[];
getLanguageList(): string[];
/** override the language detected in the browser
* @internal */
changeLanguage(language: string): Promise<void>;
/** Register a new Namespace and return it. If the namespace is already registered, it will be returned.

@@ -88,7 +91,6 @@ * @param name - the name of the namespace, which is the base name of the JSON file that contains the localization properties.

*/
registerNamespace(name: string): Promise<void>;
registerNamespace(name: string, setDefault?: true): Promise<void>;
/** @internal */
unregisterNamespace(name: string): void;
}
export {};
//# sourceMappingURL=Localization.d.ts.map

@@ -10,22 +10,21 @@ /*---------------------------------------------------------------------------------------------

import * as i18nextBrowserLanguageDetector from "i18next-browser-languagedetector";
import XHR from "i18next-xhr-backend";
import * as HttpApi from "i18next-http-backend";
import { Logger } from "@itwin/core-bentley";
/** Supplies Internationalization services.
/** Supplies localizations for iTwin.js
* @note Internally, this class uses the [i18next](https://www.i18next.com/) package.
* @public
*/
export class I18N {
/** Constructor for I18N.
* @param nameSpaces either the name of the default namespace, an array of namespaces, or undefined. If an array, the first entry is the default.
export class ITwinLocalization {
/**
* @param options object with I18NOptions (optional)
* @param renderFunction optional i18next.Callback function
*/
constructor(nameSpaces, options, renderFunction) {
constructor(options) {
var _a, _b;
this._namespaceRegistry = new Map();
this._i18next = createInstance();
const backendOptions = {
loadPath: options && options.urlTemplate ? options.urlTemplate : "locales/{{lng}}/{{ns}}.json",
const backend = {
loadPath: (_a = options === null || options === void 0 ? void 0 : options.urlTemplate) !== null && _a !== void 0 ? _a : "locales/{{lng}}/{{ns}}.json",
crossDomain: true,
};
const detectionOptions = {
const detection = {
order: ["querystring", "navigator", "htmlTag"],

@@ -35,33 +34,17 @@ lookupQuerystring: "lng",

};
nameSpaces = nameSpaces ? ("string" === typeof nameSpaces ? [nameSpaces] : nameSpaces) : [""];
const initOptions = {
interpolation: { escapeValue: true },
ns: [],
defaultNS: "",
fallbackLng: "en",
ns: nameSpaces,
defaultNS: nameSpaces[0],
backend: backendOptions,
detection: detectionOptions,
backend,
detection,
};
// if in a development environment, set to pseudo-localize, otherwise detect from browser.
const isDevelopment = process.env.NODE_ENV === "development";
if (isDevelopment) {
// if in a development environment, set debugging
if (process.env.NODE_ENV === "development")
initOptions.debug = true;
}
else {
this._i18next = this._i18next.use(i18nextBrowserLanguageDetector);
}
const initPromise = new Promise((resolve) => {
this._i18next.use(XHR)
.use(BentleyLogger)
.init(initOptions, (error, t) => {
if (renderFunction !== undefined)
renderFunction(error, t);
resolve();
})
.changeLanguage(isDevelopment ? "en" : undefined, undefined);
// call the changeLanguage method right away, before any calls to I18NNamespace.register. Otherwise, the call doesn't happen until the deferred load of the default namespace
});
for (const nameSpace of nameSpaces) {
this._namespaceRegistry.set(nameSpace, initPromise);
}
this._i18next.use(i18nextBrowserLanguageDetector)
.use((_b = HttpApi.default) !== null && _b !== void 0 ? _b : HttpApi)
.use(TranslationLogger)
.init(initOptions);
}

@@ -84,3 +67,5 @@ /** Replace all instances of `%{key}` within a string with the translations of those keys.

*/
getLocalizedKeys(line) { return line.replace(/\%\{(.+?)\}/g, (_match, tag) => this.getLocalizedString(tag)); }
getLocalizedKeys(line) {
return line.replace(/\%\{(.+?)\}/g, (_match, tag) => this.getLocalizedString(tag));
}
/** Return the translated value of a key.

@@ -139,12 +124,21 @@ * @param key - the key that matches a property in the JSON localization file.

/** @internal */
loadNamespace(name, i18nCallback) { this._i18next.loadNamespaces(name, i18nCallback); }
/** Get an already registered Namespace.
loadNamespace(name, i18nCallback) {
this._i18next.loadNamespaces(name, i18nCallback);
}
/** Get the promise for an already registered Namespace.
* @param name - the name of the namespace
* @public
*/
getNamespace(name) {
getNamespacePromise(name) {
return this._namespaceRegistry.get(name);
}
/** @internal */
languageList() { return this._i18next.languages; }
getLanguageList() {
return this._i18next.languages;
}
/** override the language detected in the browser
* @internal */
async changeLanguage(language) {
this._i18next.changeLanguage(language);
}
/** Register a new Namespace and return it. If the namespace is already registered, it will be returned.

@@ -158,6 +152,8 @@ * @param name - the name of the namespace, which is the base name of the JSON file that contains the localization properties.

*/
async registerNamespace(name) {
async registerNamespace(name, setDefault) {
const existing = this._namespaceRegistry.get(name);
if (existing !== undefined)
return existing;
if (setDefault)
this._i18next.setDefaultNamespace(name);
const theReadPromise = new Promise((resolve, _reject) => {

@@ -172,6 +168,6 @@ this.loadNamespace(name, (err, _t) => {

// possible locale. For example 'fr-ca' might be the most specific local, in which case 'fr' ) and 'en are fallback locales.
// using i18next-xhr-backend, err will be an array of strings that includes the namespace it tried to read and the locale. There
// using i18next-http-backend, err will be an array of strings that includes the namespace it tried to read and the locale. There
// might be errs for some other namespaces as well as this one. We resolve the promise unless there's an error for each possible language.
const errorList = err;
let locales = this.languageList().map((thisLocale) => `/${thisLocale}/`);
let locales = this.getLanguageList().map((thisLocale) => `/${thisLocale}/`);
for (const thisError of errorList) {

@@ -196,7 +192,3 @@ if (!thisError.includes(name))

}
/** The class that represents a registered Localization Namespace
* @note The readFinished member is a Promise that is resolved when the JSON file for the namespace has been retrieved from the server, or rejected if an error occurs.
* @public
*/
class BentleyLogger {
class TranslationLogger {
log(args) { Logger.logInfo("i18n", this.createLogMessage(args)); }

@@ -210,3 +202,3 @@ warn(args) { Logger.logWarning("i18n", this.createLogMessage(args)); }

for (let j = 0; j < i; ++j)
message += " ";
message += " ";
message += args[i];

@@ -217,3 +209,3 @@ }

}
BentleyLogger.type = "logger";
TranslationLogger.type = "logger";
//# sourceMappingURL=Localization.js.map
{
"name": "@itwin/core-i18n",
"version": "3.0.0-dev.83",
"version": "3.0.0-dev.84",
"description": "iModel.js localization code",

@@ -24,3 +24,3 @@ "main": "lib/cjs/core-i18n.js",

"peerDependencies": {
"@itwin/core-bentley": "^3.0.0-dev.83"
"@itwin/core-bentley": "^3.0.0-dev.84"
},

@@ -32,9 +32,9 @@ "//devDependencies": [

"devDependencies": {
"@itwin/core-bentley": "3.0.0-dev.83",
"@itwin/core-common": "3.0.0-dev.83",
"@itwin/build-tools": "3.0.0-dev.83",
"@itwin/eslint-plugin": "3.0.0-dev.83",
"@itwin/build-tools": "3.0.0-dev.84",
"@itwin/core-bentley": "3.0.0-dev.84",
"@itwin/core-common": "3.0.0-dev.84",
"@itwin/eslint-plugin": "3.0.0-dev.84",
"@types/node": "14.14.31",
"@types/i18next": "^8.4.2",
"@types/i18next-browser-languagedetector": "^2.0.1",
"@types/node": "14.14.31",
"eslint": "^7.11.0",

@@ -52,3 +52,3 @@ "rimraf": "^3.0.2",

"i18next-browser-languagedetector": "^2.1.0",
"i18next-xhr-backend": "^2.0.1"
"i18next-http-backend": "^1.3.1"
},

@@ -55,0 +55,0 @@ "eslintConfig": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc