Skip to content

Commit e7d2988

Browse files
Fa-BRAIKxiCO2k
andauthored
Vite plugin options (#144)
* updating docs * addition vite plugin options * Using one param * adding tests * Updating docs * Updating the docs * formatting --------- Co-authored-by: Francisco Madeira <[email protected]>
1 parent 4eec0e7 commit e7d2988

File tree

8 files changed

+101
-8
lines changed

8 files changed

+101
-8
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ export default defineConfig({
8686
});
8787
```
8888

89+
#### Vite plugin options
90+
91+
In addition to that, you can use this `Vite` plugin with additional paths to load from, this is usefull when you are using a package that let's you override your translations, or in case you are getting your application's lang files from different paths.
92+
93+
Note that if one key found in two paths, priority will be given to the last given path between these two (In this example translation key will be loaded from `public/locales`)
94+
95+
```js
96+
// vite.config.js
97+
import i18n from 'laravel-vue-i18n/vite';
98+
99+
export default defineConfig({
100+
plugins: [
101+
laravel([
102+
'resources/css/app.css'
103+
'resources/js/app.js',
104+
]),
105+
vue(),
106+
107+
i18n({
108+
// you can also change your langPath here
109+
// langPath: 'locales'
110+
additionalLangPaths: [
111+
'public/locales' // Load translations from this path too!
112+
]
113+
}),
114+
],
115+
});
116+
```
117+
89118
> During the `npm run dev` execution time, the plugin will create some files like this `php_{lang}.json` on your lang folder.
90119
> And to avoid that to be commited to your code base, I suggest to your `.gitignore` this like:
91120

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/interfaces/plugin-options.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ import { OptionsInterface } from './options'
66
export interface PluginOptionsInterface extends OptionsInterface {
77
shared?: boolean
88
}
9+
10+
export interface VitePluginOptionsInterface {
11+
langPath?: string
12+
additionalLangPaths?: string[]
13+
}

src/loader.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ export const readThroughDir = (dir) => {
147147
return data
148148
}
149149

150+
export const prepareExtendedParsedLangFiles = (langPaths: string[]) =>
151+
langPaths.reduce((acc, langPath) => [...acc, ...parseAll(langPath)], new Array<ParsedLangFileInterface>())
152+
150153
export const generateFiles = (langPath: string, data: ParsedLangFileInterface[]): ParsedLangFileInterface[] => {
151154
data = mergeData(data)
152155

src/vite.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import path from 'path'
2-
import { existsSync, writeFileSync, unlinkSync, readdirSync, rmdirSync } from 'fs'
3-
import { parseAll, hasPhpTranslations, generateFiles } from './loader'
2+
import { existsSync, unlinkSync, readdirSync, rmdirSync } from 'fs'
3+
import { hasPhpTranslations, generateFiles, prepareExtendedParsedLangFiles } from './loader'
44
import { ParsedLangFileInterface } from './interfaces/parsed-lang-file'
5+
import { VitePluginOptionsInterface } from './interfaces/plugin-options'
56
import { Plugin } from 'vite'
67

7-
export default function i18n(langPath: string = 'lang'): Plugin {
8+
export default function i18n(options: string | VitePluginOptionsInterface = 'lang'): Plugin {
9+
let langPath = typeof options === 'string' ? options : options.langPath ?? 'lang'
810
langPath = langPath.replace(/[\\/]$/, '') + path.sep
911

12+
const additionalLangPaths = typeof options === 'string' ? [] : options.additionalLangPaths ?? []
13+
1014
const frameworkLangPath = 'vendor/laravel/framework/src/Illuminate/Translation/lang/'.replace('/', path.sep)
1115
let files: ParsedLangFileInterface[] = []
1216
let exitHandlersBound: boolean = false
@@ -40,11 +44,15 @@ export default function i18n(langPath: string = 'lang'): Plugin {
4044
return
4145
}
4246

43-
files = generateFiles(langPath, [...parseAll(frameworkLangPath), ...parseAll(langPath)])
47+
const langPaths = prepareExtendedParsedLangFiles([frameworkLangPath, langPath, ...additionalLangPaths])
48+
49+
files = generateFiles(langPath, langPaths)
4450
},
4551
handleHotUpdate(ctx) {
4652
if (/lang\/.*\.php$/.test(ctx.file)) {
47-
files = generateFiles(langPath, [...parseAll(frameworkLangPath), ...parseAll(langPath)])
53+
const langPaths = prepareExtendedParsedLangFiles([frameworkLangPath, langPath, ...additionalLangPaths])
54+
55+
files = generateFiles(langPath, langPaths)
4856
}
4957
},
5058
configureServer(server) {

test/fixtures/locales/en/auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'failed' => 'These credentials are incorrect.',
5+
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.'
6+
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'sub_dir_support_is_amazing' => 'Subdirectory override is amazing',
5+
];

test/loader.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { generateFiles, parseAll, parse, hasPhpTranslations, reset } from '../src/loader';
2+
import { generateFiles, parseAll, parse, hasPhpTranslations, reset, prepareExtendedParsedLangFiles } from '../src/loader';
33

44
beforeEach(() => reset(__dirname + '/fixtures/lang/'));
55

@@ -41,6 +41,43 @@ it('includes .php lang file in nested subdirectory in .json', () => {
4141
expect(langEn['nested.cars.car.foo.level1.level2']).toBe('barpt');
4242
})
4343

44+
it('inclues additional lang paths to load from', () => {
45+
const langPath = __dirname + '/fixtures/lang/';
46+
const additionalLangPaths = [
47+
__dirname + '/fixtures/locales/'
48+
];
49+
50+
const langPaths = prepareExtendedParsedLangFiles([
51+
langPath,
52+
...additionalLangPaths,
53+
]);
54+
55+
const files = generateFiles(langPath, langPaths);
56+
57+
const langEn = JSON.parse(fs.readFileSync(langPath + files[0].name).toString());
58+
59+
expect(langEn['auth.throttle']).toBe('Too many login attempts. Please try again in :seconds seconds.');
60+
});
61+
62+
it('overwrites translations from additional lang paths', () => {
63+
const langPath = __dirname + '/fixtures/lang/';
64+
const additionalLangPaths = [
65+
__dirname + '/fixtures/locales/'
66+
];
67+
68+
const langPaths = prepareExtendedParsedLangFiles([
69+
langPath,
70+
...additionalLangPaths,
71+
]);
72+
73+
const files = generateFiles(langPath, langPaths);
74+
75+
const langEn = JSON.parse(fs.readFileSync(langPath + files[0].name).toString());
76+
77+
expect(langEn['auth.failed']).toBe('These credentials are incorrect.');
78+
expect(langEn['domain.user.sub_dir_support_is_amazing']).toBe('Subdirectory override is amazing');
79+
});
80+
4481
it('transforms .php lang to .json', () => {
4582
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/auth.php').toString());
4683

0 commit comments

Comments
 (0)