I'm using Laravel localization to provide two different languages. I've got all the path stuff set up, and mydomain.com/en/bla delivers English and stores the 'en' session variable, and mydomain.com/he/bla delivers Hebrew and stores the 'he' session variable. However, I can't figure out a decent way to provide a language-switching link. How would this work?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
This question still comes in Google search, so here's the answer if you're using Laravel 4 or 5, and mcamara/laravellocalization.
NOTE that this example shows flags (in public/img/flags/{{locale}}.gif), and to use it you will need a bit of .css, but you can modify it to display the text if you want...
FYI. The mcamara/laravellocalization documentation has examples and a LOT of helpers, so look through the documentation on github. (https://github.com/mcamara/laravel-localization)
I've been doing it like this:
Source: http://forumsarchive.laravel.io/viewtopic.php?pid=35185#p35185
You could have a Route to hand language change, for example:
Route::get('translate/(:any)', 'translator@set');
Then in the
set
action in thetranslator
controller could alter the session, depending on the language code passed via the URL.You could also alter the configuration setting by using
Config::set('application.language', $url_variable');
Controller Example - translate.php
Try use Session's. Somthing like this:
Controller:
In Route.php:
I've solved my problem by adding this to the before filter in routes.php:
What I'm doing consists of two steps: I'm creating a languages table which consists of these fields:
id | name | slug
which hold the data im gonna need for the languages for example
1 | greek | gr
2 | english | en
3 | deutch | de
The Language model I use in the code below refers to that table.
So, in my routes.php I have something like:
And then I can write me routes using the requested slug as a prefix...
This works but this code will ask the database for the languages everytime the route changes in my app. I don't know how I could, and if I should, figure out a mechanism that detects if the route changes to another language.
Hope that helped.