Jump to content
enricob

How can I get the user profile language?

Recommended Posts

Hi all,

I know I can get the current user navigation language using this query:

$currentUser = $pages->get("template=user, id=".$user->id);

$currentUserLanguage = $currentUser->language->name;

or directly:

$currentUserLanguage = $user->language->name;

Now, if I am viewing the site with this user and change navigation language using the language switcher the value of  $userLanguage changes accordingly.

However, the REAL user language as seen in the user profile on the admin panel doesn't change.

So, my question is: how can I get the user profile language as set in the user profile?

Thank you!

Share this post


Link to post
Share on other sites

You have answered your question by yourself already :)

$user->language->name will not change, you said you switch the value of $currentUserLanguage, so you can query every time for $user->language->name again.

Share this post


Link to post
Share on other sites

Maybe I am not explained myself clearly but the fact is that the variable $user->language->name changes, reflecting the language I switched to on the frontend.

But when I look at the profile of the same user on the admin panel the language has not changed...Even if I look directly at the database I see that the language field of the page user (for the user I am viewing the site with) stays the same, even if I am switching the language on the frontend.

I think the point here is that Processwire automatically populate the variable $user->language with the currently active language, in some way overriding the actual language associated with that user.

In fact if I do the same query to get an user (but not the user I am logged in), the value of $userObject->language is exactly what I expect.

Share this post


Link to post
Share on other sites

ProcessWire does change the $user->language? What kind of language switcher you are using? A pointing link or code example would be helpful, or is it some example that comes with a PW site profile?

Share this post


Link to post
Share on other sites

User language is tied to what language is requested. So it changes on runtime.

User language set in admin is for backend primary. If you need user to set a language for frontend you could add a field to user and check that.

  • Like 1

Share this post


Link to post
Share on other sites

Where is it done, the change on runtime?

Share this post


Link to post
Share on other sites

Thanks Soma, that's what I though...

But if you check the value of the language field of the current user object while he's viewing the site (not by querying the $user object, but for example looking at the database) you'll see that the value never change when the user change the navigation language, while $user->language value does.

So, if $user->language give me the current navigation language (that changes at runtime) how do I get the language value of the current user though the api?

Share this post


Link to post
Share on other sites

I would not use the $user var to get the saved value. I think it should be possible to call this: (but have not tested it yet)

$id = $user->id;

$savedLanguage = $users->get($id)->language->name;

Share this post


Link to post
Share on other sites

@horst in the LanguageSupportPageNames.module I guess. It's evaluated with some AI to get the right language and page.

Once the language is determined by the requested url the user language is set on runtime. It's not saved in DB of course.

I would not use the $user var to get the saved value. I think it should be possible to call this: (but have not tested it yet)

$id = $user->id;

$savedLanguage = $users->get($id)->language->name;

This would load the cached user already with changed language.

Thanks Soma, that's what I though...

But if you check the value of the language field of the current user object while he's viewing the site (not by querying the $user object, but for example looking at the database) you'll see that the value never change when the user change the navigation language, while $user->language value does.

So, if $user->language give me the current navigation language (that changes at runtime) how do I get the language value of the current user though the api?

This is determined very early before page renders etc. So only way without adding your own field to manage this, would be to save the language to the user with a new property (at runtime) and this should happen before LanguageSupportPageNames does it's job. In template code you won't be able to do this unless you do a DB manual query.

So an autoload module would do that with an added hook line of code in the init() (see HelloWorld.module)

public function init() {
    $this->wire("user")->savedLanguage = $this->wire("user")->language;
}

and then use in templates:

echo $user->savedLanguage->name;
  • Like 4

Share this post


Link to post
Share on other sites

Thanks Soma, you're a genius!!!

You're solution does not only work perfectly but it's also elegant and clean :)

  • Like 1

Share this post


Link to post
Share on other sites

I had the same problem,, and there is actually a way to access the profile language without using a hook by calliing loadPageField function of a Fieldtype, which fetches the data directly in the database:

$languageField = $fields->get("language");
$profileLanguageId = $languageField->type->loadPageField($user, $languageField)[0];
$profileLanguage = $pages->get("id=$profileLanguageId");

Share this post


Link to post
Share on other sites

I had the same problem,, and there is actually a way to access the profile language without using a hook by calliing loadPageField function of a Fieldtype, which fetches the data directly in the database:

$languageField = $fields->get("language");
$profileLanguageId = $languageField->type->loadPageField($user, $languageField)[0];
$profileLanguage = $pages->get("id=$profileLanguageId");

What hook?

Looks complicated. ;)

Share this post


Link to post
Share on other sites

You could also do

$current_lang = $user->language;
$pages->uncacheAll();
$saved_lang = $user->language;
$user->language = $current_lang; // reset

So it will reload the user. But you have to reset it back afterwards. (And I'm not sure a $pages->uncacheAll() will make processwire reload all pages already in cache. So I'm not sure it's a good solution if not really needed. )

Share this post


Link to post
Share on other sites

Indeed, no hook, I hadn't read it through thoroughly enough and had assumed module = hook. :-[

And your solution is indeed less complicated, I think I'll replace mine by yours, it makes the code much easier to read :)

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Juergen
      Hello @ all,
      I have created an inputfield with a configuration field in the backend where you can set a time format for every language (see screenshot below).

      As you can see both language values (default and German) have the default value(%R), but I have set different values, which were correctly stored in the DB (see screenshot below):

      As you can see the values are '%R' and '%r'.
      I have created the configuration inputfield like this:
      /** @var InputfieldText $f */ $languages = $this->wire('languages'); $f = $this->wire('modules')->get('InputfieldText'); $f->attr('name+id', 'timeformat'); $f->label = $this->_('Timeformat on frontend'); $f->initValue = '%R';//default value $f->attr('value', $this->timeformat ? $this->timeformat : '%R'); $this->message($this->get('timesformat')); if($languages) { $f->useLanguages = true; foreach($languages as $language) { if($language->isDefault()) continue; $f->set("value$language", (string) $this->get("timeformat$language->id")); } } $f->inputType = 'text'; $f->description = $this->_('Please enter the time format that the times should appear on the frontend in strftime format.'); $f->notes = sprintf($this->_('For example shows the time as 08:00, as 08:00 AM. You can find more examples at %s.'), '<a href="https://www.php.net/manual/de/function.strftime.php">https://www.php.net/manual/de/function.strftime.php</a>'); $f->columnWidth = 100; The important part here is:
      if($languages) { $f->useLanguages = true; foreach($languages as $language) { if($language->isDefault()) continue; $f->set("value$language", (string) $this->get("timeformat$language->id")); } } I have borrowed the code from the DateTimeInputfield (https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldDatetime/InputfieldDatetime.module), but the field values will be always populated with the default value ('%R').
      Are I am missing something? Does anyone has experience with multilanguage fields and could help me out?
      Thanks in advance.
    • By snobjorn
      Here's my Norwegian language pack for ProcessWire. I've been adding translations over some time, to suit my own projects users needs.
      Last updated:
      2020/06/29 (June 29, 2020)
      Status:
      Not yet complete. Translations that non-superusers will see is prioritized.
      Name:
      ProcessWire-Norwegian-Language-Pack-nb-NO
      Translated using:
      ProcessWire 3.0.161 dev
      Number of files:
      202 wire files and 97 site files – ranging from just started to completely translated. The site files includes translation some free and some pro modules, see the complete list in the README.md at GitHub.
      Available at GitHub:
      https://github.com/snobjorn/processwire-norwegian-language-pack-nb-no
    • By neonwired
      I'm hoping someone has seen this before. There doesn't appear to be an issue with the user info.

       

    • By Sten
      Hello
      Till now I hacked something with the twig template but it works no more with new PW versions so I look forward to create a module. I am working on a site in multiple languages : French, English, Italian, German, Spanish, Portuguese, Hebrew, Russian. The new posts are entered in any language with a field for language. Till now, I got twig files to get the translations with constants defined for each part of the pages.
      So I'd like to create a module to include theses files added according to the url /fr/en/...
      Have you some observations to do before I begin about the direction to take ?
      Thank you
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
×
×
  • Create New...