Skip to content

Commit

Permalink
feat(i18n-loader): handle projects with one file per language
Browse files Browse the repository at this point in the history
  • Loading branch information
aegenet committed Jun 6, 2022
1 parent 016b506 commit 1c25ea2
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 200 deletions.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,31 @@ npm i @creadigme/au-i18n-audit --save-dev

1. Check the directories of your project, example :

i18n with a specific file per namespace (`--i18nMode tree`):

```shell

├── i18n
│ ├── fr
│ │ ├── NS1.{yml,json}
│ │ └── NS2.{yml,json}
│ │ ├── NS1.{yml,yaml,json}
│ │ └── NS2.{yml,yaml,json}
│ └── en
│ ├── NS1.{yml,json}
│ └── NS2.{yml,json}
│ ├── NS1.{yml,yaml,json}
│ └── NS2.{yml,yaml,json}
└── src
├── file1.{js,ts}
├── file2.{js,ts}
└── file3.{js,ts}

```

i18n with a specific file for each language (`--i18nMode root`):

```shell

├── i18n
│ ├── fr.{yml,yaml,json}
│ └── en.{yml,yaml,json}
└── src
├── file1.{js,ts}
├── file2.{js,ts}
Expand All @@ -82,9 +98,10 @@ npm i @creadigme/au-i18n-audit --save-dev

2. Add i18n script

```json
```diff
"scripts": {
"i18n" : "au-i18n-audit --src ./src/ --i18n ./i18n --reporter summary"
+ "i18n" : "au-i18n-audit --src ./src/ --i18n ./i18n --i18nMode tree --reporter summary"
! "i18n" : "au-i18n-audit --src ./src/ --i18n ./i18n --i18nMode root --reporter summary"
}
```

Expand Down
8 changes: 8 additions & 0 deletions samples/case_04/i18n/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"EASY": {
"STOP": "STOP",
"START": "Start",
"PAUSE": "Pause",
"NOT_USED": "Another One"
}
}
18 changes: 18 additions & 0 deletions samples/case_04/src/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>

<div class="something else" title.one-time="'EASY.PAUSE' & t">?</div>
<div class="something else" title.bind="'EASY.START' & t">?</div>
<div class="something else" title.one-time="'EASY.START' & t">?</div>
<div class="something else" title.bind="'EASY.STOP' & t">?</div>

<div class="something else">${"EASY.KEY" & t} : </div>
<div class="something else">${'EASY.KEY2' | t} : </div>
<!-- ignore this -->
<div class="something else">${'EASY.KEY3'} : </div>


<div class="something else" t="EASY.KEY"></div>
<div class="something else" t="EASY.KEY2"></div>
<!-- ignore this -->
<div class="something else" tt="EASY.KEY3"></div>
</template>
28 changes: 28 additions & 0 deletions samples/case_04/src/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export class Sample3 {
public i18n: any;

/**
*/
public something() {
return this.i18n.tr('EASY.START');
}

/**
*/
public summoner() {
return this.i18n.tr('EASY.STOP');
}

/**
*/
public invocation() {
return this.i18n.tr('EASY.MIDDLE');
}

/**
*/
public moon() {
// return true; this.i18n.tr("EASY.MOON");
return true; // this.i18n.tr("EASY.MOON");
}
}
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const commandLineArgs = commandLineArgsImp.default || commandLineArgsImp;
const optionDefinitions = [
{ name: 'src', type: String, multiple: true },
{ name: 'i18n', type: String, multiple: true },
{ name: 'i18nMode', type: String },
{ name: 'remote-i18n', type: String, multiple: false },
{ name: 'lang', type: String, multiple: true },
{ name: 'namespace', type: String, multiple: true },
Expand All @@ -31,6 +32,7 @@ CLIReporter.cliAsync({
local: options.i18n
? {
i18nPaths: options.i18n,
mode: options.i18nMode,
}
: undefined,
remote: options['remote-i18n']
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/i18n-audit-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';
import { forwardSlash } from '../utils/forward-slash';
import { ELevel } from './e-level';
import { II18NConfig } from './i-i18n-config';
import { ILocalOptions, IRemoteOptions } from './i18n-loader';
import type { ILocalOptions, IRemoteOptions } from './loader/provider/i18n-loader-common';

export class I18NAuditOptions {
private _isInit?: boolean = false;
Expand Down
63 changes: 62 additions & 1 deletion src/i18n/i18n-audit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('i18n-audit', () => {
});

describe('lint', () => {
describe('local', () => {
describe('local - Tree i18n/lang/ns', () => {
it('html without i18n', async () => {
const audit = new I18NAudit({
srcPaths: [path.resolve('.\\samples\\wo_i18n\\src')],
Expand Down Expand Up @@ -176,6 +176,67 @@ describe('i18n-audit', () => {
});
});

describe('local - Root i18n/{{lang}}.fr', () => {
it('easy', async () => {
const audit = new I18NAudit(
new I18NAuditOptions({
srcPaths: [path.resolve('.\\samples\\case_04\\src')],
local: {
i18nPaths: [path.resolve('.\\samples\\case_04\\i18n')],
mode: 'root',
},
level: ELevel.EASY,
})
);

await audit.initializeAsync();
const details = await audit.validateAsync();
assert.strictEqual(details.isOk, true);
assert.strictEqual(details.languages.length, 1);
assert.strictEqual(details.unused.length, 1);
assert.strictEqual(Object.keys(details.missingKeys).length, 3);
});

it('medium', async () => {
const audit = new I18NAudit({
srcPaths: [path.resolve('.\\samples\\case_04\\src')],
local: {
i18nPaths: [path.resolve('.\\samples\\case_04\\i18n')],
mode: 'root',
},
level: ELevel.MEDIUM,
});

await audit.initializeAsync();
const details = await audit.validateAsync();
assert.strictEqual(details.isOk, false);
assert.strictEqual(details.languages.length, 1);
assert.strictEqual(details.unused.length, 1);
assert.strictEqual(Object.keys(details.missingKeys).length, 3);
});

it('hard', async () => {
const audit = new I18NAudit({
srcPaths: [path.resolve('.\\samples\\case_04\\src')],
local: {
i18nPaths: [path.resolve('.\\samples\\case_04\\i18n')],
mode: 'root',
},
level: ELevel.HARD,
i18nConfig: {
attributes: [],
},
});

await audit.initializeAsync();
const details = await audit.validateAsync();
assert.strictEqual(details.isOk, false);
assert.strictEqual(details.languages.length, 1);
assert.strictEqual(details.unused.length, 1);
assert.strictEqual(Object.keys(details.missingKeys).length, 3);
});
});

describe('remote', () => {
const fakeI18nBackend = new FakeI18NBackend();

Expand Down
3 changes: 2 additions & 1 deletion src/i18n/i18n-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { II18NAuditResults } from './i-i18n-audit-results';
import { ELevel } from './e-level';

import { I18NSeeker } from './i18n-seeker';
import { I18NLoader, I18NKeys } from './i18n-loader';
import { I18NLoader } from './loader/i18n-loader';
import type { I18NKeys } from './loader/provider/i18n-loader-common';

/**
* I18N Audit
Expand Down
Loading

0 comments on commit 1c25ea2

Please sign in to comment.