Which "key" to use to obtain current user's language setting

I’m struggling with a call to getUserValue() … looking at the Nextcloud core, this would seem to be correct:

$userLang = $this->config->getUserValue($userId, 'core', 'lang', 'default_language');

But I’m getting en back.

I have set the user’s language to “German”, so I would expect the call to return de, or at the least default_language if there’s no “lang” setting for the passed user.

From config.php:

'force_locale' => false,
'force_language' => false,
'default_language' => 'en',

If I log in to NC with that user, everything is displayed in German, so the setting sticks, but …

What am I doing wrong? :thinking:

You can always poke around in the database to see what actual data the API you are using is processing. For example the config value you asked for with the call above is in the preferences table. Note that the default value in your call is only used if there is no matching row at all in the table - it is not used if there is a row and the value in it is empty.

Thank you for the feedback.

So if I get empty back, I should (probably) force my preferred “default value” I guess?

I’ve checked the oc_preferences table, and for the given user, I have:

core    lang     fi
core    locale   sv_SE

And the NC UI does show things in Finnish (fi). But I still get en back in the lang call. I get the correct value back for the locale call. And seeing as how this is how the internals of NC seems to be doing it, I must have forgotten something or misread something. I just can’t quite figure out what.

Yeah, except for some reason, I must have been using the wrong call to get the userName/userId since I’m getting the admin of the instance, and not the user for the current session.

I’ll keep reading source code :wink:

1 Like

Yes, one has to add checking and setting of default value in the code used to read the config values, I’m afraid.

some point apart: Why do you need this?

If you are trying to use localized user messages, use the appropriate tools. I am just curious.

Yeah, I get that part, but thank you for checking. I’m doing this to (potentially) load different documents based on the user’s language setting (and if there is a language specific document).

1 Like