Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read properties of undefined (reading 'VITE_LARAVEL_VUE_I18N_HAS_PHP') #49

Closed
andrey-helldar opened this issue Jul 22, 2022 · 19 comments
Labels
bug Something isn't working more info needed

Comments

@andrey-helldar
Copy link
Contributor

andrey-helldar commented Jul 22, 2022

  • laravel-vue-i18n - 2.1.1
  • vite - 2.9.9
  • vuejs 3.2.25
  • node 16.16.0
  • without laravel
// .env

VITE_LARAVEL_VUE_I18N_HAS_PHP=false
// src/main.ts

import { createApp } from 'vue'
import { i18nVue } from 'laravel-vue-i18n'
import i18nOptions from '@/utils/lang'

import pinia from '@/utils/pinia'

createApp(App)
    .use(i18nVue, i18nOptions(pinia))
// src/utils/lang

import { Pinia } from 'pinia'
import { useSettingsStore } from '@/stores/settings'

export default (pinia: Pinia) => {
    const lang = useSettingsStore(pinia).locale

    const resolve = async (lang: string) => {
        const files = import.meta.glob('../../lang/*.json')

        return await files[`../../lang/${ lang }.json`]()
    }

    return { lang, resolve }
}

image

Result: not working

image

If we add output to the console, we see that the files are loaded:

const resolve = async (lang: string) => {
    const files = import.meta.glob('../../lang/*.json')

    console.log(files)
    console.log(
        import.meta.env.VITE_API_URL,
        import.meta.env.VITE_LARAVEL_VUE_I18N_HAS_PHP
    )

    return await files[`../../lang/${ lang }.json`]()
}

image

image

Without the VITE_LARAVEL_VUE_I18N_HAS_PHP key, I also get the error:

image

Version 1.4.4 worked perfectly.

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 22, 2022

Hey @andrey-helldar can you show me your vite.config.js file. Thanks.

@xiCO2k xiCO2k added bug Something isn't working more info needed labels Jul 22, 2022
@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 22, 2022

Fix for this error:

// node_modules/laravel-vue-i18n/dist/utils/has-php-translations.js:9
return import.meta.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP ? true : false;

// node_modules/.vite/deps/laravel-vue-i18n.js:348
return import_meta.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP ? true : false;

Result:

image

@andrey-helldar
Copy link
Contributor Author

My vite.config.ts is:

import vuetify from 'vite-plugin-vuetify'
import { defineConfig } from 'vite'
import { resolve } from 'path'

import vue from '@vitejs/plugin-vue'

export default defineConfig({
    resolve: {
        alias: {
            '@': resolve(__dirname, './src')
        }
    },
    plugins: [
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false
                }
            }
        }),
        vuetify({
            autoImport: true,
            styles: 'sass'
        })
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "./src/assets/styles/_variables";`
            }
        }
    }
})

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 22, 2022

I don't use Laravel in this project (this project only contains the frontend), so I didn't define the i18n plugin as the readme says it's for Lara.

image

Now I added a plugin call and everything worked out of the box: (no, see below)

// vite.config.ts

import { defineConfig } from 'vite'
import { resolve } from 'path'

import i18n from 'laravel-vue-i18n/dist/vite'
import vuetify from 'vite-plugin-vuetify'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
    resolve: {
        alias: {
            '@': resolve(__dirname, './src')
        }
    },
    plugins: [
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false
                }
            }
        }),
        vuetify({
            autoImport: true,
            styles: 'sass'
        }),
        i18n()
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "./src/assets/styles/_variables";`
            }
        }
    }
})

image

The comment confused me:

// Laravel >= 9
i18n(),

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 22, 2022

got it, that is an interesting use case, for sure will need to dig in more.

So you are just using the *.json lang files right?

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 22, 2022

got it, that is an interesting use case, for sure will need to dig in more.

So you are just using the *.json lang files right?

Yes

https://github.com/volunteers-crm/web/tree/laravel-vue-i18n-2.x

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 22, 2022

Can you try set on your package.json file this:

"laravel-vue-i18n": "github:xiCO2k/laravel-vue-i18n"

and then reinstall all the node dependences?

Thanks for helping me debug this,
Francisco

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 22, 2022

  1. Removed node_modules and package.lock
  2. Replace "laravel-vue-i18n": "^2.1.1" with "laravel-vue-i18n": "github:xiCO2k/laravel-vue-i18n"
  3. Run the npm i console command.
  4. Check in browser
  5. I see the error

image

image

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 22, 2022

really interesting case, will try to debug.

Thanks for all the help @andrey-helldar.

@andrey-helldar
Copy link
Contributor Author

Next steps:

  1. Go to node_modules/laravel-vue-i18n/dist/cjs/utils/has-php-translations.js:22
  2. Replace line with return typeof process.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP !== 'undefined' && process.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP ? true : false;
  3. Restart vite
  4. Go to node_modules/laravel-vue-i18n/dist/utils/has-php-translations.js:9
  5. Replace line with return typeof import.meta.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP !== 'undefined' && import.meta.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP ? true : false;
  6. Remove the node_modules/laravel-vue-i18n/dist/utils/has-php-translations.js.map file
  7. Restart vite
  8. Go to node_modules/.vite/deps/laravel-vue-i18n.js:348
  9. Replace line with return typeof import_meta.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP !== "undefined" && import_meta.env?.VITE_LARAVEL_VUE_I18N_HAS_PHP ? true : false;

Now, it's works

image

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 22, 2022

I was wrong. The problem is not solved :)

I forgot that I just changed files 😅

https://github.com/volunteers-crm/web/blob/laravel-vue-i18n-2.x/vite.config.ts

// vite.config.ts

import { defineConfig } from 'vite'
import { resolve } from 'path'

import i18n from 'laravel-vue-i18n/dist/vite'
import vuetify from 'vite-plugin-vuetify'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
    resolve: {
        alias: {
            '@': resolve(__dirname, './src')
        }
    },
    plugins: [
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false
                }
            }
        }),
        vuetify({
            autoImport: true,
            styles: 'sass'
        }),
        i18n()
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import "./src/assets/styles/_variables";`
            }
        }
    }
})

https://github.com/volunteers-crm/web/blob/laravel-vue-i18n-2.x/src/utils/lang.ts

// src/utils/lang.ts

import { Pinia } from 'pinia'
import { useSettingsStore } from '@/stores/settings'

export default (pinia: Pinia) => {
    const lang = useSettingsStore(pinia).locale

    const resolve = async (lang: string) => {
        const files = import.meta.glob('../../lang/*.json')

        return await files[`../../lang/${ lang }.json`]()
    }

    return { lang, resolve }
}

https://github.com/volunteers-crm/web/blob/laravel-vue-i18n-2.x/.env.example#L3

// .env

VITE_LARAVEL_VUE_I18N_HAS_PHP=false

And error)

image

PS: my package.json is https://github.com/volunteers-crm/web/blob/laravel-vue-i18n-2.x/package.json

andrey-helldar pushed a commit to andrey-helldar/laravel-vue-i18n that referenced this issue Jul 22, 2022
@andrey-helldar
Copy link
Contributor Author

image

It's a simple: import.meta.env is undefined

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 23, 2022

Thanks for the PR, can you try again, and let me know?

@andrey-helldar
Copy link
Contributor Author

Thanks for the PR, can you try again, and let me know?

If we add the VITE_LARAVEL_VUE_I18N_HAS_PHP variable key to the env file, then everything works. Everything works without it too.

image

Thank you!

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 23, 2022

By the way, so that the application does not load extra localizations during compilation, you can change the configuration:

before

import { createApp } from 'vue'
import { i18nVue } from 'laravel-vue-i18n'

createApp()
    .use(i18nVue, {
        resolve: async lang => {
            const langs = import.meta.glob('../../lang/*.json');
            return await langs[`../../lang/${lang}.json`]();
        }
    })
    .mount('#app');

after

import { createApp } from 'vue'
import { i18nVue } from 'laravel-vue-i18n'

createApp()
    .use(i18nVue, {
        resolve: lang => import(`../../lang/${lang}.json`)
    })
    .mount('#app');

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 24, 2022

So after the merge you got not problem?

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Jul 24, 2022

When I wrote the code, there were no problems, but yesterday I could not check because npm and yarn gave an error installing dependencies when deleting the node_modules folder. I'll try again today.

npm ERR! Cannot read properties of null (reading 'explain')

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Helldar\AppData\Local\npm-cache_logs\2022-07-24T10_18_35_446Z-debug-0.log

@andrey-helldar
Copy link
Contributor Author

So after the merge you got not problem?

It's works:

image

@xiCO2k
Copy link
Owner

xiCO2k commented Jul 24, 2022

Awesome, will tag it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working more info needed
Projects
None yet
Development

No branches or pull requests

2 participants