Skip to content

Commit 0ef7242

Browse files
committedJun 29, 2020
feat: add table() method
1 parent 3135013 commit 0ef7242

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed
 

‎readme.md

+14
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ ctx.t('bar', { value: 123 }, 'en');
155155
//=> 'template sees "123"
156156
```
157157

158+
### rosetta.table(lang)
159+
Returns: `Object` or `undefined`
160+
161+
Retrieve the the `lang`'s full dictionary/table of translation keys.
162+
163+
If the language does not exist (aka, no translations have been provided for it), you'll receive `undefined`.<br>Otherwise, you'll receive the full object as it exists within the `Rosetta` instance. See [`table`](#table).
164+
165+
> **Important:** Manipulating this object is any way will mutate and affect your `Rosetta` instance. Be careful!
166+
167+
#### lang
168+
Type: `String`
169+
170+
The language code's table to retrieve.
171+
158172

159173
### rosetta.t(key, params?, lang?)
160174
Returns: `String`

‎rosetta.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ declare module 'rosetta' {
44
locale(lang?: string): string;
55
/** Define or extend the language table */
66
set(lang: string, table: T): void;
7+
/** Get the table of translations for a language */
8+
table(lang: string): T | void;
79
/** Retrieve a translation segment for the current language */
810
t<X extends Record<string, any> | any[]>(key: string | (string | number)[], params?: X, lang?: string): string;
911
}
@@ -18,6 +20,8 @@ declare module 'rosetta/debug' {
1820
locale(lang?: string): string;
1921
/** Define or extend the language table */
2022
set(lang: string, table: T): void;
23+
/** Get the table of translations for a language */
24+
table(lang: string): T | void;
2125
/** Retrieve a translation segment for the current language */
2226
t<X extends Record<string, any> | any[]>(key: string | (string | number)[], params?: X, lang?: string): string;
2327
}

‎src/debug.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default function (obj) {
1313
return (locale = lang || locale);
1414
},
1515

16+
table(lang) {
17+
return tree[lang];
18+
},
19+
1620
t(key, params, lang) {
1721
var val = dlv(tree[lang || locale], key);
1822
if (val == null) {

‎src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export default function (obj) {
1313
return (locale = lang || locale);
1414
},
1515

16+
table(lang) {
17+
return tree[lang];
18+
},
19+
1620
t(key, params, lang) {
1721
var val = dlv(tree[lang || locale], key, '');
1822
if (typeof val === 'function') return val(params);

‎test/debug.js

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ test('(debug) usage', () => {
2626
pt: { foo: 'foo {{name}}~!' },
2727
});
2828

29+
assert.equal(
30+
ctx.table('en'),
31+
{ hello: 'Hello, {{name}}!' }
32+
);
33+
34+
assert.is(
35+
ctx.table('foobar'),
36+
undefined
37+
);
38+
2939
let foo = ctx.t('hello');
3040
assert.is(foo, undefined, '~> undefined w/o locale');
3141
assert.is(_message, `[rosetta] Missing the "hello" key within the "" dictionary`, '~> prints error message');

‎test/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ test('usage', () => {
2020
pt: { foo: 'foo {{name}}~!' },
2121
});
2222

23+
assert.equal(
24+
ctx.table('en'),
25+
{ hello: 'Hello, {{name}}!' }
26+
);
27+
28+
assert.is(
29+
ctx.table('foobar'),
30+
undefined
31+
);
32+
2333
let foo = ctx.t('hello');
2434
assert.is(foo, "", '~> "" w/o locale');
2535

0 commit comments

Comments
 (0)
Please sign in to comment.