I can recommend this package: https://github.com/mcamara/laravel-localization
Multi-Language Routes and url
Explain the best way for mutli-language routes and url
Looks very good. Will all languages be visible in search engines?
I do not recommend this laravel-localization package, because it doesn't remove locale slug from URI.
Lets say you want to have multilingual setup, where default locale does not havea slug in URI.
- http://site.com/about - default locale english
- http://site.com/fr/about - french locale
Now if you need to use Request::segment(1) you will get different results and that is a very bad thing!
Best option would be to remove the slug from URI in your applications index.php file and define a constant which you will use in config/app.php. Laravel looks for the URI in $_SERVER['REQUEST_URI'].
That way it will work in every application!
With an eye on SEO, I'd recommend to prepend the locale as a subdomain (en.site.com, fr.site.com, etc.). This will also help avoid the slug/segment problem. Of course, it will add a few issues in return that need resolving, such as getting/setting the subdomain for setting/getting the locale. Re SEO, here's an interesting article from Google that discusses the different approaches, however in German only as far as I know: http://googlewebmastercentral-de.blogspot.ch/2010/04/websites-fur-mehrere-regionen.html
Yeah don't use the same URL with different languages.
Google doesn't know if you remove locale slug from $_SERVER['REQUEST_URI'] variable as it is still present in URL. Only laravel does not see it!
We know keeeeeeeeevitaja
I'm getting confused.
Google has this: https://support.google.com/webmasters/answer/182192?hl=en#1
SO has this: http://stackoverflow.com/questions/19249159/best-practice-multi-language-website
This also looks like a solution: https://coderwall.com/p/czumyq/easy-i18n-with-language-prefix-and-language-related-route-in-laravel-4
The macamara package is sort of broken. Lots of issues ...
If my understanding is correct, { domain/locale/query } should be ok for SEO However, I don't want to show the { locale } when the default locale is same as the request_uri.
What is the recommend method? Any packages or code examples?
I am still working out what to do with the DB data.
@andy ,
Hiding the locale segment from URI for default locales is tricky at best using some kind of package like LaravelLocalization.
Consider the following case:
- site.com/about -- default locale en
- site.com/fr/about
What if your app has to work with URI segments? Current example has the about in 2 different URI segment positions.
Request::segment(1)
Request::segment(2)
So working with segments is literally impossible. As i said before:
IMHO the best option would be a little hack. Modify the index.php to remove the locale segment from the $_SERVER['REQUEST_URI'] and define a constant which will be used in the locale config file.
And you have created pretty much everything the LaravelLocalization is trying to achieve. Only difference is, it does not break anything and works with every app. Of course you need to write logic for displaying links and locale switcher.
And you also have zero SEO issues!
I don't agree with @keevitaja because, as @rspahni mentions, the url needs to specify the language for SEO purposes.
The segment issue is a technical complication indeed, but that should not make you choose a solution that affects your SEO and, therefore, your company's success.
I am thinking about making the default url localised, like so:
site.com -> redirects to site.com/en site.com/en/about site.com/fr/about
Hi,
best solution after week research:
hpolthof/laravel-translations-db
Bookmarked. I'll look into it. Do you care to share why you landed on this package?
@Trond Just manages the lang files? Doesn't do URI lang stuff...
I think we should use the URI and define our language on the subdomain, but i don't have idea how to do that.
My second option could be hide the language from the URI, and define it only on a configuration or a session, but never putting the language on the URI as a segment.
I use https://github.com/mcamara/laravel-localization in front-end and its works perfect, BUT i dont know how to do use it in back-end like b2b site:
www.example.com/b2b/en/dashboard www.example.com/b2b/fr/tableau
At the moment i got: www.example.com/en/b2b/
Just put 2 distinct routes group and prefix the route like you want
Route::group(['prefix' => ' LaravelLocalization::setLocale()], function() {
// front routes
});
Route::group(['prefix' => 'b2b/' . LaravelLocalization::setLocale()], function() {
// b2b routes
});
goopil, it gives me an error:
NotFoundHttpException in RouteCollection.php line 161:
@bulldoze He had a typo in the first Route line, Try this:
Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
// front routes
});
Route::group(['prefix' => 'b2b/' . LaravelLocalization::setLocale()], function() {
// b2b routes
});
what about a localization using sessions? is it a bad thing also ?
Basically you want to keep the localisation in the URL, for SEO purposes.. So that Google can index different languages. Remember hreflang tags :-) (Google it; Google has some examples)
I wanted my uri to actually be translated without using any en/es/pl in it, so I do somthing like this:
in routing:
Route::group(['middleware' => ['localization:en']], function () {
// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('en-login');
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('en-register');
...
});
Route::group(['middleware' => ['localization:pl']], function () {
Route::get('/', function () {
return view('welcome');
})->name('pl-home');
Route::get('zaloguj', 'Auth\LoginController@showLoginForm')->name('pl-login');
Route::get('zarejestruj', 'Auth\RegisterController@showRegistrationForm')->name('pl-register');
...
});
Ofcourse the localization middleware is setting locale base on the parameter. And you have to create routes in all languages with names like: LOCALE-ROUTE_NAME
And than all I do in my views is to create all links like this:
route(App::getLocale() . "-login");
Do you think it's good way to do it or maybe I can improve it somehow?
From what I can see, @keevitaja has the best solution.
- Modify index.php to extract locale if present.
So http://mysite.com/gr/hello becomes http://mysite.com/hello
Store the locale if specified. If not specified, store the default.
App::setLocale($locale);
-
Use laravel localisation for generating output, or whatever form you want.
-
Create your own helper functions to replace any you use for routing. This will insert the locale if required.
Is there a better way?
Hello, maybe help. Code in routes/web.php :
Route::group([
'prefix' => \App\Helpers\LocalizationHelper::prefix((string)(Request::segment(1)))
],function (){
Route::get(
'/',
function () {
return view("welcome");
}
);
Auth::routes(['verify' => true]);
Route::get("/confirm", "Auth\RegisterController@confirm")->name("confirm");
Route::post("/register", "Auth\RegisterController@singup");
Route::get('/home', 'HomeController@index')->name('home');
});
Code in your helper or something else:
/**
* Class Localization
*
* @package App\Http\Helpers
*/
class LocalizationHelper
{
const DEFAULT_LANGUAGE = 'en';
const RETURN_DEFAULT_LANGUAGE = '';
/**
* Add prefix for language
*
* @param string $segment
*
* @return string|null
*/
public static function prefix(string $segment)
{
if (false === empty($segment) && true === in_array($segment, config('app.available_locales'))) {
app()->setLocale($segment);
return $segment;
}
app()->setLocale(self::DEFAULT_LANGUAGE);
return self::RETURN_DEFAULT_LANGUAGE;
}
}
Where available_locales array as ['ru', 'en', ...]
Please or to participate in this conversation.