hasanuzzamanbe / wp-boilerplate-vue-with-vite

This is a WordPress boilerplate plugin using vue 3, build with vite. Also tailwind setup available.
https://wpminers.com/make-wordpress-plugin-using-vue-with-vite-build/
77 stars 23 forks source link

Support for translations possible? (wp.i18n) #5

Closed camya closed 1 year ago

camya commented 1 year ago

Hi,

I tried to translate the Vue Frontend, but haven't find a good way to make this possible.

Have you already worked with i18 in the boilerplate?

Best

hasanuzzamanbe commented 1 year ago

Hi @camya , yes, you can do that easily. It's not in the boilerplate code yet. I will add it today. Thanks for letting me know about it.

You can do it by following this:

  1. Localize i18 array with translatable string like this. image

  2. Set a global method in your mixin as $t

                $t(str) {
                    let translateString = WPWVTAdmin.i18n[str];
                    if (translateString) {
                        return translateString;
                    }
                    return str;
                },
  3. Use it from your Vue or js $t('My Text')

Thanks

hasanuzzamanbe commented 1 year ago

Translation Support adds. You can check the code in the latest commit. https://github.com/hasanuzzamanbe/wp-boilerplate-vue-with-vite/commit/c02d98436107caf1560e8336479c788f6af77ebd#diff-b9f7743dad11fb515ee3f862cc1183f6a854846a210fadad57e207e64ec7cbb0

//PHP code Just localize the string like this

 add_filter('WPWVT/frontend_translatable_strings', function($translatable){
  $translatable['world'] = __('World', 'wp-boilerplate-vue-with-vite');
   return $translatable;
}, 10, 1);

//Vue Js code <h1> {{ $t('world') }} </h1>

camya commented 1 year ago

Great. Thank you for the quick update. This looks very good. Created translations with the Loco Translate plugin, and it works fine.

hasanuzzamanbe commented 1 year ago

loved to hear that, You are welcome.