How to set locale by controller laravel - php

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

Related

Laravel get route by locale

Currently, I've got the following route in my web app.
Route::get(__('routes.account.dogs'),
[DogController::class, 'index'])->name('dog.index')->middleware('auth');
I specify the URL translation for the route in two different language files.
return [
//Account
'account' => [
'dogs' => 'honden',
]
];
When I send a queued email with this link, I want to translate the link on the user's locale (saved in the database). So I want to get this route based on the user's locale, but app()->setLocale($user->locale); cannot be used because it's a queued job. How can I fix this?
In a perfect world it would be something like:
route('dog.index', [], $user->locale);
laravel __() helper function also accepts the optional 3rd argument $locale for setting the locale to translate.
So you could use the $user->locale in the helper as follows.
url(__('account.dogs', [], $user->locale));
As #Vincent Decaux suggested, you need a library to make your life easier. You could use the one they use or the one I use arcanedev/localization.
Once you set a middleware, everything will be taken care of. But I am concerned regarding your comment:
Yes indeed! but then I still need to do something with the domain. For example .de or .com
Why not use the current domain (.com?) and simply add a directory /en or whatever locale you need instead? It seems inconvenient to me to change the entire domain.
When you use Mail::to(), you can use locale() to choose the translation
Mail::to($user->email)
->locale($user->locale)
->queue(new YourEmail());
https://laravel.com/docs/9.x/mail#localizing-mailables

Laravel Localization on Laravel 6.9

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.

Laravel localization with url

I am developing a multi-language web app using Laravel framework. So in this app, I have a special condition to do multi-language feature as below.
a user can select from some flags and change the language manually.
It changes his URL to /{lang} .. so, for example, webapp.com/cs - so he will see everything that in Czech language. webapp.com/en - see everything in English.
Chosen localization should be persistent so would not disappear when user change page or something - it should always be in the URL.
I created Route to set locale in session as follows.
Route::get('/{locale}', function ($locale) {
session()->put('locale', $locale);
return back();
});
And created middleware and added it to $middlewareGroups in the http\Kernel as well.
Below is my middleware.
public function handle($request, Closure $next)
{
if (session()->has('locale')) {
app()->setLocale(session('locale'));
}
return $next($request);
}
Localization is going well and it gives correct translations and everything. But what I need is to show in the URL what is the language is. as example webapp.com/cs,webapp.com/en. It would be great if anyone can help me with this problem.
Thanks.
You can try this,
In your every route you can append current local language like this
Route::get('/home/'.app()->getLocale(),'HomeController#index');
and one more thing you should see this
Laravel app()->getLocale() inside routes always print the default "en"
Hope this helps :)

Laravel; how make setLocale permanent?

I've my home route
Route::get('/', 'HomeController#index')->name('home');
And a specific route to change language
Route::get('/setLocale/{locale}', 'HomeController#setLocale')->name('setLocale');
In HomeController->setLocale($locale) I check if $locale is a valid locale, then simply do
\App::setLocale($locale);
Then redirect to home.
Here, in HomeController->index() I verify locale using
$locale = \App::getLocale();
The problem is that after user CHANGES the locale, the app set the new locale, redirect, but the locale detected is still the DEFAULT locale, not the new one setup by the user.
How / where / when can I make persistent the change to app locale?
I thinked Laravel was setting a locale cookies or something when using setLocale and re-reading it when using getLocale but now I think it's not this the way Laravel works.
I ask, again: how can I set app locale so that is preserved after page change?
I did that by using a middleware. Here's my code:
LanguageMiddleware:
public function handle($request, Closure $next)
{
if(session()->has('locale'))
app()->setLocale(session('locale'));
else
app()->setLocale(config('app.locale'));
return $next($request);
}
remember to register your middleware :)
The users are able to change the language, with a simple GET-Route:
Route::get('/lang/{key}', function ($key) {
session()->put('locale', $key);
return redirect()->back();
});
Hopefully that helps you :)
Apparently, there are some changes in Laravel 7.0!
If you are using the code above, please change from a BeforeMiddleware to a AfterMiddleware! (see https://laravel.com/docs/7.x/middleware)
If you don't do this, the value of session('locale') will always be null!
setlocale only works for the current request. If you want the value to be remembered, you will have to set a cookie manualy.
I recommend using a middleware to do this.

Laravel 5.3 How to avoid locale before prefix in api routes in multilanguage app?

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

Categories