@pcdevil/eleventy-plugin-intl-utils 0.2.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.
- package/.eleventy.js +15 -0
- package/.eslintrc.json +36 -0
- package/.nvmrc +1 -0
- package/LICENSE.md +20 -0
- package/README.md +101 -0
- package/package.json +44 -0
- package/src/country_name.js +9 -0
- package/src/language_name.js +9 -0
- package/src/short_datetime_format.js +12 -0
- package/src/year_interval.js +11 -0
package/.eleventy.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const countryNameFactory = require('./src/country_name');
|
|
4
|
+
const languageNameFactory = require('./src/language_name');
|
|
5
|
+
const shortDateTimeFormatFactory = require('./src/short_datetime_format');
|
|
6
|
+
const yearIntervalFactory = require('./src/year_interval');
|
|
7
|
+
|
|
8
|
+
module.exports = (eleventyConfig, pluginConfig = {}) => {
|
|
9
|
+
const { locale } = pluginConfig;
|
|
10
|
+
|
|
11
|
+
eleventyConfig.addFilter('country_name', countryNameFactory(locale));
|
|
12
|
+
eleventyConfig.addFilter('language_name', languageNameFactory(locale));
|
|
13
|
+
eleventyConfig.addFilter('short_datetime_format', shortDateTimeFormatFactory(locale));
|
|
14
|
+
eleventyConfig.addFilter('year_interval', yearIntervalFactory(locale));
|
|
15
|
+
};
|
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"eslint:recommended",
|
|
4
|
+
"standard"
|
|
5
|
+
],
|
|
6
|
+
|
|
7
|
+
"ignorePatterns": [
|
|
8
|
+
"!.eleventy.js"
|
|
9
|
+
],
|
|
10
|
+
|
|
11
|
+
"parserOptions": {
|
|
12
|
+
"sourceType": "script"
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
"root": true,
|
|
16
|
+
|
|
17
|
+
"rules": {
|
|
18
|
+
"curly": ["error", "all"],
|
|
19
|
+
"array-element-newline": ["error", "consistent"],
|
|
20
|
+
"arrow-parens": ["error", "always"],
|
|
21
|
+
"comma-dangle": ["error", {
|
|
22
|
+
"arrays": "always-multiline",
|
|
23
|
+
"exports": "always-multiline",
|
|
24
|
+
"functions": "never",
|
|
25
|
+
"imports": "always-multiline",
|
|
26
|
+
"objects": "always-multiline"
|
|
27
|
+
}],
|
|
28
|
+
"indent": ["error", "tab"],
|
|
29
|
+
"no-tabs": "off",
|
|
30
|
+
"object-property-newline": ["error", {
|
|
31
|
+
"allowAllPropertiesOnSameLine": false
|
|
32
|
+
}],
|
|
33
|
+
"semi": ["error", "always"],
|
|
34
|
+
"valid-jsdoc": "off"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v16.13.1
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2021 Attila Gonda https://attilagonda.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# eleventy-plugin-intl-utils
|
|
2
|
+
A set of internationalization utils for [Eleventy](https://www.11ty.dev/).
|
|
3
|
+
|
|
4
|
+
## Motivation
|
|
5
|
+
It's super fast to create a static website with **Eleventy** and with the help of [`eleventy-plugin-i18n`](https://www.npmjs.com/package/eleventy-plugin-i18n) localization can be done with ease too.
|
|
6
|
+
|
|
7
|
+
However it only gives solution for static data and dynamic data still needs treatment during layout rendering. This plugin aims to help with a set of [filters](https://www.11ty.dev/docs/filters/).
|
|
8
|
+
|
|
9
|
+
The implementations of the filters are mostly wrappers around the [ECMAScript Internationalization API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) with a little _bit of sugar_.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
This guide corresponds to [Eleventy documentation](https://www.11ty.dev/docs/plugins/#add-the-plugin-to-eleventy-in-your-config-file):
|
|
13
|
+
|
|
14
|
+
1. Install the package
|
|
15
|
+
```bash
|
|
16
|
+
npm install --save @pcdevil/eleventy-plugin-intl-utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
2. Add to your config file
|
|
20
|
+
```javascript
|
|
21
|
+
// file: .eleventy.js
|
|
22
|
+
const intlUtils = require('@pcdevil/eleventy-plugin-intl-utils');
|
|
23
|
+
|
|
24
|
+
module.exports = function (eleventyConfig) {
|
|
25
|
+
// ...
|
|
26
|
+
|
|
27
|
+
const intlUtilConfig = {
|
|
28
|
+
locale: 'en-GB',
|
|
29
|
+
};
|
|
30
|
+
eleventyConfig.addPlugin(intlUtils, intlUtilConfig);
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Configuration object
|
|
35
|
+
A general configuration object can be passed optionally via Eleventy config when the plugin is initiated. This sections lists the available configuration properties.
|
|
36
|
+
|
|
37
|
+
### `locale`
|
|
38
|
+
This property affects the output language of the filters. See [`locales` argument section on Intl page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument) for more info.
|
|
39
|
+
|
|
40
|
+
- **Type**: String
|
|
41
|
+
- **Default value**: `undefined` (**Node.js** will pick up the system's language)
|
|
42
|
+
- **Example values**: `"en-GB"`, `"hu"`
|
|
43
|
+
|
|
44
|
+
## Filters
|
|
45
|
+
|
|
46
|
+
### `country_name`
|
|
47
|
+
Transforms a country code into a country name. Useful to display author's location.
|
|
48
|
+
|
|
49
|
+
- **Used Internationalization API**: `Intl.DisplayNames()` with _region_ type ([MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames))
|
|
50
|
+
|
|
51
|
+
#### LiquidJS Example
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
{% assign country = "FR" %}
|
|
55
|
+
{{ country | country_name }}
|
|
56
|
+
{% comment %} renders "France" when the `intlUtilConfig.locale` is `en` {% endcomment %}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `language_name`
|
|
60
|
+
Transforms a language code to a renderable text. Useful for language selectors or when the current language is displayed.
|
|
61
|
+
|
|
62
|
+
- **Used Internationalization API**: `Intl.DisplayNames()` with _language_ type ([MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames#using_type_language_with_languagedisplay))
|
|
63
|
+
|
|
64
|
+
#### LiquidJS Example
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
{% assign language = "en-GB" %}
|
|
68
|
+
{{ language | language_name }}
|
|
69
|
+
{% comment %} renders "British English" when the `intlUtilConfig.locale` is `en` {% endcomment %}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### `short_datetime_format`
|
|
73
|
+
Transforms a date string to a short date time text. Useful for blog post dates.
|
|
74
|
+
|
|
75
|
+
- **Used Internationalization API**: `Intl.DateTimeFormat()` with _short_ date and time style ([MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat))
|
|
76
|
+
|
|
77
|
+
#### LiquidJS Example
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
{% assign postDate = "2021-12-12 17:36 +0100" %}
|
|
81
|
+
{{ postDate | short_datetime_format }}
|
|
82
|
+
{% comment %} renders "12/12/21, 5:36 PM" when the `intlUtilConfig.locale` is `en` {% endcomment %}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### `year_interval`
|
|
86
|
+
Transforms two date strings to a year range. Useful when a larger time interval is presented.
|
|
87
|
+
|
|
88
|
+
The input should be an array with two date string and the first one should precede before the second one.
|
|
89
|
+
|
|
90
|
+
- **Used Internationalization API**: `Intl.DateTimeFormat.prototype.formatRange()` with _numeric_ year ([MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRange))
|
|
91
|
+
|
|
92
|
+
#### LiquidJS Example
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
{% assign dates = "2011-05-16 11:00 +0200, 2020-03-01 00:00 +0100" | split: ", " %}
|
|
96
|
+
{{ dates | year_interval }}
|
|
97
|
+
{% comment %} renders "2011 – 2020" (with en dash in the middle) when the `intlUtilConfig.locale` is `en` {% endcomment %}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
Available under the [MIT license](LICENSE.md).
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pcdevil/eleventy-plugin-intl-utils",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"description": "A set of internationalization utils for Eleventy.",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">= 16.0.0"
|
|
11
|
+
},
|
|
12
|
+
"main": ".eleventy.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"lint": "npx eslint ."
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/pcdevil/eleventy-plugin-intl-utils.git"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"11ty",
|
|
22
|
+
"blog",
|
|
23
|
+
"eleventy",
|
|
24
|
+
"internationalization",
|
|
25
|
+
"static-site",
|
|
26
|
+
"website"
|
|
27
|
+
],
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Attila Gonda",
|
|
30
|
+
"url": "https://attilagonda.com"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/pcdevil/eleventy-plugin-intl-utils/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/pcdevil/eleventy-plugin-intl-utils#readme",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"eslint": "^7.32.0",
|
|
39
|
+
"eslint-config-standard": "^16.0.3",
|
|
40
|
+
"eslint-plugin-import": "^2.25.3",
|
|
41
|
+
"eslint-plugin-node": "^11.1.0",
|
|
42
|
+
"eslint-plugin-promise": "^5.2.0"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function shortDateTimeFormatFactory (locale) {
|
|
4
|
+
const dateTimeFormat = new Intl.DateTimeFormat(locale, {
|
|
5
|
+
dateStyle: 'short',
|
|
6
|
+
timeStyle: 'short',
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
return (date) => dateTimeFormat.format(new Date(date));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = shortDateTimeFormatFactory;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function yearIntervalFactory (locale) {
|
|
4
|
+
const dateTimeFormat = new Intl.DateTimeFormat(locale, {
|
|
5
|
+
year: 'numeric',
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
return ([startDate, endDate]) => dateTimeFormat.formatRange(new Date(startDate), new Date(endDate));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = yearIntervalFactory;
|