Consistency in Laravel localization - laravel

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

Related

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);

Joomla get translation - custom

I have a custom component generating a email newsletter. Now I want to generate the html in the language of the user.
But how can I access the user language file within the code?
using
$lang->setLanguage( $language );
$lang->load();
does not wrok
Thanks
If the email is generated in front-end while user is navigating your site, you can get the language user is using:
$lang = JFactory::getLanguage();
$langTag = $lang->getTag();
echo $langTag; // for example, "en-GB" for English.
$langTag could be different if your site is a multilingual site and user is browsing in a different language, not English.
When you get the language tag, you can generate your newsletter in that language and send.
If you send newsletter from back-end, you can get user's language like this
$user = JFactory::getUser(USER ID HERE); // $user = JFactory::getUser(123);
$langTag $user->getParam('language', 'en-GB'));
echo $langTag;
User can change his/her language in user profile, if user uses the default language, $langTag will be empty, so we use 'en-GB' as fallback.
I hope this helps you.
include
protected $autoloadLanguage = true;
inside your component class in php. And use language files.

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.

codeigniter_i18n multi language

I already read and follow the instruction from this link codeigniter_i18 multilanguage and it's works, but I have a little problem here, I don't know maybe at the routes config or the scripts.
for the example, this http://mysite.com is en default language in index of the site, but if I want to change different language for the instance dutch, so how to implement that I can get the url like this http://mysite.com/nl/
thanks in advance
Using it in the path like actually makes things a lot more complicated, because you ALWAYS need the first segment to be a country code (so you need to use /en for English)
An easier method to consider is to set a session variable when they select a language, and do it "in the background":
In your MY_Controller:
public function __construct()
{
parent::__construct();
$lang_code = ($this->session->userdata('lang_code'))? $this->session->userdata('lang_code'):'english';
$this->lang->load('project_launch', $lang_code);
$this->lang->load('project_launch_template', $lang_code);
$this->lang->load('project_launch_uploader', $lang_code);
}
function lang_select(){
$lang_code = $this->input->post('lang_code');
$this->session->set_userdata('lang_code', $lang_code );
}
and have your language selector (dropdown, little flags, whatever) call lang_select() to change the language & set the session variable; the construct will check the language each page load and load the appropriate language files

Resources