简体   繁体   English

如何在vue.js 2中使用lang类laravel?

[英]How can I use lang class laravel in vue.js 2?

My vue component like this : 我的Vue组件是这样的:

<template>
    <ul class="nav nav-tabs nav-tabs-bg">
        <li role="presentation" v-for="item in tabs">
            1. failed -> {{ item.name }} 2.success -> {{trans('purchase.payment.tab')}}
        </li>
    </ul>
</template>
<script>
    export default {
        data() {
            return {
                tabs: [
                    {name: "trans('purchase.payment.tab')"}
                ]
            }
        }
    }
</script>

My lang in laravel(resources/lang/en/purchase.php) like this : 我在laravel(resources / lang / en / purchase.php)中的lang像这样:

<?php
return [
    'payment' => [
        'tab' => 'Payment Status',
    ],
    ...
];

If the component vue executed, the result like this : 如果组件vue被执行,则结果如下:

  1. failed -> trans('purchase.payment.tab') 2.success -> Payment Status 失败-> trans('purchase.payment.tab')2.成功->付款状态

So, if trans used in data, it does not work 因此,如果在数据中使用反式,则无法正常工作

How can I solve this problem? 我怎么解决这个问题?

Is not possible to use a PHP helper inside JavaScript. 无法在JavaScript中使用PHP帮助器。 But, you can create an object of translations. 但是,您可以创建翻译对象。

In your AppServiceProvider (you can create a new one if you want): 在您的AppServiceProvider (如果需要,您可以创建一个新的):

// Don't forget to import the facade
use Illuminate\Support\Facades\Lang;


public function boot() {
    $translations = [
     'auth' => Lang::get('auth'),
     'pagination' => Lang::get('pagination'),
     'passwords' => Lang::get('passwords'),
     'validation' => Lang::get('validation'),
   ];

   view()->share('translations', json_encode($translations));

}

In your HTML (I suggest header) you can just call: 在您的HTML(我建议标头)中,您可以调用:

window.app = {
    translations: {!! $translations !!},
}

And to access using in JS, you can just do this, for example: 要在JS中使用,您可以执行以下操作,例如:

this.app.translations.auth.throttle // Too many login attempts. Please try again in :seconds seconds.

I use vue-i18n for that. 我为此使用vue-i18n That way you should make its own dictionary. 这样,您应该制作自己的字典。 I made a i18n/en.js file; 我制作了一个i18n/en.js文件;

module.exports = {
    login: {
      title: 'Login',
      loginButton: 'Login',
      emailInput: 'email',
      passwordInput: 'password',
    },
    Form: {
      title: 'Form',
    }
}

and a i18n/hu.js with the same variables in Hungarian. 以及在匈牙利语中具有相同变量的i18n/hu.js Then I made a i18n/map.js file: 然后我制作了一个i18n/map.js文件:

var en = require('./en.js');
var hu = require('./hu.js');

module.exports = {
  en,
  hu,
}

and finally, set it in vue.js , check my app.js file part: 最后,在vue.js设置,检查我的app.js文件部分:

require('./bootstrap'); // vue comes from here
import VueI18n from 'vue-i18n'
import dictionary from './i18n/map'

var localeTmp = document.documentElement.lang;
var locale = "hu";
if(localeTmp) {
  locale = localeTmp
}

const i18n = new VueI18n({
  locale: locale, // set locale
  dictionary, // set map of dictionary
})
....
const app = new Vue({
    el: 'app',
    i18n,
});

Its a very elegant way. 这是一种非常优雅的方式。

and how I use in component? 以及如何在组件中使用? simple: 简单:

  ....
  <md-input-container>
    <md-icon>person</md-icon>
    <label>{{ $t("loginForm.emailInput") }}</label>
    <md-input email name="email" required v-model="email" />
  </md-input-container>
  ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM