svelte-django-gettext 0.0.1 → 1.0.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/.babelrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "useBuiltIns": "entry",
7
+ "corejs": {"version": "3"}
8
+ }
9
+ ]
10
+ ]
11
+ }
@@ -0,0 +1,15 @@
1
+ name: CI
2
+ on: [push]
3
+ jobs:
4
+ eslint:
5
+ runs-on: ubuntu-latest
6
+ steps:
7
+ - uses: actions/checkout@v2.4.0
8
+ - run: npm install
9
+ - run: npm run eslint
10
+ tests:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v2.4.0
14
+ - run: npm install
15
+ - run: npm run test
package/README.md CHANGED
@@ -20,7 +20,7 @@ export default {
20
20
  }),
21
21
  ...
22
22
 
23
- writeGettextExtracts('js/_),
23
+ writeGettextExtracts('js/_svelte_django_extracted_messages.js'),
24
24
  ]
25
25
  }
26
26
  ```
@@ -37,8 +37,7 @@ import gettext from 'svelte-django-gettext';
37
37
  <h1>{gettext('hello')}</h1>
38
38
  ```
39
39
 
40
-
41
- Make sure the page loads the generated catalog, from a django template: `<script src="{% url 'javascript-catalog' %}"></script>`
40
+ Make sure the page loads the generated catalog, from a django template: `<script src="{% url 'javascript-catalog' %}"></script>`. If this is not the case, all functions will just return their message id.
42
41
 
43
42
  # How it works:
44
43
 
package/build.js CHANGED
@@ -21,7 +21,7 @@ export function writeGettextExtracts(extractsFile = '_svelte_django_extracted_me
21
21
  };
22
22
  }
23
23
 
24
- export async function gettextStringExtractor({ content, attributes, filename }) {
24
+ export async function gettextStringExtractor({ content, filename }) {
25
25
  if (content.indexOf('gettext') === -1) {
26
26
  return;
27
27
  }
package/index.js CHANGED
@@ -1,20 +1,33 @@
1
- export * from './build.js'
1
+ export * from './build';
2
2
 
3
-
4
- function capfirst(s) {
5
- return s.charAt(0).toUpperCase() + s.slice(1);
3
+ /* Proxy to globally defined django.*gettext functions if it is available.
4
+ */
5
+ function capfirst(str, condition) {
6
+ return condition ? (str.charAt(0).toUpperCase() + str.slice(1)) : str;
6
7
  }
7
8
 
8
9
  export function gettext(msgid, doCapfirst) {
9
10
  const django = window.django;
10
11
  const msgstr = django ? django.gettext(msgid) : msgid;
11
- return doCapfirst ? capfirst(msgstr) : msgstr;
12
- };
12
+ return capfirst(msgstr, doCapfirst);
13
+ }
13
14
 
14
15
  export function pgettext(context, msgid, doCapfirst) {
15
16
  const django = window.django;
16
17
  const msgstr = django ? django.pgettext(context, msgid) : msgid;
17
- return doCapfirst ? capfirst(msgstr) : msgstr;
18
+ return capfirst(msgstr, doCapfirst);
19
+ }
20
+
21
+ export function ngettext(singular, plural, count, doCapfirst) {
22
+ const django = window.django;
23
+ const msgstr = django ? django.ngettext(singular, plural, count) : (count === 1 ? singular : plural);
24
+ return capfirst(msgstr, doCapfirst);
25
+ }
26
+
27
+ export function npgettext(context, singular, plural, count, doCapfirst) {
28
+ const django = window.django;
29
+ const msgstr = django ? django.npgettext(context, singular, plural, count) : (count === 1 ? singular : plural);
30
+ return capfirst(msgstr, doCapfirst);
18
31
  }
19
32
 
20
33
  export default gettext;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "svelte-django-gettext",
3
- "version": "0.0.1",
3
+ "version": "1.0.0",
4
4
  "description": "Use django gettext in Svelte",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ "test": "jest test",
9
+ "eslint": "eslint -c package.json --fix *.js test/"
9
10
  },
10
11
  "keywords": [
11
12
  "Svelte",
@@ -14,5 +15,60 @@
14
15
  "i18n"
15
16
  ],
16
17
  "author": "Jan Pieter Waagmeester",
17
- "license": "ISC"
18
- }
18
+ "license": "ISC",
19
+ "devDependencies": {
20
+ "@babel/preset-env": "^7.16.0",
21
+ "eslint": "^8.3.0",
22
+ "eslint-plugin-import": "^2.25.3",
23
+ "jest": "^27.3.1"
24
+ },
25
+ "jest": {
26
+ "testEnvironment": "jsdom"
27
+ },
28
+ "eslintConfig": {
29
+ "env": {
30
+ "browser": true,
31
+ "es2021": true
32
+ },
33
+ "extends": "eslint:recommended",
34
+ "parserOptions": {
35
+ "ecmaVersion": 13,
36
+ "sourceType": "module"
37
+ },
38
+ "plugins": [
39
+ "import"
40
+ ],
41
+ "rules": {
42
+ "indent": [
43
+ "error",
44
+ 4,
45
+ {
46
+ "SwitchCase": 1
47
+ }
48
+ ],
49
+ "quotes": [
50
+ "error",
51
+ "single"
52
+ ],
53
+ "import/order": [
54
+ "error",
55
+ {
56
+ "groups": [
57
+ "builtin",
58
+ "external",
59
+ "internal",
60
+ [
61
+ "parent",
62
+ "sibling"
63
+ ]
64
+ ],
65
+ "newlines-between": "always",
66
+ "alphabetize": {
67
+ "order": "asc",
68
+ "caseInsensitive": true
69
+ }
70
+ }
71
+ ]
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,65 @@
1
+ /* globals describe, expect, test */
2
+ import { gettext, ngettext, npgettext, pgettext } from '../index.js';
3
+
4
+
5
+ describe('gettext', function() {
6
+ test('Is identity function if django is not defined', function() {
7
+ window.django = undefined;
8
+ expect(gettext('house')).toBe('house');
9
+ expect(gettext('house', true)).toBe('House');
10
+ });
11
+
12
+ test('Proxies to django.gettext', function() {
13
+ window.django = {
14
+ gettext: function() {
15
+ return 'huis';
16
+ }
17
+ };
18
+ expect(gettext('house')).toBe('huis');
19
+ expect(gettext('house', true)).toBe('Huis');
20
+ });
21
+ });
22
+
23
+ describe('ngettext', function() {
24
+ test('Is identity function if django is not defined', function() {
25
+ window.django = undefined;
26
+ expect(ngettext('house', 'houses', 1)).toBe('house');
27
+ expect(ngettext('house', 'houses', 2)).toBe('houses');
28
+ expect(ngettext('house', 'houses', 1, true)).toBe('House');
29
+ expect(ngettext('house', 'houses', 2, true)).toBe('Houses');
30
+ });
31
+ test('Proxies to django.ngettext', function() {
32
+ window.django = {
33
+ ngettext: function(singular, plural, count) {
34
+ return count == 1 ? 'huis' : 'huizen';
35
+ }
36
+ };
37
+ expect(ngettext('house', 'houses', 1)).toBe('huis');
38
+ expect(ngettext('house', 'houses', 2)).toBe('huizen');
39
+ });
40
+ });
41
+
42
+ describe('npgettext', function() {
43
+ test('Is identity function if django is not defined', function() {
44
+ window.django = undefined;
45
+ expect(npgettext('search results', '1 result', '%d results', 1)).toBe('1 result');
46
+ });
47
+ });
48
+
49
+
50
+ describe('pgettext', function() {
51
+ test('Is identity function if django is not defined', function() {
52
+ window.django = undefined;
53
+ expect(pgettext('date', 'may')).toBe('may');
54
+ expect(pgettext('date', 'may', true)).toBe('May');
55
+ });
56
+ test('Proxies to django.pgettext', function() {
57
+ window.django = {
58
+ pgettext: function() {
59
+ return 'mei'
60
+ }
61
+ };
62
+ expect(pgettext('date', 'may')).toBe('mei');
63
+ expect(pgettext('date', 'may', true)).toBe('Mei');
64
+ });
65
+ });