1

I'm using this package - https://github.com/mcamara/laravel-localization for localization. And it works fine when I manually change the url to /en, /de etc... But is it possible to change url when (html) option is selected. For example I want to add this to my navigation bar:

<form action = "somePage" method = "POST">
    <select>
        <option value = "en">English</option>
        <option value = "de">Deutsch</option>
                       ...
    </select>
</form>

Is there an option in Laravel to add "value" to the string?

EDIT

With this code, it changes URL, but I want just to replace /de and /en. For example, it changes http://localhost:8000/en/home to http://localhost:8000/en ... I want it to stay on the same page, just to change the language. Example: http://localhost:8000/en/profile to http://localhost/de/profile

<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<option value="">Select...</option>
<option value={{ url('/en') }}>English</option>
<option value="{{ url('/de') }}">Deutsch</option>

harunB10
  • 4,823
  • 15
  • 63
  • 107
  • Do a redirect on 'click' to the correct url (having the correct locale in front). – DevK Jan 19 '17 at 16:36
  • But how to keep that URL prefix when something else is clicked? For example when user clicks my profile, and previously he/she chose German language... In this package it is defined in middleware that it automatically adds url prefix according to 'locale' value in config/app.php – harunB10 Jan 19 '17 at 16:39
  • 1
    I'm using the same package in one of my projects. Do you have your routes set up like this: http://pastebin.com/caK3GSpm This is copied from my project and it's working fine for me. If I'm on /en all links (using route()) are correct and language is too. – DevK Jan 19 '17 at 16:43
  • Thx @devk. It works now. The prefix is not changing. Still, have to figure out how to add url prefix with select option. – harunB10 Jan 19 '17 at 16:49

2 Answers2

2

Do something like this.

<form id="selectbox" name="" >
    <select onchange="javascript:location.href = this.value;">
        <option value={{ url('/de') }}>Deutsch</option>
        <option value={{ url('/en') }}>English</option>
    </select>
</form>

I refered this and this post for my answer.

Gayan
  • 3,614
  • 1
  • 27
  • 34
0

It works now. Thx everybody...

<script type="text/javascript">
    $('#changeLang').change(function (e) {
        var locAppend = $(this).find('option:selected').val(),
            locSnip = window.location.href.split('/')[4];
        console.log(window.location.href = locAppend + "/" + locSnip)
        //window.location.href = locAppend + locSnip;
    });
</script>
Gayan
  • 3,614
  • 1
  • 27
  • 34
harunB10
  • 4,823
  • 15
  • 63
  • 107