Skip to content

Commit 84b9329

Browse files
authored
feat: Add vite plugin to get the php translation files. (#37)
* feat: Add Vite Plugin. * fix: Export Properly. * fix: Try Transform * fix: improve `process.env` detection. * fix: The `env` vars are only available on `config` method. * fix: Get `env` vars. * fix: set target to 'es2019' * fix: Remove strict. * fix: try `es5`. * fix: Add different `tsconfig` for the plugins and the client side. * fix: Change build to `ESNext`. * feat: Add Cleanup on exit. * fix: Improve `process.env` detection. * cleanup * debug * wip * test: fix test suite * docs: Improve documentation. * fix: Keep `full-reload` for now. * style: fixes.
1 parent fd32fb3 commit 84b9329

File tree

12 files changed

+1374
-1174
lines changed

12 files changed

+1374
-1174
lines changed

README.md

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</p>
1616

1717
## Installation
18+
1819
With [npm](https://www.npmjs.com):
1920
```sh
2021
npm i laravel-vue-i18n
@@ -29,31 +30,85 @@ yarn add laravel-vue-i18n
2930

3031
> If you want to see a screencast on how to setup check out this video: [How to use Laravel Vue i18n plugin](https://www.youtube.com/watch?v=ONRo8-i5Qsk).
3132
33+
### With Vite
34+
3235
```js
3336
import { createApp } from 'vue'
3437
import { i18nVue } from 'laravel-vue-i18n'
3538

3639
createApp()
3740
.use(i18nVue, {
38-
resolve: lang => import(`../../lang/${lang}.json`),
41+
resolve: async lang => {
42+
const langs = import.meta.glob('../../lang/*.json');
43+
return await langs[`../../lang/${lang}.json`]();
44+
}
3945
})
4046
.mount('#app');
4147
```
4248

43-
### With `vite`
49+
#### SSR (Server Side Rendering)
4450

45-
The `resolve` method will need to be:
51+
For Server Side Rendering the resolve method should not receive a `Promise` and instead take advantage of the `globEager` method like this:
4652

4753
```js
48-
resolve: async lang => {
49-
const langs = import.meta.glob('../../lang/*.json');
50-
return await langs[`../../lang/${lang}.json`]();
51-
}
52-
````
54+
.use(i18nVue, {
55+
lang: 'pt',
56+
resolve: lang => {
57+
const langs = import.meta.globEager(`../../lang/${lang}.json`);
58+
return langs[`../../lang/${lang}.json`].default;
59+
},
60+
})
61+
```
62+
63+
#### PHP Translations Available on Vue
64+
65+
In order to load `php` translations, you can use this `Vite` plugin.
66+
67+
```js
68+
// vite.config.js
69+
import i18n from 'laravel-vue-i18n/vite';
70+
71+
export default defineConfig({
72+
plugins: [
73+
laravel([
74+
'resources/css/app.css'
75+
'resources/js/app.js',
76+
]),
77+
vue(),
78+
79+
// Laravel >= 9
80+
i18n(),
81+
82+
// Laravel < 9, since the lang folder is inside the resources folder
83+
// you will need to pass as parameter:
84+
// i18('resources/lang'),
85+
],
86+
});
87+
```
88+
89+
> During the `npm run dev` execution time, the plugin will create some files like this `php_{lang}.json` on your lang folder.
90+
> And to avoid that to be commited to your code base, I suggest to your `.gitignore` this like:
5391
54-
### With SSR
92+
```bash
93+
lang/php_*.json
94+
```
5595

56-
The `resolve` method can receive a `require` instead of a `Promise`:
96+
### With Webpack / Laravel Mix
97+
98+
```js
99+
import { createApp } from 'vue'
100+
import { i18nVue } from 'laravel-vue-i18n'
101+
102+
createApp()
103+
.use(i18nVue, {
104+
resolve: lang => import(`../../lang/${lang}.json`),
105+
})
106+
.mount('#app');
107+
```
108+
109+
#### SSR (Server Side Rendering)
110+
111+
For Server Side Rendering the resolve method should receive a `require` instead of a `Promise`:
57112

58113
```js
59114
.use(i18nVue, {
@@ -62,7 +117,7 @@ The `resolve` method can receive a `require` instead of a `Promise`:
62117
})
63118
````
64119

65-
### Laravel Mix Plugin
120+
#### PHP Translations Available on Vue
66121

67122
In order to load `php` translations, you can use this `Mix` plugin.
68123

@@ -74,8 +129,9 @@ require('laravel-vue-i18n/mix');
74129
mix.i18n();
75130
76131
// Laravel < 9, since the lang folder is inside the resources folder
77-
// you will need to pass as parameter.
78-
mix.i18n('resources/lang');
132+
// you will need to pass as parameter:
133+
134+
// mix.i18n('resources/lang');
79135
```
80136

81137
### Usage

babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
plugins: ['@vue/babel-plugin-jsx'],
2+
plugins: ['babel-plugin-transform-vite-meta-env', '@vue/babel-plugin-jsx'],
33
presets: [
44
['@babel/preset-env',
55
{

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = {
44
preset: 'ts-jest',
55
globals: {
66
'ts-jest': {
7-
babelConfig: true
7+
tsconfig: './tsconfig.plugins.json',
8+
babelConfig: './babel.config.js',
89
}
910
},
1011
testEnvironment: 'jsdom',

0 commit comments

Comments
 (0)