Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

t-i18n

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

t-i18n - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

CREDITS.md

41

dist/es6/format.d.ts
export declare type IntlFormat = Intl.DateTimeFormat | Intl.NumberFormat;
export declare type IntlFormatType = (typeof Intl.DateTimeFormat | typeof Intl.NumberFormat);
export declare type IntlFormatOptions = Intl.DateTimeFormatOptions | Intl.NumberFormatOptions;
export declare type CachedFormatter = (locale: string, formatOptions?: IntlFormatOptions) => IntlFormat;
export declare let dateTimeFormatOptions: {
[s: string]: Intl.DateTimeFormatOptions;
export declare type IntlFormatType<X extends IntlFormat> = X extends Intl.DateTimeFormat ? typeof Intl.DateTimeFormat : typeof Intl.NumberFormat;
export declare type IntlFormatOptions<X extends IntlFormat> = X extends Intl.DateTimeFormat ? Intl.DateTimeFormatOptions : Intl.NumberFormatOptions;
export declare type CachedFormatter<X extends IntlFormat> = (locale: string, formatOptions?: IntlFormatOptions<X>) => X;
export declare const dateTimeFormats: {
short: {
month: string;
day: string;
year: string;
};
long: {
month: string;
day: string;
year: string;
};
dateTime: {
month: string;
day: string;
year: string;
hour: string;
minute: string;
};
};
export declare let numberFormatOptions: {
[s: string]: Intl.NumberFormatOptions;
export declare const numberFormats: {
currency: {
style: string;
currency: string;
};
decimal: {
style: string;
};
percent: {
style: string;
};
};
export default function createCachedFormatter(intlFormat: (IntlFormatType)): CachedFormatter;
export default function createCachedFormatter<X extends IntlFormat>(intlFormat: IntlFormatType<X>): CachedFormatter<X>;

34

dist/es6/format.js

@@ -1,36 +0,34 @@

export let dateTimeFormatOptions = {
export const dateTimeFormats = {
short: {
month: 'short',
day: 'numeric',
month: "short",
day: "numeric",
year: "numeric"
},
long: {
month: 'long',
day: 'numeric',
year: 'numeric'
month: "long",
day: "numeric",
year: "numeric"
},
dateTime: {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric'
month: "short",
day: "numeric",
year: "numeric",
hour: "numeric",
minute: "numeric"
}
};
dateTimeFormatOptions.default = dateTimeFormatOptions.long;
export let numberFormatOptions = {
export const numberFormats = {
currency: {
style: 'currency',
style: "currency",
currency: "USD"
},
decimal: {
style: 'decimal'
style: "decimal"
},
percent: {
style: 'percent'
style: "percent"
}
};
numberFormatOptions.default = numberFormatOptions.decimal;
export default function createCachedFormatter(intlFormat) {
let cache = {};
const cache = {};
return function (locale, formatOptions) {

@@ -37,0 +35,0 @@ const args = Array.prototype.slice.call(arguments);

@@ -1,30 +0,15 @@

import { IcuReplacements, Messages, MFunc, SetupOptions, AnyReplacements } from "./types";
import { CachedFormatter } from "./format";
import { IcuReplacements, Config, AnyReplacements } from "./types";
import { numberFormats, dateTimeFormats } from "./format";
export interface TFunc {
(message: string, replacements?: IcuReplacements, id?: string): string;
$: <X>(message: string, replacements?: AnyReplacements<X>, id?: string) => (X | string)[];
date: (value: Date | number, formatName?: keyof typeof dateTimeFormats, locale?: string) => string;
generateId: (message: string) => string;
locale: () => string;
lookup: (id: string, replacements?: IcuReplacements, defaultMessage?: string) => string;
setup: (options?: SetupOptions) => any;
date: (value: any, formatName?: string, locale?: string) => string;
number: (value: any, formatName?: string, locale?: string) => string;
$: <X>(message: string, replacements?: AnyReplacements<X>, id?: string) => (X | string)[];
_i18nInstance?: I18n;
number: (value: number, formatName?: keyof typeof numberFormats, locale?: string) => string;
set: (options?: Partial<Config>) => Config;
}
export declare const defaultLanguage = "en";
export declare class I18n {
locale: string;
messages: Messages;
idGenerator: (message: string) => string;
dateFormatter: CachedFormatter;
numberFormatter: CachedFormatter;
static defaultSetup: SetupOptions;
constructor(options?: SetupOptions);
format(type: string, value: number | Date, formatStyle?: string, locale?: string): any;
getKey(key: string, locale?: string): MFunc | String;
generateId(message: string): string;
lookup(id: string, replacements?: IcuReplacements, defaultMessage?: string): string;
setup(options?: SetupOptions): SetupOptions;
}
export declare function i18nNamespace(): TFunc;
export declare const makeT: () => TFunc;
declare const _default: TFunc;
export default _default;
export declare const makeT: typeof i18nNamespace;

@@ -1,37 +0,40 @@

import createCachedFormatter, { numberFormatOptions, dateTimeFormatOptions } from "./format";
import createCachedFormatter, { numberFormats, dateTimeFormats } from "./format";
import { generator, assign, splitAndEscapeReplacements } from "./helpers";
import parseIcu from "./icu";
import parseXml from "./xml";
export const defaultLanguage = "en";
export class I18n {
constructor(options) {
this.locale = "en";
this.messages = {};
this.idGenerator = generator.hyphens;
this.setup(Object.assign({}, I18n.defaultSetup, options));
this.dateFormatter = createCachedFormatter(Intl.DateTimeFormat);
this.numberFormatter = createCachedFormatter(Intl.NumberFormat);
const defaultLanguage = "en";
const getKey = (allMessages, locale, key) => {
const messages = allMessages[locale];
const defaultMessages = allMessages[defaultLanguage];
if (messages && messages[key]) {
return messages[key];
}
format(type, value, formatStyle, locale = this.locale) {
const options = (type === "date") ? dateTimeFormatOptions : numberFormatOptions;
const formatter = (type === "date") ? this.dateFormatter : this.numberFormatter;
const optionsForFormatStyle = formatStyle ? (options[formatStyle] || options.default) : options.default;
return formatter.call(this, locale, optionsForFormatStyle).format(value);
if (defaultMessages && defaultMessages[key]) {
return defaultMessages[key];
}
getKey(key, locale = this.locale) {
const messages = this.messages[locale];
const defaultMessages = this.messages[defaultLanguage];
if (messages && messages[key]) {
return messages[key];
}
if (defaultMessages && defaultMessages[key]) {
return defaultMessages[key];
}
return "";
}
generateId(message) {
return this.idGenerator(message);
}
lookup(id, replacements = {}, defaultMessage = "") {
const translation = this.getKey(id, this.locale) || defaultMessage || id;
return "";
};
export const makeT = () => {
const dateFormatter = createCachedFormatter(Intl.DateTimeFormat);
const numberFormatter = createCachedFormatter(Intl.NumberFormat);
let messages = {};
let locale = defaultLanguage;
let idGenerator = generator.hyphens;
const set = (options = {}) => {
messages = options.messages || messages;
locale = options.locale || locale;
idGenerator = options.idGenerator || idGenerator;
return { messages, locale, idGenerator };
};
const date = (value, style = "long", dateLocale = locale) => {
const format = dateTimeFormats[style] || dateTimeFormats.long;
return dateFormatter(dateLocale, format).format(value);
};
const number = (value, style = "decimal", numberLocale = locale) => {
const format = numberFormats[style] || numberFormats.decimal;
return numberFormatter(numberLocale, format).format(value);
};
const generateId = (message) => idGenerator(message);
const lookup = (id, replacements = {}, defaultMessage = "") => {
const translation = getKey(messages, locale, id) || defaultMessage || id;
if (typeof translation === "string") {

@@ -41,48 +44,22 @@ return parseIcu(translation, replacements);

return translation(replacements);
}
setup(options = {}) {
const { locale, idGenerator, messages } = options;
if (idGenerator)
this.idGenerator = idGenerator;
if (locale)
this.locale = locale;
if (messages)
this.messages = messages;
return {
messages: this.messages,
locale: this.locale,
idGenerator: this.idGenerator
};
}
}
I18n.defaultSetup = {
messages: {},
locale: defaultLanguage,
idGenerator: generator.hyphens
};
function createT(context) {
const T = (message, replacements, id) => {
if (!id)
id = context.generateId(message);
return context.lookup(id, replacements, message);
};
const T = (message, replacements = {}, id = "") => {
return lookup(id || generateId(message), replacements, message);
};
const $ = (message, replacements = {}, id = "") => {
const [icu, xml] = splitAndEscapeReplacements(replacements);
const translatedMessage = T(message, icu, id);
return parseXml(translatedMessage, xml);
};
const properties = {
_i18nInstance: context,
setup: context.setup.bind(context),
lookup: context.lookup.bind(context),
date: context.format.bind(context, "date"),
number: context.format.bind(context, "number"),
$: (message, replacements = {}, id) => {
const [icu, xml] = splitAndEscapeReplacements(replacements);
const translatedMessage = T(message, icu, id);
return parseXml(translatedMessage, xml);
},
$,
date,
generateId,
locale: () => locale,
lookup,
number,
set,
};
return assign(T, properties);
}
export function i18nNamespace() {
let i18nInstance = new I18n();
return createT(i18nInstance);
}
export default i18nNamespace();
export const makeT = i18nNamespace;
};
export default makeT();

@@ -8,7 +8,6 @@ export declare type MFunc = (replacements?: IcuReplacements) => string;

}
export interface SetupOptions {
messages?: Messages;
locale?: string;
idGenerator?: (message: string) => string;
compiler?: Compiler;
export interface Config {
messages: Messages;
locale: string;
idGenerator: (message: string) => string;
}

@@ -15,0 +14,0 @@ export declare type Mutable<X> = {

export declare type IntlFormat = Intl.DateTimeFormat | Intl.NumberFormat;
export declare type IntlFormatType = (typeof Intl.DateTimeFormat | typeof Intl.NumberFormat);
export declare type IntlFormatOptions = Intl.DateTimeFormatOptions | Intl.NumberFormatOptions;
export declare type CachedFormatter = (locale: string, formatOptions?: IntlFormatOptions) => IntlFormat;
export declare let dateTimeFormatOptions: {
[s: string]: Intl.DateTimeFormatOptions;
export declare type IntlFormatType<X extends IntlFormat> = X extends Intl.DateTimeFormat ? typeof Intl.DateTimeFormat : typeof Intl.NumberFormat;
export declare type IntlFormatOptions<X extends IntlFormat> = X extends Intl.DateTimeFormat ? Intl.DateTimeFormatOptions : Intl.NumberFormatOptions;
export declare type CachedFormatter<X extends IntlFormat> = (locale: string, formatOptions?: IntlFormatOptions<X>) => X;
export declare const dateTimeFormats: {
short: {
month: string;
day: string;
year: string;
};
long: {
month: string;
day: string;
year: string;
};
dateTime: {
month: string;
day: string;
year: string;
hour: string;
minute: string;
};
};
export declare let numberFormatOptions: {
[s: string]: Intl.NumberFormatOptions;
export declare const numberFormats: {
currency: {
style: string;
currency: string;
};
decimal: {
style: string;
};
percent: {
style: string;
};
};
export default function createCachedFormatter(intlFormat: (IntlFormatType)): CachedFormatter;
export default function createCachedFormatter<X extends IntlFormat>(intlFormat: IntlFormatType<X>): CachedFormatter<X>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dateTimeFormatOptions = {
exports.dateTimeFormats = {
short: {
month: 'short',
day: 'numeric',
month: "short",
day: "numeric",
year: "numeric"
},
long: {
month: 'long',
day: 'numeric',
year: 'numeric'
month: "long",
day: "numeric",
year: "numeric"
},
dateTime: {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric'
month: "short",
day: "numeric",
year: "numeric",
hour: "numeric",
minute: "numeric"
}
};
exports.dateTimeFormatOptions.default = exports.dateTimeFormatOptions.long;
exports.numberFormatOptions = {
exports.numberFormats = {
currency: {
style: 'currency',
style: "currency",
currency: "USD"
},
decimal: {
style: 'decimal'
style: "decimal"
},
percent: {
style: 'percent'
style: "percent"
}
};
exports.numberFormatOptions.default = exports.numberFormatOptions.decimal;
function createCachedFormatter(intlFormat) {

@@ -37,0 +35,0 @@ var cache = {};

@@ -1,30 +0,15 @@

import { IcuReplacements, Messages, MFunc, SetupOptions, AnyReplacements } from "./types";
import { CachedFormatter } from "./format";
import { IcuReplacements, Config, AnyReplacements } from "./types";
import { numberFormats, dateTimeFormats } from "./format";
export interface TFunc {
(message: string, replacements?: IcuReplacements, id?: string): string;
$: <X>(message: string, replacements?: AnyReplacements<X>, id?: string) => (X | string)[];
date: (value: Date | number, formatName?: keyof typeof dateTimeFormats, locale?: string) => string;
generateId: (message: string) => string;
locale: () => string;
lookup: (id: string, replacements?: IcuReplacements, defaultMessage?: string) => string;
setup: (options?: SetupOptions) => any;
date: (value: any, formatName?: string, locale?: string) => string;
number: (value: any, formatName?: string, locale?: string) => string;
$: <X>(message: string, replacements?: AnyReplacements<X>, id?: string) => (X | string)[];
_i18nInstance?: I18n;
number: (value: number, formatName?: keyof typeof numberFormats, locale?: string) => string;
set: (options?: Partial<Config>) => Config;
}
export declare const defaultLanguage = "en";
export declare class I18n {
locale: string;
messages: Messages;
idGenerator: (message: string) => string;
dateFormatter: CachedFormatter;
numberFormatter: CachedFormatter;
static defaultSetup: SetupOptions;
constructor(options?: SetupOptions);
format(type: string, value: number | Date, formatStyle?: string, locale?: string): any;
getKey(key: string, locale?: string): MFunc | String;
generateId(message: string): string;
lookup(id: string, replacements?: IcuReplacements, defaultMessage?: string): string;
setup(options?: SetupOptions): SetupOptions;
}
export declare function i18nNamespace(): TFunc;
export declare const makeT: () => TFunc;
declare const _default: TFunc;
export default _default;
export declare const makeT: typeof i18nNamespace;
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -15,38 +7,44 @@ var format_1 = require("./format");

var xml_1 = require("./xml");
exports.defaultLanguage = "en";
var I18n = (function () {
function I18n(options) {
this.locale = "en";
this.messages = {};
this.idGenerator = helpers_1.generator.hyphens;
this.setup(__assign({}, I18n.defaultSetup, options));
this.dateFormatter = format_1.default(Intl.DateTimeFormat);
this.numberFormatter = format_1.default(Intl.NumberFormat);
var defaultLanguage = "en";
var getKey = function (allMessages, locale, key) {
var messages = allMessages[locale];
var defaultMessages = allMessages[defaultLanguage];
if (messages && messages[key]) {
return messages[key];
}
I18n.prototype.format = function (type, value, formatStyle, locale) {
if (locale === void 0) { locale = this.locale; }
var options = (type === "date") ? format_1.dateTimeFormatOptions : format_1.numberFormatOptions;
var formatter = (type === "date") ? this.dateFormatter : this.numberFormatter;
var optionsForFormatStyle = formatStyle ? (options[formatStyle] || options.default) : options.default;
return formatter.call(this, locale, optionsForFormatStyle).format(value);
if (defaultMessages && defaultMessages[key]) {
return defaultMessages[key];
}
return "";
};
exports.makeT = function () {
var dateFormatter = format_1.default(Intl.DateTimeFormat);
var numberFormatter = format_1.default(Intl.NumberFormat);
var messages = {};
var locale = defaultLanguage;
var idGenerator = helpers_1.generator.hyphens;
var set = function (options) {
if (options === void 0) { options = {}; }
messages = options.messages || messages;
locale = options.locale || locale;
idGenerator = options.idGenerator || idGenerator;
return { messages: messages, locale: locale, idGenerator: idGenerator };
};
I18n.prototype.getKey = function (key, locale) {
if (locale === void 0) { locale = this.locale; }
var messages = this.messages[locale];
var defaultMessages = this.messages[exports.defaultLanguage];
if (messages && messages[key]) {
return messages[key];
}
if (defaultMessages && defaultMessages[key]) {
return defaultMessages[key];
}
return "";
var date = function (value, style, dateLocale) {
if (style === void 0) { style = "long"; }
if (dateLocale === void 0) { dateLocale = locale; }
var format = format_1.dateTimeFormats[style] || format_1.dateTimeFormats.long;
return dateFormatter(dateLocale, format).format(value);
};
I18n.prototype.generateId = function (message) {
return this.idGenerator(message);
var number = function (value, style, numberLocale) {
if (style === void 0) { style = "decimal"; }
if (numberLocale === void 0) { numberLocale = locale; }
var format = format_1.numberFormats[style] || format_1.numberFormats.decimal;
return numberFormatter(numberLocale, format).format(value);
};
I18n.prototype.lookup = function (id, replacements, defaultMessage) {
var generateId = function (message) { return idGenerator(message); };
var lookup = function (id, replacements, defaultMessage) {
if (replacements === void 0) { replacements = {}; }
if (defaultMessage === void 0) { defaultMessage = ""; }
var translation = this.getKey(id, this.locale) || defaultMessage || id;
var translation = getKey(messages, locale, id) || defaultMessage || id;
if (typeof translation === "string") {

@@ -57,52 +55,25 @@ return icu_1.default(translation, replacements);

};
I18n.prototype.setup = function (options) {
if (options === void 0) { options = {}; }
var locale = options.locale, idGenerator = options.idGenerator, messages = options.messages;
if (idGenerator)
this.idGenerator = idGenerator;
if (locale)
this.locale = locale;
if (messages)
this.messages = messages;
return {
messages: this.messages,
locale: this.locale,
idGenerator: this.idGenerator
};
var T = function (message, replacements, id) {
if (replacements === void 0) { replacements = {}; }
if (id === void 0) { id = ""; }
return lookup(id || generateId(message), replacements, message);
};
I18n.defaultSetup = {
messages: {},
locale: exports.defaultLanguage,
idGenerator: helpers_1.generator.hyphens
var $ = function (message, replacements, id) {
if (replacements === void 0) { replacements = {}; }
if (id === void 0) { id = ""; }
var _a = helpers_1.splitAndEscapeReplacements(replacements), icu = _a[0], xml = _a[1];
var translatedMessage = T(message, icu, id);
return xml_1.default(translatedMessage, xml);
};
return I18n;
}());
exports.I18n = I18n;
function createT(context) {
var T = function (message, replacements, id) {
if (!id)
id = context.generateId(message);
return context.lookup(id, replacements, message);
};
var properties = {
_i18nInstance: context,
setup: context.setup.bind(context),
lookup: context.lookup.bind(context),
date: context.format.bind(context, "date"),
number: context.format.bind(context, "number"),
$: function (message, replacements, id) {
if (replacements === void 0) { replacements = {}; }
var _a = helpers_1.splitAndEscapeReplacements(replacements), icu = _a[0], xml = _a[1];
var translatedMessage = T(message, icu, id);
return xml_1.default(translatedMessage, xml);
},
$: $,
date: date,
generateId: generateId,
locale: function () { return locale; },
lookup: lookup,
number: number,
set: set,
};
return helpers_1.assign(T, properties);
}
function i18nNamespace() {
var i18nInstance = new I18n();
return createT(i18nInstance);
}
exports.i18nNamespace = i18nNamespace;
exports.default = i18nNamespace();
exports.makeT = i18nNamespace;
};
exports.default = exports.makeT();

@@ -8,7 +8,6 @@ export declare type MFunc = (replacements?: IcuReplacements) => string;

}
export interface SetupOptions {
messages?: Messages;
locale?: string;
idGenerator?: (message: string) => string;
compiler?: Compiler;
export interface Config {
messages: Messages;
locale: string;
idGenerator: (message: string) => string;
}

@@ -15,0 +14,0 @@ export declare type Mutable<X> = {

@@ -1,2 +0,2 @@

Copyright (c) 2017 AgileBits Inc.
Copyright (c) 2017-2018 AgileBits Inc.

@@ -3,0 +3,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

{
"name": "t-i18n",
"version": "0.5.0",
"version": "0.6.0",
"description": "Simple, standards-based localization",
"author": "Mitch Cohen <mitch.cohen@me.com>",
"homepage": "https://github.com/agilebits/t-i18n#readme",
"repository": {

@@ -7,0 +8,0 @@ "type": "git",

@@ -32,3 +32,3 @@ # T-i18n

```js
T.setup({
T.set({
locale: "es",

@@ -98,3 +98,3 @@ messages: {

To get locale-aware pluralization, you should [precompile your translations](https://messageformat.github.io/build/) using an ICU-compliant tool. Then pass the compiled messages to `T.setup` instead of strings.
To get locale-aware pluralization, you should [precompile your translations](https://messageformat.github.io/build/) using an ICU-compliant tool. Then pass the compiled messages to `T.set` instead of strings.

@@ -101,0 +101,0 @@ ```js

@@ -67,3 +67,3 @@ #!/usr/bin/env node

const evaluatedMessage = evaluate(message, srcFile);
let idText = id ? id.text : index_1.T._i18nInstance.generateId(evaluatedMessage);
let idText = id ? id.text : index_1.T.generateId(evaluatedMessage);
messages[idText] = evaluatedMessage;

@@ -70,0 +70,0 @@ });

@@ -18,3 +18,3 @@ #!/usr/bin/env node

if (node.text) return node.text;
const expression = (node as ts.Node).getFullText(src);

@@ -39,3 +39,3 @@

if (call.expression.kind && call.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
const methodCall = (call.expression as ts.PropertyAccessExpression),

@@ -46,3 +46,3 @@ obj = (methodCall.expression as ts.Identifier),

if (obj.text === objectName && method.text === methodName) {
res.push(node);
res.push(node);
}

@@ -52,3 +52,3 @@ }

return ts.forEachChild(node, find);
return ts.forEachChild(node, find);
}

@@ -63,3 +63,3 @@ return res;

if (!node) return;
if (node.kind === ts.SyntaxKind.CallExpression) {

@@ -73,4 +73,4 @@ const call = (node as ts.CallExpression),

}
return ts.forEachChild(node, find);
return ts.forEachChild(node, find);
}

@@ -81,3 +81,3 @@ return res;

function extractMessages(contents: string) {
const srcFile = ts.createSourceFile("file.ts", contents, ts.ScriptTarget.ES2017, false, ts.ScriptKind.TSX);
const srcFile = ts.createSourceFile("file.ts", contents, ts.ScriptTarget.ES2017, false, ts.ScriptKind.TSX);
const tCalls = [...findFunctionCall("T", srcFile), ...findMethodCall("T", "$", srcFile)];

@@ -88,3 +88,3 @@ let messages = {}

const evaluatedMessage = evaluate(message, srcFile);
let idText = id ? id.text : T._i18nInstance.generateId(evaluatedMessage);
let idText = id ? id.text : T.generateId(evaluatedMessage);
messages[idText] = evaluatedMessage;

@@ -91,0 +91,0 @@ })

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