Jump to content

The language of $user->language (guest) is not correctly


Dennis Spohr
 Share

Recommended Posts

Hi all,

I have a multi-language site with 2 languages: the default one (English) and German.

But if I load my root-page, the language of my guest ($user->language) is always the default one (English).
My browser (Firefox in this case) is in German - so the $user->language should be set to German in this case, right?

Shouldn't I define the German language somewhere with "DE" oder "DE-de"? How does the system knows actually that this is suppose to be the German translations?

I already translated the C in
wire--modules--languagesupport--languagesupport-module
into de_DE.UTF-8 already.

Any idea why this is not working?

Thanks!
Dennis

Link to comment
Share on other sites

ProcessWire doesn't do any detection in terms of language.

The default here translates more to main language on which everything operates.

So the behaviour you experience is perfectly normal and fine. In case you need some kind of detection, you have to get this done with either PHP (not so solid, when ProCache is in place) or JS.

In the past we had a module for this but it was super outdated and is no longer listed in the modules directory.

Here are some posts about "language redirect" or "language detection".
In case you want to change the default/main language for your project: "Change homepage's default language"

Link to comment
Share on other sites

Ah okay, then it makes sense! I thought that functionality was built in.

If anyone needs this: I created the following to detecting the language:
(The second one I got from https://stackoverflow.com/a/25749660)

function LanguageDetection(array $available_languages)
{
    $session = wire('session');
    $page = wire('page');
    $user = wire('user');
    $config = wire('config');
    $languages = wire('languages');
    
    if ($page->id == $config->rootPageID && !$session->languageDetected)
    {
        foreach (DetectPreferedLanguages($available_languages) as $language => $prio)
        {
            $user->language = $languages->get($language);
            break;
        }

        $session->languageDetected = true;
        $session->redirect($page->localUrl($user->language));
    }    
    $session->languageDetected = true;
}

function DetectPreferedLanguages(array $available_languages) {

    $available_languages = array_flip($available_languages);
    $langs = [];

    if (!isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
        return $langs;

    preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]), $matches, PREG_SET_ORDER);

    foreach($matches as $match)
    {
        list($a, $b) = explode('-', $match[1]) + array('', '');
        $value = isset($match[2]) ? (float) $match[2] : 1.0;

        if (isset($available_languages[$match[1]]))
        {
            $langs[$match[1]] = $value;
            continue;
        }

        if (isset($available_languages[$a]))
            $langs[$a] = $value - 0.1;
    }

    arsort($langs);
    return $langs;
}

 

Now on the load of every page I just called my method like this (in my case at the $config->prependTemplateFile):

LanguageDetection(['de']);

 

On my site it seems to work with ProCache as well. I guess it depends on the settings in ProCache?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...