Skip to content

Commit 5404601

Browse files
committed
Initial commit
add github actions
0 parents  commit 5404601

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+13068
-0
lines changed

.github/workflows/lint-and-test.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Lint & Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
test:
13+
name: Test
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
node-version: ['14']
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Setup node
23+
uses: actions/setup-node@v2
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
cache: 'npm'
27+
- name: Install npm@8
28+
run: npm i -g npm@8
29+
- name: Install
30+
run: npm ci --prefer-offline
31+
- run: npm run build
32+
- run: npm test
33+
env:
34+
CI: true

.github/workflows/publish-to-npm.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Publish to NPM
2+
3+
on:
4+
workflow_dispatch
5+
6+
jobs:
7+
build:
8+
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
matrix:
13+
node-version: ['14']
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Setup node
18+
uses: actions/setup-node@v2
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
cache: 'npm'
22+
- name: Install npm@8
23+
run: npm i -g npm@8
24+
- name: Install
25+
run: npm ci --prefer-offline
26+
- run: npm run build
27+
- run: npm test
28+
env:
29+
CI: true
30+
- name: Publish to NPM
31+
uses: JS-DevTools/npm-publish@v1
32+
with:
33+
token: ${{ secrets.NPM_TOKEN }}

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
.idea
3+
/dist
4+
/node_modules
5+
/locales

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Wuif
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, 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,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# next-translate Scanner
2+
3+
[![npm version](https://img.shields.io/npm/v/next-translate-scanner)](https://www.npmjs.com/package/next-translate-scanner)
4+
5+
Used to extract translations for https://github.com/vinissimus/next-translate.
6+
7+
When translating an application, maintaining the translation catalog by hand is painful.
8+
This package parses your code and automates this process.
9+
10+
## Features
11+
12+
- Choose your weapon: A CLI, a standalone parser or a stream transform
13+
- Creates one catalog file per locale and per namespace
14+
- Supports next-translate features:
15+
- **Default Value**: fill translations with provided default values
16+
- **Plural**: keys of the form `key_zero`, `key_one`, `key_two`, `key_few`, `key_many` , `key_other` and numbers
17+
18+
## Usage
19+
20+
### CLI
21+
22+
```bash
23+
npm install next-translate-scanner
24+
```
25+
26+
```js
27+
// next-translate-scanner.config.js
28+
module.exports = {
29+
input: [
30+
'./pages/**/*.@(jsx|tsx|js|ts)',
31+
'./components/**/*.@(jsx|tsx|js|ts)'
32+
],
33+
output: './locales/$LOCALE/$NAMESPACE.json'
34+
}
35+
36+
```
37+
38+
```
39+
// package.json
40+
{
41+
"scripts": {
42+
...
43+
"extract-translations": "next-translate-scanner"
44+
}
45+
}
46+
```
47+
48+
### *.ts / *.tsx
49+
50+
Typescript is supported via Javascript and Jsx lexers. If you are using Javascript syntax (e.g. with React), follow the steps in Jsx section, otherwise Javascript section.
51+
52+
### Configuration
53+
54+
```ts
55+
type ScannerConfig = {
56+
// Array of strings using the glob syntax (https://www.npmjs.com/package/glob)
57+
input: string[] | string
58+
// Available locales, can be importet from i18n.js
59+
locales: string[]
60+
61+
// Change the separator that is used for nested keys. Set to false to disable keys nesting in JSON translation files. Can be useful if you want to use natural text as keys.
62+
keySeparator: string
63+
// Char to split namespace from key. You should set it to false if you want to use natural text as keys.
64+
nsSeparator: string
65+
// Default namespace used if not passed to useTranslation or in the translation key.
66+
defaultNS?: string
67+
68+
// If keys inside json should be sorted
69+
sort: boolean
70+
// If keys removed from code should be deleted automatically
71+
keepRemoved: boolean
72+
// Output path for the generated files (default: './locales/$LOCALE/$NAMESPACE.json')
73+
output: string
74+
// Create default value from element
75+
defaultValue: (data: ExtractedElement) => string | null | undefined
76+
// Indention used in json files (default: 2 spaces)
77+
indentation: number
78+
// Replace existing translations if default values are set in code
79+
replaceDefaults: boolean
80+
// Fail the task if any warning is triggered
81+
failOnWarnings: boolean
82+
}
83+
```
84+
85+
### Caveats
86+
87+
While next-translate extracts translation keys in runtime, next-translate-scanner doesn't run the code, so it can't interpolate values in these expressions:
88+
89+
```
90+
t(key)
91+
t('key' + id)
92+
t(`key${id}`)
93+
```
94+
95+
As a workaround you should specify possible static values in comments anywhere in your file:
96+
97+
```
98+
// t('key_1')
99+
// t('key_2')
100+
t(key)
101+
102+
/*
103+
t('key1')
104+
t('key2')
105+
*/
106+
t('key' + id)
107+
```
108+
109+
## Info
110+
111+
Lexers where inspired by https://github.com/i18next/i18next-parser

_test-files/files/demo.jsx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
import { useTranslation } from 'next-translate'
3+
4+
export default function Description() {
5+
const { t } = useTranslation('ns1')
6+
const title = t('title')
7+
const nested = t('my.nested.key')
8+
const description = t`description`
9+
const example = t('ns2:example', { count: 3 })
10+
11+
return (
12+
<>
13+
<h1>{title}</h1>
14+
<p>{description}</p>
15+
<p>{example}</p>
16+
<p>{nested}</p>
17+
<>
18+
)
19+
}

_test-files/files/use-demo.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
import useTranslation from 'next-translate/useTranslation'
3+
4+
export default function useDemo() {
5+
const { t } = useTranslation('ns1')
6+
const title = t('title')
7+
const description = t`description`
8+
const example = t('ns2:example', { count: 3 })
9+
10+
return title + description + example
11+
}

_test-files/locales/de/common.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"key": "de_translation",
3+
"first_one": "de_first",
4+
"first_other": "de_first plural",
5+
"second_one": "de_second",
6+
"second_zero": "de_second plural zero",
7+
"second_other": "de_second plural other",
8+
"my": {
9+
"nested": {
10+
"key": "de_nested key",
11+
"plural_one": "de_plural",
12+
"plural_other": "de_plural plural"
13+
}
14+
}
15+
}

_test-files/locales/de/ns1.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"key": "de_translation"
3+
}

_test-files/locales/en/common.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"key": "en_translation",
3+
"first_one": "en_first",
4+
"first_other": "en_first plural",
5+
"second_one": "en_second",
6+
"second_zero": "en_second plural zero",
7+
"second_other": "en_second plural other",
8+
"my": {
9+
"nested": {
10+
"key": "en_nested key",
11+
"plural_one": "en_plural",
12+
"plural_other": "en_plural plural"
13+
}
14+
}
15+
}

_test-files/locales/en/ns1.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"key": "en_translation"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
input: ['_test-files/files/**/*.@(jsx|js)', '_test-files/files/**/*.@(jsx|js)']
3+
}

_test-files/test-config.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ScannerConfig } from '../src/types/scanner-config.type'
2+
3+
const testConfig: ScannerConfig = {
4+
input: [],
5+
locales: ['en', 'de'],
6+
7+
keySeparator: '.',
8+
nsSeparator: ':',
9+
defaultNS: undefined,
10+
11+
sort: true,
12+
keepRemoved: true,
13+
output: './locales/$LOCALE/$NAMESPACE.json',
14+
defaultValue: () => null,
15+
indentation: 2,
16+
replaceDefaults: false,
17+
failOnWarnings: false
18+
}
19+
20+
export default testConfig

babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', {targets: {node: 'current'}}],
4+
'@babel/preset-typescript',
5+
],
6+
};

0 commit comments

Comments
 (0)