@nitro/app 9.4.2 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -31,12 +31,22 @@ Object.keys(files).forEach((key) => {
|
|
|
31
31
|
const helperTagFactory = require(files[key]);
|
|
32
32
|
|
|
33
33
|
// expose helper as custom tag
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
Twig.extend((TwigInstance) => {
|
|
35
|
+
const result = helperTagFactory(TwigInstance);
|
|
36
|
+
|
|
37
|
+
// if the helper returned a tag (i.e., a config object), register it
|
|
38
|
+
if (result && result.type && result.parse) {
|
|
39
|
+
TwigInstance.exports.extendTag(result);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// if it didn’t return anything, it’s probably a filter or global setup
|
|
43
|
+
// Nothing else to do here
|
|
37
44
|
});
|
|
38
45
|
});
|
|
39
46
|
|
|
47
|
+
// register t filter
|
|
48
|
+
require('./filters/t')(Twig);
|
|
49
|
+
|
|
40
50
|
// eslint-disable-next-line
|
|
41
51
|
Twig.renderWithLayout = (viewPath, options, fn) => {
|
|
42
52
|
const layoutPath = path.join(options.settings.views, `${options.layout}.${options.settings['view engine']}`);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Simple Twig Translation Filter
|
|
5
|
+
*
|
|
6
|
+
* @dependency: https://www.npmjs.com/package/i18next
|
|
7
|
+
* http://i18next.com/
|
|
8
|
+
*
|
|
9
|
+
* @examples
|
|
10
|
+
* default translation file in project/locales/default/translation.json
|
|
11
|
+
*
|
|
12
|
+
* {{ 'test.example.string'|t }}
|
|
13
|
+
* {{ 'test.example.interpolation'|t({ name: 'developer' }) }}
|
|
14
|
+
*
|
|
15
|
+
* It should be also possible to use other translation features from i18next (http://i18next.com/translate/)
|
|
16
|
+
*/
|
|
17
|
+
const i18next = require('i18next');
|
|
18
|
+
|
|
19
|
+
module.exports = function registerTranslationFilter (Twig) {
|
|
20
|
+
// Register the `t` filter
|
|
21
|
+
Twig.extendFilter('t', (value, args) => {
|
|
22
|
+
try {
|
|
23
|
+
// Twig passes filter args as an array: args[0] is your options object
|
|
24
|
+
const options = args && args[0] ? args[0] : {};
|
|
25
|
+
return i18next.t(value, options);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error('Translation error:', e);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|