As in the laravel documentation I have configured my application as below to change languages.
Created sub-directories inside the resources lang folder with each directory contain a messages.php file with relevant languages. Like below
/resources
/lang
/si
messages.php
/ta
messages.php
Then made a get route as in the documentation and it's same as in the documentation
Route::get('change-lang/{locale}', function ($locale) {
if (! in_array($locale, ['en', 'si', 'ta'])) {
abort(400);
}
(dd(App::getLocale()); // this gives me the default lang en
App::setLocale($locale);
// (dd(App::getLocale()); // this gives me the setted language
});
This is the first time I'm using this setLocale() method. Usually, I used to change the language via passing additional parameters with each route and when I did it it's working fine. But I want to make new application with setLocale() method since it is very easy to woking. So I have done everything according to the documentation but still, the language doesn't get changed.
Can anybody please help me to fix this.
Related
I want to set locale for change language but my project is not website but it is a Facebook bot which developed through base controller, I want Example thank.
The default language for your application is stored in the config/app.php configuration file. Of course, you may modify this value to suit the needs of your application. You may also change the active language at runtime using the setLocale method on the App facade:
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
//
});
see more in https://laravel.com/docs/5.7/localization#introduction
I'm using Laravel 5.6 and trying to sort out what's the best method to translate a route.
I use top level domain to sort the locale so
mydomain.pt -> locale pt
mydomain.es -> locale es
mydomain.com -> locale en
I created a middleware to handle this
class Language
{
public function handle($request, Closure $next)
{
$url_array = explode('.', parse_url($request->url(), PHP_URL_HOST));
$top_domain = end($url_array);
if(!empty($top_domain)){
App::setLocale($top_domain);
}
}
}
This is working fine and the translated strings within the respective files work when i switch domain.
However i want to be able, for SEO purposes, to have links like this
mydomain.pt/dynamic/perto-de-mim
mydomain.es/dynamic/cerca-de-mi
mydomain.com/dynamic/near-me
What's the best approach in laravel to get my route
Route::get('{category}/near-me/', 'ServicesController#nearMe');
To work with all the examples above?
Thank you
Laravel allow you to provide translation string :
/resources
/lang
/en
routes.json
/es
routes.json
In resources/lang/es/routes.json :
{
"near-me": "cerca-de-mi"
}
And your route become :
Route::get('{category}/' . __('routes.near-me') . '/', 'ServicesController#nearMe');
You could see here the use of the Laravel helper method __() to retrieve the translation from the xx/routes.json files.
I planed to create Departmental Store Laravel Web Application in Tamil Language Not Using English. How to do it?
You can create web apps using laravel's Localization.
For that, you need to create language strings. Language strings are stored in files within the resources/lang directory.
You need to create locale:
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
//
});
Then create file within that directory and save it messages.php name like this:
<?php
return [
'welcome' => 'Welcome to our application'
];
?>
Then you need to use this as in any blade file like:
#lang('messages.welcome')
Also, please read the laravel docs from here:
enter link description here
In your encoding defined at DB and front end use - UTF-8 encoding which supports most of the international languages
I've built a multilanguage app in Laravel 5.3 and I have a bunch of api routes that return me some resources where the url is something like this http://app-domain.com/api/resource when I'm on my default language.
The problem presents itself when I'm not in the default language. When the app tries to call the api it sends a request to http://app-domain.com/locale/api/resource which returns nothing since it's not the correct path.
My implementation is very simple, just basic routing in my routes/api.php
Route::group([
'prefix' => 'api'
],function() {
Route::get('resource', 'ApiController#getResource')->name('get-resource');
});
Then I'm using jquery ajax object to call it
$.get( "api/resource", function( data ) {
//run somecode
});
How can I address this problem and have my api routes resolving correctly no matter the language? Is there some params that I can set in the routes to prevent this? I've been looking in the documentation but found nothing relevant.
P.S.: I'm using Mcmanamara Laravel Localization
IMHO for setting locale its better if you use a query-string: i.e.:
http://app-domain.com/api/resource?locale=en
For two, valid, reasons:
1) The locale in the querystring could also be non existing, so you can fallback to default locale.
2) Having a locale parameter in a route could conflict with other routes parameters generating a lot of confusion
I am trying to test the routing mechanism of my app (which is a Laravel PHP App) that was based on the documentation (http://laravel.com/docs/routing#route-prefixing). The case is: We are trying to handle the app localisation based on the route, for example:
www.example.com/en/something -> Should use EN as language,
www.example.com/es/something -> Should use ES as language,
www.example.com/de/something -> Should use DE as language.
We accomplished the behaviour described before by using prefixes and it works perfect. The thing is that I am not able to write tests to test this behaviour. I did a little bit of research, and I found this third party library (https://github.com/mcamara/laravel-localization/blob/master/tests/LocalizerTests.php), that uses a similar approach, and I think it might help you understand which is the situation.
If you take a look in there, he is not testing the scenario that I want to test (I mean, with the locale code on the path).Btw: I've already turned on the filters with Route::enableFilters() and it does not work :(
Thanks a lot
If you use route inside the group.
Route::group(array('domain' => {account}.'.$domain), function()
{
Route::get('myproduct/en/list', 'HomeController#showWelcome');
});
then try this.
public function testBasicExample() {
$crawler = $this->client->request('GET', 'http://subdomain.domain.local/myproduct/en/list');
$this->assertTrue($this->client->getResponse()->isOk());
}