Development BlogPlurals in language

Post Reply
User avatar
Topic Starter
Restless Rancor
Great Britain

Site Admin
New Member
Posts: 48
Joined: October 15th, 2018, 3:14 pm
Age: 25
Location: Hampshire, England
Contact:

Plurals in language

#1

Post by Restless Rancor » October 20th, 2018, 1:49 pm

I was able to easily add the language functions to my User's Post Count on Index extension, but am having trouble with plurals.

Adding the function to the listener was easy enough:

Code: Select all

public function load_language_on_setup($event)
{
	$lang_set_ext = $event['lang_set_ext'];
	$lang_set_ext[] = array(
		'ext_name' => 'restlessrancor/postcountonindex',
		'lang_set' => 'postcountonindex',
	);
	$event['lang_set_ext'] = $lang_set_ext;
}
And the language too:

Code: Select all

$lang = array_merge($lang, array(
	'PCOI_PREFIX'	=> 'You have',
	'PCOI_SINGULAR'	=> 'post',
	'PCOI_SUFFIX'	=> 'posts!',
));
So index_body_markforums_before.html can now contain {L_PCOI_PREFIX} {USER_POST_COUNT} {L_PCOI_SUFFIX} instead of You have {USER_POST_COUNT} Posts!.

It currently works on an IF statement to determine if the post count is either 1 or not 1. {% if USER_POST_COUNT == '1' %} and outputs the correct plural, but I know I can simplify the system to not require that IF statement and have it add the plural itself.

I've tried to define a plural array, but this didn't do the trick. A "too few arguments" error is displayed.

I'm going to have to spend more time figuring this one out, but I'm sure once I've sussed it it'll be simple enough to implement in the future on other projects.


These aren't the droids you're looking for.

User avatar
Topic Starter
Restless Rancor
Great Britain

Site Admin
New Member
Posts: 48
Joined: October 15th, 2018, 3:14 pm
Age: 25
Location: Hampshire, England
Contact:

#2

Post by Restless Rancor » October 21st, 2018, 5:21 pm

With a lot of guidance I was finally able to get this working as intended.

Code: Select all

public function pcoi_header()
{
	$posts = (int) $this->user->data['user_posts'];		
	$this->template->assign_vars([
		'S_USER_ID'		=> $this->user->data['user_id'],
		'USER_POST_COUNT' => $this->language->lang('USER_POSTS', $posts),
	]);
}
I had to define the post count and feed it into the language file, so:

Code: Select all

$lang = array_merge($lang, array(
	'USER_POSTS'	=> array(
		0	=> 'You don’t have any posts.',
		1	=> 'You have %d post.',
		2	=> 'You have %d posts!',
	),
));
will now output:
0 Posts: "You don't have any posts."
1 Post: "You have 1 post."
2+ Posts "You have x posts!"

And this also results in not requiring the two {% if USER_POST_COUNT == 'x' %} in the template files, I can now simply have (now updated for full twig syntax):

Code: Select all

<a href="search.php?author_id={{ S_USER_ID }}&sr=posts" class="mark-read leftside" title="Search your posts">{{ lang(USER_POST_COUNT) }}</a>
These aren't the droids you're looking for.

Post Reply

Topic Subscribers

1 user subscribed to this topic: Restless Rancor