how to make language button switch on laravel [closed] - laravel

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i'm using blade template, all file content is in view and i need 2 language on my project, how the easiest way to make the button switcher language? can someone help me?
Thank you

You can create language links like:
<a href="{{ route('locale.setting', 'en') }}">
EN
</a>
<a href="{{ route('locale.setting', 'es') }}">
ES
</a>
You will need a route that will change the current language:
Route::get('set-locale/{locale}', function ($locale) {
App::setLocale($locale);
session()->put('locale', $locale);
return redirect()->back();
})->middleware('check.locale')->name('locale.setting');
And a middleware that will set the appropriate language for other routes:
public function handle($request, Closure $next)
{
if(session()->has('locale')) {
app()->setLocale(session('locale'));
app()->setLocale(config('app.locale'));
}
return $next($request);
}

Laravel comes with localization support. Its explained in depth here.
What you basically need to do is to define all your localization strings inside the resources/lang directory. By default you'll find an en directory where there are files that hold localization for different parts of the application. You can create an app.php file inside the en folder and define keywords and their English text. Then add another folder for your language (for example es for Spanish) and also create another app.php in there with the Spanish texts. These will be your translation strings.
These strings can be used in your views using the __ function or #lang blade directive.
To change the current language (locale), you need to setup a route that listens for this request and set the locale accordingly. Something like this:
routes/web.php
Route::get('/setLang?lang={locale}', function ($locale) {
\App::setLocale($locale);
// Session::put('locale', $locale); // This should also work
return back();
});

that's alot of work... You should use language files, like en.php and use define() to store translations. Note that de variable should be the same across all files. Example:
en.php
define('GREETINGS', 'Hello');
Now in portuguese for example:
pt.php
define('GREETINGS', 'Olá');
Now, in your language selector or master switch, you should store in a session variable like user_language = 'en. On every request / page load, define the proper file to load translations.
EDIT #1 - To be clean, use a middleware to handle this.
I think you got the idea!

Related

PUT and DELETE route is not showing in laravel route list on apiResource [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I added apiResource routes in my api.php file. Although it shows all the resourceful routes for product/category, it is not showing the routes for PUT and DELETE methods in the case of products routes.
Showing 404 not found error.
Any help?
You can solve the missing parameter names using the parameters method.
Route::prefix('products')->group(function() {
Route::apiResource('/', \App\Http\Controllers\ProductController::class)
->parameters(['' => 'product']);
Route::apiResource('/categories', \App\Http\Controllers\CategoryController::class);
});
That should give you the following:
Note that it is a convention to make your resources plural rather than singular.
You can accomplish a lot using scoped nested resources and shallow nesting:
Route::apiResource('products', ProductController::class);
Route::apiResource('products.categories', ProductCategoryController::class);
Route::apiResource('products', ProductController::class);
Route::apiResource('products.categories', ProductCategoryController::class)->shallow();

How can add Laravel Helper in Vue js Inertia JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Good day
I Beginner in Inertia Js and Vue Js i have one Helper for Laravel normally I use this code in .blade Laravel and working Helper::reaction_count($id,$reaction) but in Inertia Js i thing must passed everything from controller , can help me , how i can add Helper code in .vue theme ?
Thanks in advance
If you want to pass object to Javascript from laravel directly then you can use laracasts/utilities package.
composer require laracasts/utilities
Then add a line in config/app.php
// config/app.php
'providers' => [
'...',
'Laracasts\Utilities\JavaScript\JavaScriptServiceProvider'
];
You can pass object to javascript like this
use JavaScript;
public function index()
{
JavaScript::put([
'count' => Helper::reaction_count($id,$reaction)
]);
...
}
For more information, please check here
https://github.com/laracasts/PHP-Vars-To-Js-Transformer

Laravel is there any other way to display data from controller to blade? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose I only selected one specific row from the database,
and returned that result to view.blade.php, now I want to display that single row of data. How would I do it without using #foreach is there any other function in laravel that i can use?
you can use first() to get an object instead of collection. so you need not to loop through to get object property.
controller
public function functionName($parameter)
{
$value = Model::where('field_name', $parameter)->first();
return view('view-name', compact('value'));
}
view blade
{{ $value->property }}
Suppose you have a record stored in $data variable now
you can simply display it on your view template like
`echo $data->image; //without using any #foreach loop`

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

Laravel 4 multilanguage routing

I'm trying to implement a multilanguage routing.
The problem I'm facing lies in pointing out a route translated to more than one language, to a controller of its own. Let me give a simple example:
Let's say I have a simple route as follows
Route::get('/contacts', 'PageController#contacts');
And I want the same controller to be used for another route, but this time translated in another language, german for example.
Route::get('/kontakte', 'PageController#contacts');
For a simple webiste, with no more than 5-6 pages, writing down the routes for all languages would not be such a pain, but for more complex website, with huge amount of pages and having more than 2 available languages, a solution like this would be ugly.
I found an older topic here, where the author suggested loading a route.php file depending on the currently selected language. But still, this would require more than one file to be edited for further need.
A point of suggestion or currently working solution would be really appreciated.Thanks for your help.
Just some quick thoughts:
A solution can be to do grouping routes with a prefix like '/en/' and '/de/'.
So you will have /en/contact and /de/contact.
docs for this: http://laravel.com/docs/routing#route-prefixing
This way you can just create a loop through your available languages, and register the route.
The con here that you can't have a /de/kontake or /kontakte url, because there is 1 loop with routes, and they probably will be in English.
<?php
$languages = array('en', 'de');
foreach($langauges as $language)
{
Route::group(array('prefix' => $language), function()
{
Route::get('/', 'HomeController#index');
Route::get('contact', 'HomeController#contact');
});
}
A second solution will be to store all your routes in a database (or just an array to test it in the beginning)
You will need some Page and PageLocal models for it.
Page: id, name, controller
example: 1, contact, PageController#contact
PageLocal: id, page_id, language, slug
example: 1, 1, en, contact
example: 1, 1, de, kontakte
Loop through all Pages, lazy load the PageLocal with it, and register the routes.
You can throw out the language column if you like, but lookout for duplicate slugs. Thats why a language prefix is a good idea. (And perhaps it will help with some SEO...)
<?php
$Pages::with('Locals')->all();
foreach($Pages as $Page)
{
foreach($Page->Locals as $PageLocal)
{
Route::get($PageLocal->language.'/'.$PageLocal->slug, $Page->controller);
}
}
And after that you still have to think about url's without a language prefix, get and post routes, etc, etc, but this will get something started.

Resources