Skip to content

Commit a227ec1

Browse files
mvsdejujuzitroni
andcommitted
feat: Initial release
Co-authored-by: Judith Plath <jujuzitroni@gmail.com>
0 parents  commit a227ec1

18 files changed

+3115
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = tab
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.{md,yaml,yml}]
12+
indent_style = space

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
index.cjs

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
message="chore(release): %s"
2+
sign-git-tag=true

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
21

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock.json

CHANGELOG.md

Whitespace-only changes.

CONTRIBUTING.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Contributing
2+
3+
## Getting started
4+
5+
Optionally choose the correct Node.js version with [nvm](https://nvm.sh/) installed:
6+
7+
```sh
8+
nvm use
9+
```
10+
11+
Install dependencies with:
12+
13+
```sh
14+
npm install
15+
```
16+
17+
All commits must follow the [Conventional Commits](https://www.conventionalcommits.org/) specification, so an automatic changelog can be generated.
18+
19+
## Run linter
20+
21+
```sh
22+
npm run lint
23+
```
24+
25+
## Run test
26+
27+
The tests are written with the [Node.js test runner](https://nodejs.org/api/test.html).
28+
29+
```sh
30+
npm run test
31+
```
32+
33+
## Format code
34+
35+
```sh
36+
npm run format
37+
```
38+
39+
## Create release
40+
41+
Bump the package version and generate a changelog:
42+
43+
```sh
44+
npm version …
45+
```
46+
47+
See [npm version docs](https://docs.npmjs.com/cli/v10/commands/npm-version) for all available arguments.
48+
49+
Publish the new version with:
50+
51+
```sh
52+
npm publish
53+
```

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT license
2+
3+
Copyright © 2024 Fynn Becker
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Twig Drupal String
2+
3+
Add support for the Drupal [Twig `t` and `trans` filters](https://symfony.com/doc/current/translation.html#translation-filters) in combination with the [String module](https://www.drupal.org/project/string) and [Twig.js](https://github.com/twigjs/twig.js/).
4+
5+
## Installation
6+
7+
Add the package to your dependencies:
8+
9+
```sh
10+
npm install --save-dev twig twig-drupal-string
11+
```
12+
13+
## Example
14+
15+
Crate a file called `strings.yaml` with the following content:
16+
17+
```yaml
18+
welcome:
19+
default: Hello world
20+
```
21+
22+
Then create `render-template.mjs`:
23+
24+
```js
25+
import Twig from "twig";
26+
import { twigDrupalString } from "twig-drupal-string";
27+
28+
twigDrupalString({
29+
twig: Twig,
30+
files: ["strings.yaml"],
31+
});
32+
33+
const data = `<h1>{{ 'welcome'|t }}</h1>`;
34+
const template = Twig.twig({ data });
35+
const output = template.render();
36+
37+
console.log(output);
38+
```
39+
40+
Run the example with:
41+
42+
```sh
43+
node render-template.mjs
44+
45+
# The output is:
46+
# <h1>Welcome</h1>
47+
```
48+
49+
## Placeholders
50+
51+
The filter also supports placeholders inside the strings that will be replaced with dynamic data during template rendering.
52+
53+
Add the following to `strings.yaml`:
54+
55+
```yaml
56+
greeting:
57+
default: Hello @name
58+
```
59+
60+
Then adjust the template inside `render-template.mjs`:
61+
62+
```js
63+
const data = `<h1>{{ 'greeting'|t({'@name': 'world'}) }}</h1>`;
64+
const template = Twig.twig({ data });
65+
const output = template.render();
66+
67+
// Output will be:
68+
// <h1>Hello world</h1>
69+
```
70+
71+
## Watch mode
72+
73+
For development purposes, a watch mode can be enabled that reloads the translation strings from disk if any of the referenced files change.
74+
75+
Set the `watch` options:
76+
77+
```js
78+
twigDrupalString({
79+
twig: Twig,
80+
files: ["strings.yaml"],
81+
watch: true,
82+
});
83+
```
84+
85+
## Contributing
86+
87+
See [contributing documentation](CONTRIBUTING.md) for more information.
88+
89+
## Sponsored by
90+
91+
<a href="https://factorial.io/">
92+
<img src="https://logo.factorial.io/color.svg" width="40" height="56" alt="Factorial">
93+
</a>

eslint.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import js from "@eslint/js";
2+
import simpleImportSort from "eslint-plugin-simple-import-sort";
3+
import globals from "globals";
4+
5+
export default [
6+
js.configs.recommended,
7+
{
8+
languageOptions: {
9+
globals: {
10+
...globals.node,
11+
},
12+
},
13+
plugins: {
14+
"simple-import-sort": simpleImportSort,
15+
},
16+
rules: {
17+
"simple-import-sort/imports": "error",
18+
"simple-import-sort/exports": "error",
19+
},
20+
},
21+
];

0 commit comments

Comments
 (0)