loading lang file for another user.

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

loading lang file for another user.

Post by andreask » Fri Aug 26, 2016 10:45 pm

Is there a way to load a lang file FOR another user and not for the one that is active $user.

What I want to do is I want to assign some lang variables to my e-mail reminder cron task.
I've found the way to load a template file when when there is NO user, but how do I load a lang file FOR a user?

Also I have another question.

I've noticed that $messenger will fail if template is not found according to the user lang preferences.
Is there a way to have a fallback in this case? because some users have chosen some "exotic" languages that of course I do not have the template for. But this breaks the whole process.

Thank you
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

User avatar
david63
Jr. Extension Validator
Posts: 14645
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: loading lang file for another user.

Post by david63 » Sat Aug 27, 2016 1:18 pm

Cannot say that it is something that I have needed to do but if you look at MarkDHamill's Digests extension I seem to recall that he is doing what it is that you are after
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: loading lang file for another user.

Post by andreask » Sat Aug 27, 2016 11:14 pm

Unfortunately it's not what I need, his digest sender uses only English... :cry:
(to my understanding at least)

For my other problem here is what I did...

Code: Select all

function get_lang()
	{
		$sql = 'SELECT lang_iso
                FROM ' . LANG_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$lang = $row;
		}
		$db->sql_freeresult($result);

		return $lang;
	}
and then where I need the lang fallback I just do...

Code: Select all

$lang = (in_array($user['user_lang'], $this->get_lang())) ? $user['user_lang'] : $this->config['default_lang'];
Nothing smart, but it does the job...

I just cannot understand how a user could select a language that does not exist on board...
Last edited by andreask on Sun Aug 28, 2016 4:35 pm, edited 1 time in total.
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

User avatar
3Di
Registered User
Posts: 12893
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: loading lang file for another user.

Post by 3Di » Sat Aug 27, 2016 11:52 pm

If I correctly understood you could try and check first if such an exotic language exist, if not then fallback to English. Something alike, just to give you an idea from where to start from.

if ( file_exists($lang_path . $user->data['user_lang']) )
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20889
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: loading lang file for another user.

Post by RMcGirr83 » Sun Aug 28, 2016 2:15 am

In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: loading lang file for another user.

Post by andreask » Sun Aug 28, 2016 11:05 pm

Thank you all for your suggestions.
what about my first question?

I tried to load user data this way...

Code: Select all

$this->user->data = $user;
where user is an array from a complete select from users_table used in a foreach row.

But it does not work this way I guess...

Thanks!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: loading lang file for another user.

Post by andreask » Tue Aug 30, 2016 7:18 pm

Maybe I should rephrase...

Doing this..
$this->user->lang('VAR_TO_LANG');

Will load the value of VAR_TO_LANG (from the language file of ext) for a user to his language (if it exists) otherwise it will fallback to EN. (or something like that) IF there is a session for $user.

But, what I need is to use the configuration of a user to load the template according to his configured language.
Because if I just use (in my case) $this->user->add_lang_ext('andreask/ium', 'body'); it will load the default language of the board and NOT the language set by the wanted user.

Thanks again.
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3097
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: loading lang file for another user.

Post by kasimi » Tue Aug 30, 2016 8:00 pm

Tested with 3.1.9. Don't rely on this code to work in any other version of phpBB, it may break at any time. You need to inject the user_loader service into your class.

Code: Select all

$user_row = $this->user_loader->get_user($some_user_id);
$user = new \phpbb\user(null);
$user->lang_name = $user->data['user_lang'] = $user_row['user_lang'];
$user->add_lang_ext('andreask/ium', 'body');
echo $user->lang('VAR_TO_LANG');
I imagine doing this in 3.2.x should be easier and more reliable since the language-related logic has been extracted from the user class, but I haven't looked into that as I assumed you need this to work with 3.1.x.

andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: loading lang file for another user.

Post by andreask » Tue Aug 30, 2016 8:19 pm

Kasimi thank you!
Could you please explain to me this line?
$user->lang_name = $user->data['user_lang'] = $user_row['user_lang']; what exactly does it mean/do?
I am not that knowledgeable to php, Still learning.

One last thing. If I inject the $user class via the services.yml and __constructor etc...
How do I define this new user([b][u]null[/u][/b])?

Yes you are correct this is for 3.1.x (for now).

My respects!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3097
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: loading lang file for another user.

Post by kasimi » Tue Aug 30, 2016 9:00 pm

Code: Select all

$user->lang_name = $user->data['user_lang'] = $user_row['user_lang'];
is the same as

Code: Select all

$user->lang_name = $user_row['user_lang'];
$user->data['user_lang'] = $user_row['user_lang']; 
$user->lang_name and $user->data['user_lang'] are both accessed in the $user->add_lang_ext() method which is why they need to be set prior to calling it.

The user service that you inject and store in $this->user is always the current user. In my opinion it's best not to pollute the current user's object with other language data which is why a new user instance is created, independent of the current user. The constructor expects a $datetime_class to be passed as first argument but you don't need that for grabbing language data, hence the null. If you do need to format dates using that user instance after all, you can pass '\phpbb\datetime' to it.

andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: loading lang file for another user.

Post by andreask » Tue Aug 30, 2016 9:17 pm

Awesome!
I was actually looking for this!

Thanks for the explanation!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

andreask
Registered User
Posts: 458
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: loading lang file for another user.

Post by andreask » Tue Feb 13, 2018 2:35 pm

I am resurrecting this thread because I have to, and I don't see it practical to open a new one.

After following kasimi's suggestion, I am trying (once again) to use the following code...

Code: Select all

	$user_row = $this->user_loader->get_user($sleeper['user_id']);
	$user_instance = new \phpbb\user($language, $sleeper['user_timezone']);
	$user_instance->lang_name = $user_instance->data['user_lang'] = $sleeper['user_lang'];
	$user_instance->timezone = $user_instance->data['user_timezone'] = $sleeper['user_timezone'];
	$user_instance->add_lang_ext('andreask/ium', 'body');
First of all I'd like to point that for this to work, I need to inject language. (but this is another story).

My problem is now that even though most of language works fine.
Meaning this works fine $user_instance->add_lang_ext('andreask/ium', 'body');
It changes according to users language, but of course this is only because I specify the language during on template() of messenger.
$messenger->template('@andreask_ium/sleeper', $lang);
But if I use for instance this $user_instance->lang('INCLUDE_FORUM_TOPICS') I only get the English. version.

If I do a dump on user_instance the correct language is set everywhere but here...

Code: Select all

object(phpbb\user)[147]
  protected 'language' => 
    object(phpbb\language\language)[105]
      protected 'common_language_files' => 
        array (size=1)
          0 => string 'common' (length=6)
      protected 'common_language_files_loaded' => boolean true
      protected 'default_language' => string 'el' (length=2)
      protected 'user_language' => string 'en' (length=2)
      protected 'language_fallback' => 
        array (size=3)
          0 => string 'en' (length=2)
          1 => string 'el' (length=2)
          2 => string 'en' (length=2)
      protected 'lang' => 
        array (size=2960)
          'TRANSLATION_INFO' => string '' (length=0)
          'DIRECTION' => string 'ltr' (length=3)
          'DATE_FORMAT' => string '|d M Y|' (length=7)
          'DATETIME_FORMAT' => string '|d M Y, H:i|' (length=12)
          'USER_LANG' => string 'en-gb' (length=5)
see protected 'user_language' => string 'en' and 'USER_LANG' => string 'en-gb' (length=5).

Any suggestions on how I can make it use the users language?
NOT the $this->user but the loaded user $user_instance

Thank you for your time!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3097
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: loading lang file for another user.

Post by kasimi » Sat Feb 17, 2018 5:28 pm

In 3.2 you don't need a new user instance, you can work with the language class:

Code: Select all

// Inject this if you're in a service
$language_loader = $phpbb_container->get('language.loader');

$other_language = new \phpbb\language\language($language_loader);
$other_language->set_user_language($sleeper['user_lang']);
echo $other_language->lang('LOGIN_REQUIRED');

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3097
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: loading lang file for another user.

Post by kasimi » Sun Feb 18, 2018 9:52 am

On second thought, you don't need to do this. Use the template system. After all, the email.txt files are Twig templates.

Assign the data to the template before sending the email:

Code: Select all

$this->template->assign_var('NUMBER', 2);
Contents of email.txt:

Code: Select all

There are {{ lang('HOW_MANY', NUMBER) }}.
HOW_MANY is defined in your language file:

Code: Select all

'HOW_MANY' => [
    0 => 'none',
    1 => 'just one',
    2 => 'so many',
]
This will send the email There are so many.

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 6 guests