Adriano Faria Posted February 2, 2018 Posted February 2, 2018 It may seem a stupid question but I really don't remember if there's a way. Long story short: I have a task that creates a topic and the post content is using the ACP language set which is different from the public side. User uses French in public side and English on ACP. As task runs on ACP, it is using English on posts. I can change a language but it's a per member basis and it is a task, not a member firing the action. \IPS\Member::loggedIn()->acp_language = $id; Is there a way to set the public side language in the task?
bfarber Posted February 2, 2018 Posted February 2, 2018 Well, stepping back, you shouldn't be calling to anything related to \IPS\Member::loggedIn() in a task. Tasks can be executed via cron, which have no session instance. I would recommend rethinking how you are retrieving the language strings in the task.
Adriano Faria Posted February 2, 2018 Author Posted February 2, 2018 11 minutes ago, bfarber said: Well, stepping back, you shouldn't be calling to anything related to \IPS\Member::loggedIn() in a task. Tasks can be executed via cron, which have no session instance. I would recommend rethinking how you are retrieving the language strings in the task. No, i’m not using. I just show how can I change the lang but it doesn’t apply exactly because it’s a task. What I need is to know how to use front end lang instead of ACP lang in the created topic, which is fired by a task.
Numbered Posted February 2, 2018 Posted February 2, 2018 Not really understand. But \IPS\Member::loggedIn()->language(true) - returned front-end of the current user \IPS\Lang object. After that you can get any language kay values with \IPS\Member::loggedIn()->language()->addToStack(...); where ... - you can just put a key or use it with sprintf. Without true param calling \IPS\Lang object will contain acp language too. So, for example, if your task need to send some text to some user and content of this text should contain right language from lang-keys - so you may use something like \IPS\Member::load(123456)->language()->addToStack('some_key'); - it will put correct value for your text on the selected by this member preffered lang (or default).
Adriano Faria Posted February 2, 2018 Author Posted February 2, 2018 10 minutes ago, Upgradeovec said: Not really understand. But \IPS\Member::loggedIn()->language(true) - returned front-end of the current user \IPS\Lang object. After that you can get any language kay values with \IPS\Member::loggedIn()->language()->addToStack(...); where ... - you can just put a key or use it with sprintf. Without true param calling \IPS\Lang object will contain acp language too. So, for example, if your task need to send some text to some user and content of this text should contain right language from lang-keys - so you may use something like \IPS\Member::load(123456)->language()->addToStack('some_key'); - it will put correct value for your text on the selected by this member preffered lang (or default). That’s not the case. The task calls a method in my content item and this method creates a topic based on a template, which has lang bits. I don’t use lang bits in the task.
bfarber Posted February 2, 2018 Posted February 2, 2018 Your best bet is something like this.... $language = \IPS\Lang::load( \IPS\Member::loggedIn()->language ?: \IPS\Lang::defaultLanguage() ); $langBit = $language->addToStack('...'); The language() method of \IPS\Member does have a $frontOnly parameter, however the method also leverages caching of the language object, so it wouldn't really help in this case.
Adriano Faria Posted February 2, 2018 Author Posted February 2, 2018 6 minutes ago, bfarber said: Your best bet is something like this.... The only problem here is that I don't have a lang bit to call in the task. It has something like this: ... $this->updateTopicWithResult( $raffle, $winnerArray ); ... then: protected function updateTopicWithResult( \IPS\raffles\Raffle $raffle, $winners ) { /* Get */ try { $topic = \IPS\forums\Topic::load( $raffle->topicid ); $content = \IPS\Theme::i()->getTemplate( 'submit', 'raffles', 'front' )->updateTopicWithWinners( $raffle->id, $raffle->type, $winners ); \IPS\Member::loggedIn()->language()->parseOutputForDisplay( $content ); $post = \IPS\forums\Topic\Post::create( $topic, $content, FALSE, NULL, NULL, $raffle->author() ); $post->post = $content; $post->save(); } catch ( \OutOfRangeException $e ) { return; } } The langs are in the updateTopicWithWinners template.
bfarber Posted February 2, 2018 Posted February 2, 2018 Your approach here is problematic. $content = \IPS\Theme::i()->getTemplate( 'submit', 'raffles', 'front' )->updateTopicWithWinners( $raffle->id, $raffle->type, $winners ); \IPS\Member::loggedIn()->language()->parseOutputForDisplay( $content ); This relies on a logged in member (again there won't be any when tasks run via cron). Also \IPS\Theme checks the dispatcher location, which won't be set for tasks run via cron. Instead of using a template, I'd probably recommend approaching this differently.
Adriano Faria Posted February 2, 2018 Author Posted February 2, 2018 True. Not sure why parseOutputForDisplay is there. It has any effect in this case. I will refactor this. Maybe simply update a flag in the table and create the post elsewhere.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.