kazupon / vue-i18n

:globe_with_meridians: Internationalization plugin for Vue.js
https://kazupon.github.io/vue-i18n/
MIT License
7.28k stars 861 forks source link

how solve different imgs in vue-i18n #248

Open zhangruinian opened 7 years ago

zhangruinian commented 7 years ago

vue & vue-i18n version

ex: 2.1.0, 5.0 I want use different imgs in diffent imgs , can I use with vue-18in?

What is actually happening?

kazupon commented 7 years ago

Please, give us detailed explanation.

zhangruinian commented 7 years ago

For example, I have x-en.png, x-ch.png and x-ja.png. How can I use it like the different strings in vue-i18n.

claudiocleberson commented 7 years ago

Hi guys,

My workaround for that issue was to use the v-bind, and so far it works.

Template: img v-bind:src="$t('images.homebg')"

Locale.js: export default { 'en-US': { messages: { hello: 'Hello' }, images: { imagebg: '../../static/images/phone01.png' } }, 'pt-BR': { messages: { hello: 'Ola' }, images: { imagebg: '../../static/images/screen01.png' } }
}

main.js: const i18n = new VueI18n({ locale: 'pt-BR', messages, images });

new Vue({ el: '#app', router, store, i18n, });

zhangruinian commented 7 years ago

@claudiocleberson thanks, it is a good way. I find an another way .

    <img :src="imgCloud[$i18n.locale]" alt="">
    import imgCloudzh from '@/assets/imgs/sass-cloud.png'
    import imgClouden from '@/assets/imgs/sass-clouden.png'
    data () {
            return {
                imgCloud: {
                    zh: imgCloudzh,
                    en: imgClouden
                }
            }
        },
WuChenDi commented 2 years ago

@claudiocleberson thanks, it is a good way. I find an another way .

    <img :src="imgCloud[$i18n.locale]" alt="">
    import imgCloudzh from '@/assets/imgs/sass-cloud.png'
    import imgClouden from '@/assets/imgs/sass-clouden.png'
    data () {
            return {
                imgCloud: {
                    zh: imgCloudzh,
                    en: imgClouden
                }
            }
        },

Thank you, this is effective. I would like to ask if there is a more convenient method other than this.

gonnaeat commented 1 year ago

Another way is to add your images in the /public (root) folder. This will avoid the supplement ID to the image file during the build. If you add a "staticimages" folder to the /public folder, with a folder per language to get the following result: <img src="/staticimages/zh/myimage.jpg'" /> <img src="/staticimages/en/myimage.jpg'" /> or dynamically: <img :src="'/staticimages/'+$i18n.locale+'/myimage.jpg'" /> In that case, you don't have to declare or import all the images in you components. Hope this helps.

WuChenDi commented 1 year ago

@gonnaeat

Thank you for your reply. We have decided to develop a loader that detects if there are multiple language versions of image resources and handle the replacement of these resources.