• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setLocale() doesn't change the language set by browser

#1
Question 
Hello everybody



I'm new to CI and to any framework and it's just awesome! Heart 
I'm not used to write in forums as I'm usualy allways able to find a solution already posted somewhere so it's my first time in 20 years of IT. Big Grin So I hope my english is ok as the rules say "proper english only" and I correctly explain my problem.



So to my problem:

I'm making a bilingual site french/german with CMS features so I use bilingual db's and the CI locale feature.



The language recognition of browsers works fine but I'm unable to change the language through



$this->request->setLocale($language);

or

setlocale(LC_ALL, $language);



when I control it with getLocale() I can see that setLocale() works but there is no change in the views...



in Config/App.php

public $defaultLocale = 'de';

public $negotiateLocale = true;

public $supportedLocales = ['de-CH','de-DE','de-AT','fr-CH','fr-FR','fr-CA','fr-BE','fr','de'];      ( tryed also ['de','fr'] )



my switchlanguage-button calls Language.php Controller with:

PHP Code:
<?php namespace App\Controllers;
use 
CodeIgniter\HTTP\IncomingRequest;

class 
Language extends BaseController
{
    public function 
setLanguage($language)
    {
        
$session = \Config\Services::session();
        
$session->set('language',$language);
        
$this->request->setLocale($language);
        
$session->set('locale',$this->request->getLocale());
        
/*setlocale(LC_ALL, $language);*/
        
return redirect()->back();
    }



I'm using session('language') to select the right columns in the db's and the locale for all the titles, bottons, validation errors and more...




in BaseController.php I got:

PHP Code:
$session = \Config\Services::session();
if (
session('language') == null) {
    
$locale $this->request->getLocale();
    
$language substr($locale02);
    
$session->set('language',$language);
    
$session->set('locale',$locale); /*<-this only for debuging*/



Can you see where I'm wrong? I can't find a solution. CI locale strictly follows the Browsers properties and I can't change it...
I'm sure it's something stupid Wink


thank you
Reply

#2
Tongue 
I still don't know if setlocal() should change the selected language used by the language files or if it was just my immagination...

So I finaly did manualy insert the language variable in all the lang() functions like that:
Code:
<?= lang('Titles.titleStundenplan', [],$language)?>

The only problem now is for the validation error language who strictly follows the user properties set in browser. I don't know how to change that manualy.

any help would be apreciated...
Reply

#3
To set the language you need to do this:

PHP Code:
$language = \Config\Services::language();
$language->setLocale($real_language); 

That way you won't need to set the language every single time. Also, I think that this function would do the validation for you.
Reply

#4
Thank you but it's still not working.....
Checking with $language->getLocale() I see it's changing but in the lang() function it doesn't nor for validation...

PHP Code:
public function setLanguage($real_language)
 {
    $language = \Config\Services::language();
    $language->setLocale($real_language); 

    /*just for debuging*/
    $session = \Config\Services::session();
    $session->set('language',$real_language);
    $session->set('locale',$language->getLocale());

return 
redirect()->back();

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.