Laravel - DataTable Localization - laravel

Morning,
I'm building a multilanguage site with Laravel.
Changing of display language is OK, when user select language (fr or en).
But only DataTable options are not translated (search, previous, ...).
I can translate it into Datable {language:..} but this method is static.
Thks for your help !

I did the same thing for the datatable,
You can use laravel lang function and blade syntax for mixing php into script ,
to do that,You can use laravel lang function for language property for dynamic localization
Here is sample for:
$('#example').DataTable( {
"language": {
"lengthMenu":"{!!__('dataTable.menu')!!}",
"zeroRecords": "{!!__('dataTable.zeroRecord')!!}",
....
}
} );
And in resources/lang folder in your laravel project, create correspond php file array for your desired languages
For more information about laravel localization check this

Related

Voyager Addition field options

I'm using Voyager Laravel to build my app, but I'm having some issues regarding the form fields, in my Bread column of "transmission" I have included in the JSON form this:
column transmission:
{
"options": {
"option1": "Automática",
"option2": "Manual"
}
}
In the admin is displaying well the information, the problem is when I call it in the frontend for example:
$car->transmission -> outputs -> "option1"
I'm trying to find in the documentation regarding calling only the text/label of the option1 that is correct, but no information I'm finding.
Anybody the uses Voyager had the same issue?
Not at all. That's up to you.
But you can use the key (option1, option2, ...) and rename it to something you can use in your frontend.

How to switch view by locale in Laravel 6?

I'd like to use different views for different locale in Laravel 6 instead of translate phrase by phrase.
Is there some mechanism or package that allows to do it?
Otherwhise how could I write my views and controllers such that it would be done cleanly and relaiably?
Thanks in advance
Order your views into language maps (e.g. the English view for "test" as en\test.blade.php, the French view fr\test.blade.php)
Then use a locale variable from your route to determine the view that is returned.
For example: Route::get('{locale}/test', 'SomeController#test');
public function test($locale)
{
return view($locale.'test');
}

Routes laravel translation

After much testing and searching, I can not find the solution.
My problem is that I have, for example the route '/services', '/servicios' in spanish.
If my website is in english, all texts are correctly shown in english, same in spanish. But the problem is with the text links, only appear in default locale.
In routes/web.php:
Route::get(trans('rutas.servicios'), 'ServiceController#index')->name('servicios')
File lang/es/rutas.php:
return [
'servicios' => '/servicios',
];
File lang/en/rutas.php:
return [
'servicios' => '/services',
];
and link html:
#lang('menus.servicios')
My goal is to use translated urls without using laravel prefix. That is to say:
https://www.myweb.com/services ~ english
https://www.myweb.com/servicios ~ spanish
share the same Route controller.
Any help? Lot of thanks for your time!
EDIT WITH SOLUTION:
I discovered that laravel performs route mapping before selecting the language. So what I did was to set the language before this, but the session and cookies variables load after this. But I finally discovered that the cached variables satisfied my needs.
At the top of function map() of RouteServiceProvider.php:
App::setLocale(Cache::get('locale'));
To store the selected locale:
Cache::put('locale', $locale, $minutes);

Consistency in Laravel localization

I have a laravel appication that I am building. I am building a multi language application which would have french and spanish and its url would be
www.example.com/fr/route/slug for french
www.example.com/es/route/slug for spanish
www.example.com/route/slug for english which is the main one
But I am very confused as to how I should go about maintaining the consistency from one url to another within the same language i.e. when I click on a link which is under french, what should be returned should still be french. e.g:
from
www.example.com/fr/route/slug1 to
www.example.com/fr/route/another_path to
www.example.com/fr/route/final_path which would be maintaing same language path
www.example.com/es/route/slug1 to
www.example.com/es/route/another_path to
www.example.com/es/route/final_path for spanish
www.example.com/route/slug1 to
www.example.com/route/another_path to
www.example.com/route/final_path for english
Also when I change from e.g. french to english the page must be consistent e.g.
www.example.com/fr/route/slug1 to
www.example.com/fr/route/another_path in french to english
www.example.com/route/another_path
What are the steps I should take? Any help would be appreciated. Thanks in advance.
To maintain this you can follow the below steps :
At change point of language at your website ( eg. Change language from dropdown ) , store selected value in session and then your page should be reload
Then you have to create one middelware to manage the localization.
You just have to simply write few line of code in handle method of the Middleware.
$lang = Session::get("language");
App::setLocale($lang);
Thats it, this will manage your consistency.
You should add this group for all your route
Route::group(['middlewareGroups' => 'web', 'prefix' => '{local}'], function () {
and then make middleware where you catch the local and then :
public function handle($request, Closure $next)
{
App::setLocale($locale);
}
And apply it by adding it in your $middleware array located in the Http/kernel.php file

Codeigniter calendar language problem

I'm trying to get the calendar in CodeIgniter to print month names in Swedish.
In config.php I have this line:
$config ['language'] = 'Swedish';
In autoload.php I have this line:
$autoload ['language'] = array ('calendar');
In my controller:
$this->lang->load('calendar', 'swedish');
So here I am trying to print the date:
<? php echo date ('d M Y', $row['pubDate']);?>
The result is as: 23 May 2011
Month names are printed in English, which is wrong, I want it in Swedish. I have calendar_lang.php in Swedish.
Any advice on how I solve this?
Codeigniter lang files are decided to "translate" only codeigniter functions, methods etc.
They do not affect default php function, like that one you are using (date).
You should look at this page and follow the links on that page.

Resources