shortlink sitename in language file

Discussion forum for Extension Writers regarding Extension Development.
User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

shortlink sitename in language file

Post by alex75 » Wed Aug 15, 2018 12:19 pm

Hello guys. for the validation of my extension, I was asked not to use the short link {SITENAME} in the template. This is because in some languages the sitename may be before my phrase "welcome back to". this was suggested to me by the validation team:
In some languages the site could be on a different location as at the end. Because of that, you should use $ user-> lang () with as the parameter, instead of doing it in the template
. How can I solve the problem? Thanks for all.

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

Re: shortlink sitename in language file

Post by 3Di » Sat Aug 18, 2018 12:19 am

Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

Re: shortlink sitename in language file

Post by alex75 » Sat Aug 18, 2018 8:55 am

Thanks for your reply. I had already read that guide, I already understood that you must insert a placeholder (% s).
But it does not explain how and where to specify how to transform that placeholder into the sitename string.
I added the placeholder to my language file:
$lang = array_merge($lang, array(
'WELCOMEBACK_ON' => 'Welcome back on %s',
));
then I tried to add "sitename" to my file listener, but without success.
I added ('core.page_header_after' => 'wb_sitename',)
namespace alex75\welcomeback\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class listener implements EventSubscriberInterface
{

protected $user;
protected $template;
protected $root_path;
protected $phpEx;
protected $config;


public function __construct(
\phpbb\user $user,
\phpbb\config\config $config,
\phpbb\template\template $template,
$root_path,
$phpEx)
{
$this->user = $user;
$this->config = $config;
$this->template = $template;
$this->root_path = $root_path;
$this->phpEx = $phpEx;
}


static public function getSubscribedEvents()
{
return array(
'core.user_setup' => 'load_language',
'core.page_header' => 'build_urls',
'core.page_header_after' => 'wb_sitename',
);
}
after I added:
public function wb_sitename($event)
{
$this->template->assign_vars(array(
'WB_SITENAME' => $config['sitename'],
));
}
But obviously something is missing.

User avatar
david63
Registered User
Posts: 16563
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: shortlink sitename in language file

Post by david63 » Sat Aug 18, 2018 9:23 am

Try 'WB_SITENAME' => $this->config['sitename'],

You should have had an error with that code - do you use a development environment on your development server?

I am not sure though that that is what you are wanting. If you are trying to add the sitename to a lang var then the format should be $this->language->lang('MY_LANG_VAR', $var)

$user->lang is depreciated and in 3.2 you should be using $language->lang or in your case $this->language->lang('WELCOMEBACK_ON', $this->config['sitename'])
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

User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

Re: shortlink sitename in language file

Post by alex75 » Sat Aug 18, 2018 7:20 pm

Thanks for your help.
david63 wrote:
Sat Aug 18, 2018 9:23 am
You should have had an error with that code - do you use a development environment on your development server?
No I did not receive any errors (only that in the template I read welcomeback on %s)
I use xampp locally, with phpbb 321.
david63 wrote:
Sat Aug 18, 2018 9:23 am
I am not sure though that that is what you are wanting. If you are trying to add the sitename to a lang var
Yes, I want to add variable sitename to my language file.
I wrote the code you suggested to me.

Code: Select all

public function wb_sitename($event)
	{
	$this->language->lang('WELCOMEBACK_ON', $this->config['sitename']);
	}
now I receive this error by phpbb debugging:
Fatal error: Call to a member function lang() on a non-object in C:\xampp\htdocs\phpbbstoreit\ext\alex75\welcomeback\event\listener.php on line 82
line 82 is this that I wrote now.

User avatar
david63
Registered User
Posts: 16563
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: shortlink sitename in language file

Post by david63 » Sat Aug 18, 2018 8:31 pm

You need to assign that code to a template variable which the template will use something like

Code: Select all

public function wb_sitename($event)
{
$this->template->assign_vars(array(
'WB_SITENAME' => $this->language->lang('WELCOMEBACK_ON', $this->config['sitename']),
));
}
Then you would have WB_SITENAME in the template at the place where you want the language to be displayed.
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

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: shortlink sitename in language file

Post by GanstaZ » Sat Aug 18, 2018 8:34 pm

There's no language service available at the moment in your code, that's why you get that error. Inject $language service to use $this->language->lang().
"When answer lies in the question,.. question becomes redundant!"

User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

Re: shortlink sitename in language file

Post by alex75 » Sat Aug 18, 2018 8:47 pm

does not work. same error:
Fatal error: call to a member function lang () on a non-object.
maybe it's better than place all my file listener.

Code: Select all

<?php
/**
*
* @package welcome back
* @copyright (c) 2015 alex75 https://www.phpbb-store.it
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

namespace alex75\welcomeback\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class listener implements EventSubscriberInterface
{

	protected $user;
	protected $template;
	protected $root_path;
	protected $phpEx;
	protected $config;


	public function __construct(
		\phpbb\user $user,
		\phpbb\config\config $config,
		\phpbb\template\template $template,
		$root_path,
		$phpEx)
	{
		$this->user = $user;
		$this->config = $config;
		$this->template = $template;
		$this->root_path = $root_path;
		$this->phpEx = $phpEx;
		$this->language = $lang;
	}


	static public function getSubscribedEvents()
	{
		return array(
			'core.user_setup'	=> 'load_language',
			'core.page_header'	=> 'build_urls', 
			'core.page_header_after' => 'wb_sitename', 
		);
	}


	public function load_language($event)
	{
		$lang_set_ext = $event['lang_set_ext'];
		$lang_set_ext[] = array(
			'ext_name' => 'alex75/welcomeback',
			'lang_set' => 'welcomebacklang',
		);
		$event['lang_set_ext'] = $lang_set_ext;
	}

	public function build_urls($event)
	{
		$url_1 = append_sid($this->root_path . 'search.' . $this->phpEx, 'author=' . $this->user->data['username'] . '&amp;sf=firstpost&amp;sr=topics');

		$url_2 = append_sid($this->root_path . "ucp." . $this->phpEx, "i=ucp_main&amp;mode=drafts");

		$url_3 = append_sid($this->root_path . "ucp." . $this->phpEx, "i=ucp_main&amp;mode=subscribed");
		
		$url_4 = append_sid($this->root_path . "ucp." . $this->phpEx, "i=ucp_main&amp;mode=bookmarks");
		
		$url_5 = append_sid($this->root_path . "ucp." . $this->phpEx, "i=ucp_attachments&amp;mode=attachments");

		$this->template->assign_vars(array(
			'U_WBYOURTOPICS'		=> $url_1,
			'U_WBYOURDRAFTS'		=> $url_2,
			'U_WBYOURSUBSCRIBED'	=> $url_3,
			'U_WBYOURBOOKMARKS'		=> $url_4,
			'U_WBYOURATTACHMENTS'	=> $url_5,
		));
	}
	
	public function wb_sitename($event)
	{
	$this->template->assign_vars(array(
	'WB_SITENAME' => $this->language->lang('WELCOMEBACK_ON', $this->config['sitename']),
	));
	}
}
GanstaZ wrote:
Sat Aug 18, 2018 8:34 pm
There's no language service available at the moment in your code, that's why you get that error. Inject $language service to use $this->language->lang().
Valid hypothesis. now I added this:

Code: Select all

$this->language = $lang;
do you think is correct?
The error received is always the same:
Fatal error: call to a member function lang () on a non-object

User avatar
david63
Registered User
Posts: 16563
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: shortlink sitename in language file

Post by david63 » Sat Aug 18, 2018 8:49 pm

You need to inject language
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

User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

Re: shortlink sitename in language file

Post by alex75 » Sat Aug 18, 2018 8:59 pm

Code: Select all

$this->language	= $language;
same error:
Fatal error: Call to a member function lang() on a non-object
screen error.jpg

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: shortlink sitename in language file

Post by GanstaZ » Sat Aug 18, 2018 9:10 pm

You need to inject it the same way as you did with $user, $config, $template and so on.

Code: Select all

class listener implements EventSubscriberInterface
{

	protected $language;

	public function __construct(\phpbb\language\language $language)
	{
		$this->language = $language;
	}
}
"When answer lies in the question,.. question becomes redundant!"

User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

Re: shortlink sitename in language file

Post by alex75 » Sat Aug 18, 2018 9:19 pm

@GanstaZ

Code: Select all

class listener implements EventSubscriberInterface
{

	protected $user;
	protected $template;
	protected $root_path;
	protected $phpEx;
	protected $config;
	protected $language;


	public function __construct(
		\phpbb\user $user,
		\phpbb\config\config $config,
		\phpbb\template\template $template,
		$root_path,
		$phpEx)
	{
		$this->user = $user;
		$this->config = $config;
		$this->template = $template;
		$this->root_path = $root_path;
		$this->phpEx = $phpEx;
		$this->language	= $language;
	}
same error... :shock: :? :roll:

Edit:
So

Code: Select all

class listener implements EventSubscriberInterface
{

	protected $user;
	protected $template;
	protected $root_path;
	protected $phpEx;
	protected $config;
	protected $language;


	public function __construct(
		\phpbb\user $user,
		\phpbb\config\config $config,
		\phpbb\template\template $template,
		$root_path,
		$phpEx,
		$language)
	{
		$this->user = $user;
		$this->config = $config;
		$this->template = $template;
		$this->root_path = $root_path;
		$this->phpEx = $phpEx;
		$this->language	= $language;
	}
new error:
[phpBB Debug] PHP Warning: in file [ROOT]/ext/alex75/welcomeback/event/listener.php on line 31: Missing argument 6 for alex75/welcomeback/event/listener::__construct(), called in [ROOT]/cache/production/container_4335734bbdd20f586549a504dff5f80c.php on line 486 and defined

Fatal error: Call to a member function lang() on a non-object

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: shortlink sitename in language file

Post by GanstaZ » Sat Aug 18, 2018 9:24 pm

Did you update your .yml service file to inject $language & purged the cache? $language in your construct should be \phpbb\language\language $language.
"When answer lies in the question,.. question becomes redundant!"

User avatar
alex75
Registered User
Posts: 358
Joined: Sun Jun 10, 2012 9:09 am
Location: Italy
Name: Alessandro
Contact:

Re: shortlink sitename in language file

Post by alex75 » Sat Aug 18, 2018 9:37 pm

This is my services.yml now:

Code: Select all

services:
    alex75.welcomeback.listener:
        class: alex75\welcomeback\event\listener
        arguments:
            - '@user'
            - '@config'
            - '@template'
            - '@language'
            - '%core.root_path%'
            - '%core.php_ext%'
        tags:
            - { name: event.listener }
I purge the cache. this is new error:
Catchable fatal error: Object of class phpbb\language\language could not be converted to string in C:\xampp\htdocs\phpbbstoreit\ext\alex75\welcomeback\event\listener.php on line 64
line 64 is this:

Code: Select all

public function build_urls($event)
	{
		$url_1 = append_sid($this->root_path . 'search.' . $this->phpEx, 'author=' . $this->user->data['username'] . '&amp;sf=firstpost&amp;sr=topics');

User avatar
GanstaZ
Registered User
Posts: 804
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: shortlink sitename in language file

Post by GanstaZ » Sat Aug 18, 2018 9:43 pm

When you inject a service then everything should match in service (.yml) file and in your class (.php) file. If language service is 4'th argument in your .yml file, then in your constructor it should be 4'th as well.
"When answer lies in the question,.. question becomes redundant!"

Post Reply

Return to “Extension Writers Discussion”