Skip to content

Commit 8076362

Browse files
committed
feat: Add env var.
1 parent ea1a798 commit 8076362

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ function setLanguage({ lang, messages }: LanguageInterface): string {
138138
* It resolves the language file or data, from direct data, require or Promise.
139139
*/
140140
function resolveLang(callable: Function, lang: string): Promise<LanguageJsonFileInterface> {
141+
console.log('HAS PHP', process?.env?.LARAVEL_VUE_I18N_HAS_PHP);
142+
141143
const data = callable(lang)
142144

143145
if (data instanceof Promise) {

src/loader.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ import fs from 'fs';
22
import path from 'path';
33
import { Engine } from 'php-parser';
44

5+
export const hasPhpTranslations = (folderPath: string): boolean => {
6+
folderPath = folderPath.replace(/[\\/]$/, '') + path.sep;
7+
8+
const folders = fs.readdirSync(folderPath)
9+
.filter(file => fs.statSync(folderPath + path.sep + file).isDirectory())
10+
.sort();
11+
12+
for (const folder of folders) {
13+
const lang = {};
14+
15+
const files = fs.readdirSync(folderPath + path.sep + folder)
16+
.filter(file => /\.php$/.test(file));
17+
18+
if (files.length > 0) {
19+
return true;
20+
}
21+
}
22+
23+
return false;
24+
}
25+
526
export const parseAll = (folderPath: string): { name: string, path: string }[] => {
627
folderPath = folderPath.replace(/[\\/]$/, '') + path.sep;
728

src/mix.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import path from 'path';
22
import fs from 'fs';
33

4-
// @ts-ignore
54
import mix from 'laravel-mix';
6-
// @ts-ignore
75
import { Component } from 'laravel-mix/src/components/Component';
6+
import { EnvironmentPlugin } from 'webpack';
87

9-
import { parseAll } from './loader';
8+
import { parseAll, hasPhpTranslations } from './loader';
109

1110
class BeforeBuildPlugin {
1211
callback: Function;
@@ -35,6 +34,12 @@ mix.extend('i18n', class extends Component {
3534
ignored: /php_\w+\.json/,
3635
};
3736

37+
if (hasPhpTranslations(this.langPath)) {
38+
config.plugins.push(new EnvironmentPlugin({
39+
'LARAVEL_VUE_I18N_HAS_PHP': true,
40+
}));
41+
}
42+
3843
config.plugins.push(new BeforeBuildPlugin(() => {
3944
files = parseAll(this.langPath);
4045
}))

test/loader.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import { parseAll, parse } from '../src/loader';
3+
import { parseAll, parse, hasPhpTranslations } from '../src/loader';
44

55
beforeEach(() => {
66
const folderPath = __dirname + '/fixtures/lang/';
@@ -30,3 +30,9 @@ it('transforms .php lang to .json', () => {
3030

3131
expect(lang['failed']).toBe('These credentials do not match our records.');
3232
});
33+
34+
it('checks if there is .php translations', () => {
35+
const hasTranslations = hasPhpTranslations(__dirname + '/fixtures/lang/');
36+
37+
expect(hasTranslations).toBe(true);
38+
});

0 commit comments

Comments
 (0)