Skip to content

Commit 5b76bb0

Browse files
committed
feat: Configurable filter names
1 parent c29f424 commit 5b76bb0

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ const output = template.render();
6868
// <h1>Hello world</h1>
6969
```
7070

71+
## Custom filter names
72+
73+
By default, the filter names `t` and `trans` are supported. You can overwrite or extend these names with the `filterNames` option:
74+
75+
```js
76+
import Twig from "twig";
77+
import { twigDrupalString } from "twig-drupal-string";
78+
79+
twigDrupalString({
80+
Twig,
81+
files: ["strings.yaml"],
82+
filterNames: ["t", "trans", "tc"],
83+
});
84+
```
85+
7186
## Watch mode
7287

7388
For development purposes, a watch mode can be enabled that reloads the translation strings from disk if any of the referenced files change.
@@ -86,11 +101,12 @@ twigDrupalString({
86101

87102
The `twigDrupalString` method receives an options object with the following properties:
88103

89-
| Property | Type | Description |
90-
| -------- | ---------- | --------------------------------------------- |
91-
| `Twig` | `Twig` | Twig.js engine instance |
92-
| `files` | `string[]` | Array of paths to translation string files |
93-
| `watch` | `boolean` | Enable or disable watch mode, default `false` |
104+
| Property | Type | Description |
105+
| ------------- | ---------- | ------------------------------------------------------ |
106+
| `Twig` | `Twig` | Twig.js engine instance |
107+
| `files` | `string[]` | Array of paths to translation string files |
108+
| `filterNames` | `string[]` | Array of filter name strings, default `["t", "trans"]` |
109+
| `watch` | `boolean` | Enable or disable watch mode, default `false` |
94110

95111
## Contributing
96112

lib/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import YAML from "yaml";
55
/**
66
* @param {import("./types.d.ts").Options} options
77
*/
8-
export function twigDrupalString({ Twig, files, watch = false }) {
8+
export function twigDrupalString({
9+
Twig,
10+
files,
11+
filterNames = ["t", "trans"],
12+
watch = false,
13+
}) {
914
let translations = getTranslations(files);
1015

1116
/** @type {Set<string>} */
@@ -69,8 +74,9 @@ export function twigDrupalString({ Twig, files, watch = false }) {
6974
return string;
7075
};
7176

72-
Twig.extendFilter("t", trans);
73-
Twig.extendFilter("trans", trans);
77+
for (const filterName of filterNames) {
78+
Twig.extendFilter(filterName, trans);
79+
}
7480
}
7581

7682
/**

lib/types.d.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import Twig from "twig";
22

33
type Options = {
4-
/** Twig engine instance */
4+
/**
5+
* Twig engine instance
6+
*/
57
Twig: typeof Twig;
68

7-
/** Paths to translation string files */
9+
/**
10+
* Paths to translation string files
11+
*/
812
files: string[];
913

10-
/** Enable watch mode (disabled by default) */
14+
/**
15+
* Adjust the filter names
16+
*
17+
* Default: `["t", "trans"]`
18+
*/
19+
filterNames?: string[];
20+
21+
/**
22+
* Enable watch mode
23+
*
24+
* Default: `false`
25+
*/
1126
watch?: boolean;
1227
};
1328

test/filter-name.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,18 @@ suite("filter name", () => {
2828

2929
assert.equal(result, expected);
3030
});
31+
32+
test("custom: tc", () => {
33+
const Twig = createTwigInstance();
34+
twigDrupalString({
35+
Twig,
36+
files: ["./test/strings.yaml"],
37+
filterNames: ["tc"],
38+
});
39+
40+
const data = `<h1>{{ 'welcome'|tc }}</h1>`;
41+
const result = Twig.twig({ data }).render();
42+
43+
assert.equal(result, expected);
44+
});
3145
});

0 commit comments

Comments
 (0)