Skip to content

Commit c38787d

Browse files
committed
Create error versions of T
In B5X, we had an issue where there were two options for Locale providers and one file used the wrong one, leading to a perplexing issue with partial translations. If the T function that initialized the empty context had errored instead of falling back to something halfway useful, the problem would have been a lot clearer. Here I provide functions to make that kind of setup easy so that consumers don't need to maintain them.
1 parent 8a9270a commit c38787d

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "t-i18n",
3-
"version": "0.6.5",
3+
"version": "0.6.6",
44
"description": "Simple, standards-based localization",
55
"author": "Mitch Cohen <[email protected]>",
66
"homepage": "https://github.com/agilebits/t-i18n#readme",

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export { Plural, generator } from "./helpers";
22

3-
export { default as T, makeBasicT, makeT } from "./t-i18n";
3+
export {
4+
default as T,
5+
makeBasicT,
6+
makeT,
7+
makeErrorBasicT,
8+
makeErrorT,
9+
} from "./t-i18n";

src/t-i18n.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,47 @@ export const makeT = (): TFunc => {
203203
return assign(T, formatters);
204204
};
205205

206+
/**
207+
* Create a version of `T` that only errors.
208+
*
209+
* This is useful if you need to enforce a certain way of loading strings,
210+
* such as with React's Context Providers.
211+
*/
212+
export const makeErrorBasicT = (message: string): BasicTFunc => {
213+
const errorFn = () => {
214+
throw new Error(message);
215+
};
216+
217+
return assign(errorFn, {
218+
$: errorFn,
219+
generateId: errorFn,
220+
locale: errorFn,
221+
lookup: errorFn,
222+
set: errorFn,
223+
});
224+
};
225+
226+
/**
227+
* Create a version of `T` that only errors.
228+
*
229+
* This is useful if you need to enforce a certain way of loading strings,
230+
* such as with React's Context Providers.
231+
*/
232+
export const makeErrorT = (message: string): TFunc => {
233+
const errorFn = () => {
234+
throw new Error(message);
235+
};
236+
237+
return assign(errorFn, {
238+
$: errorFn,
239+
generateId: errorFn,
240+
locale: errorFn,
241+
lookup: errorFn,
242+
set: errorFn,
243+
date: errorFn,
244+
number: errorFn,
245+
});
246+
};
247+
206248
// singleton (T)
207249
export default makeT();

test/t-i18n.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
/// <reference path="../node_modules/@types/mocha/index.d.ts" />
22

33
import { expect } from "chai";
4-
import { makeBasicT, makeT, Plural, generator, T as globalT } from "../src";
4+
import {
5+
makeBasicT,
6+
makeErrorBasicT,
7+
makeErrorT,
8+
makeT,
9+
Plural,
10+
generator,
11+
T as globalT,
12+
} from "../src";
513
import { dateTimeFormats, numberFormats } from "../src/format";
614
import { BasicTFunc, TFunc } from "../src/t-i18n";
715

@@ -325,3 +333,33 @@ describe("T.number", () => {
325333
expect(result).to.equal(expected);
326334
});
327335
});
336+
337+
describe("makeErrorBasicT", () => {
338+
it("should produce a T that errors on every function call", () => {
339+
const message = "LocaleProvider is not mounted";
340+
const T = makeErrorBasicT(message);
341+
342+
expect(() => T("test")).to.throw(message);
343+
expect(() => T.$("test")).to.throw(message);
344+
expect(() => T.generateId("test")).to.throw(message);
345+
expect(() => T.locale()).to.throw(message);
346+
expect(() => T.lookup("abc")).to.throw(message);
347+
expect(() => T.set({ locale: "en" })).to.throw(message);
348+
});
349+
});
350+
351+
describe("makeErrorT", () => {
352+
it("should produce a T that errors on every function call", () => {
353+
const message = "LocaleProvider is not mounted";
354+
const T = makeErrorT(message);
355+
356+
expect(() => T("test")).to.throw(message);
357+
expect(() => T.$("test")).to.throw(message);
358+
expect(() => T.generateId("test")).to.throw(message);
359+
expect(() => T.locale()).to.throw(message);
360+
expect(() => T.lookup("abc")).to.throw(message);
361+
expect(() => T.set({ locale: "en" })).to.throw(message);
362+
expect(() => T.date(new Date())).to.throw(message);
363+
expect(() => T.number(12345)).to.throw(message);
364+
});
365+
});

0 commit comments

Comments
 (0)