简体   繁体   English

Laravel-如何在app.php中设置语言环境

[英]Laravel - how to set the locale language in app.php

I try to set the local language when the user selects it, but it keeps going back to my default language when I refresh the page... 当用户选择本地语言时,我会尝试设置它,但是当我刷新页面时,它会一直回到我的默认语言...

'locale' => 'en',
'locales' => ['fr', 'en', 'es'],
'fallback_locale' => 'fr',

So when the user selects another language, here is what I do (Seems like I tried everything :) ). 因此,当用户选择另一种语言时,这就是我要做的事情(似乎我尝试了一切:))。

Any idea what else I can try? 知道我还能尝试什么吗?

public function setLang($lang){
        App::setLocale($lang);
        app()->setLocale($lang);
        Session::put('locale', $lang);
        setlocale(LC_ALL, $lang);
        session(['locale' => $lang]);
        return $lang;
    }

If I set 'locale' to 'fr' in app.php, then it will be 'fr' by default. 如果我在app.php中将“ locale”设置为“ fr”,则默认情况下将为“ fr”。

[EDIT] Here is the javascript (vueJS) part, I call setLang through an axios call: [编辑]这是javascript(vueJS)部分,我通过axios调用调用setLang:

                     <div class="locale-changer" aria-labelledby="dropdown07">
                        <select class="dropdown-menu-lang " aria-labelledby="dropdown07" v-model="$i18n.locale" @change="langChanged($i18n.locale)" >
                            <option class="dropdown-item-lang" :selected="$i18n.locale == lang" v-for="(lang, i) in langs" :key="`Lang${i}`" :value="lang">{{ lang }}</option>
                            <!-- <option class="dropdown-item-lang" selected="true" :key="`Lang${i}`" value="fr">fr</option> -->

                        </select>
                    </div>



       mounted(){
            this.$i18n.Locale = localStorage.Lang;
            console.log(" this.$i18n.Locale - " + this.$i18n.Locale); //it is OK here, the correct language which has been chosen by the user last time is displayed
        },
        methods: {
            langChanged(lang){
                localStorage.Lang=lang;
                axios.get('/setlang/'+lang).then( );
            }
        }

You have to show the codebase where you actually call the setLang function. 您必须在实际调用setLang函数的位置显示代码库。

Also you'll need to use some kind of persistence (maybe grab it from the URL or the Session) 另外,您还需要使用某种持久性(也许可以从URL或Session中获取它)

I found out how to do this! 我发现了该怎么做!

It was impossible to set $i18n.locale after the app was initialized. 初始化应用程序后,无法设置$ i18n.locale。 So I set it in app.js, before initializing everything. 因此,在初始化所有内容之前,我在app.js中进行了设置。

I don't find it clean but at least it works. 我觉得它不是干净的,但至少可以用。 If someone gets the same issue, here is app.js: 如果有人遇到相同的问题,这里是app.js:

import Vue from 'vue';
import Vuetify from 'vuetify';
import VueInternationalization from 'vue-i18n';
import Locale from './vue-i18n-locales.generated.js';
Vue.use(Vuetify);
Vue.use(VueInternationalization);

require('./bootstrap');

window.Vue = require('vue');

var App = Vue.component('app', require('./App.vue').default, {
    name: 'app'
}); 

var Home = Vue.component('home', require('./components/home/HomeComponent.vue').default); 
//and all other components


let lang=localStorage.Lang;
if(lang==null){ //I really don't like this part though... if tehre is nothing in the local storage, I need to get the language in the DB
    axios.get('/getlang/').then(
        response => {
            lang =  response.data;
            init();
        }
    );
}
else{
    init();
}



function init(){
    let i18n = new VueInternationalization({
        locale: lang,
        messages: Locale
    });

    const app = new Vue({
        el: '#app',
        i18n,
        components:{app:App, home:Home} //and all other omponents
    });
  }

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

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