MyBB Community Forums
[Rejected] Language variables with spritf keeps first value and all language vars. - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Development (https://community.mybb.com/forum-161.html)
+--- Forum: MyBB 1.8 Development (https://community.mybb.com/forum-165.html)
+---- Forum: 1.8 Bugs and Issues (https://community.mybb.com/forum-157.html)
+----- Forum: Rejected (https://community.mybb.com/forum-184.html)
+----- Thread: [Rejected] Language variables with spritf keeps first value and all language vars. (/thread-215076.html)



Language variables with spritf keeps first value and all language vars. - Whiteneo - 2017-12-29

I am currently developing a new plugin and found this error into mybb.

In postbit where values are changes into every post or any new pages you can see this error.

Sample 1:

$lang->sample1 = "Sample 1";
---------
other lang vars.
------------
$lang->sample1 = "This is a new value for sample 1";



The right value must be the latest one, but unfortunatelly allways takes the first value.

And the real problem is when i use lang->sprintf

I mean if i woul like to use this:

$lang->sample2 = "User id for this post {1}";

$lang->sample2 = $lang->spritf($lang->sample2,$post['newvalue']);

And if i use this:

$lang->sample2 = "User id for this post ".$post['newvalue'];

Then the right value appears like if i use the sprintf ...

And use it into a hook in postbit template then allways takes the first post uid and the same text

This way i can not do any language vars like sprintf any more and i have to use directly into post vars, or how can we use this ?


RE: Language variables with spritf keeps first value and all language vars. - Wildcard - 2017-12-29

Please give us a code sample that we can use to test this issue. Sorry you are having troubles.


RE: Language variables with spritf keeps first value and all language vars. - Whiteneo - 2017-12-30

https://github.com/WhiteNeo/post_rate

Even i was sent in here but still waiting validation.

I have comented lang part into postbit with the common errors and in new templates i have to add multi instances to retrieve code besides one var or lang var. Maybe i am doing something wrong and no problem mate i know how hard is mantain the code up to date.

Thanks for your time.

I was taken the value from a new serialized data into posts and when i dump the var all values are right, if i use the code in parts it is fine too but if i use sprintf then the same value keeps it as the first one result.

In new templates i have to do the same.

If you can review that to help me on the right way to write all missing parts in this plugin it would be awesome.

Ok i have make a video of what i mean.

In ajax all works fine, even if i write lang vars with the value but do not with sprintf, only some parts works fine even if i put into a post var same happens, the value are there but only if i set without sprintf works fine.



Thanks in advance.


RE: Language variables with spritf keeps first value and all language vars. - Wildcard - 2017-12-30

Okay Whiteneo, I spent some time this morning and was able to track down the problem.

In inc/plugins/dnt_post_rate.php on line 860 (and 861 in the commented line), you are overwriting this language variable:

$l['pcl_total'] = '<span style="display:block">This post has been rated {1} times.';

So that on the first iteration it is set to this:

$l['pcl_total'] = '<span style="display:block">This post has been rated 6 times.';

Then the next call to MyLanguage::sprintf cannot replace the {1} because it isn't there any more.

To fix, just assign to a variable instead of assigning back to the language variable.


RE: Language variables with spritf keeps first value and all language vars. - Whiteneo - 2017-12-30

Ok undestand so the right way is using vars instead lang vars, well i can now continue writting this plugin without several unnecesary lang addittions because i do not like that Smile i prefer to use single as posible and editable as posible for user in the front end but backend i can focus into fix errors, customize the content to be easy to change from user's side.

Thanks for your time.


RE: Language variables with spritf keeps first value and all language vars. - Wildcard - 2017-12-30

(2017-12-30, 09:44 PM)Whiteneo Wrote: Ok undestand so the right way is using vars instead lang vars

To be clear, there is nothing wrong with assigning values to new properties of the $lang object, but in cases where MyLanguage::sprintf is used, those variables (with the number placeholders, ie. {1}, {2}) should not be overwritten. Instead create a new (unused) property:

foreach ($things as $id => $thing) {
	$value1 = $thing->['property1'];
	$value2 = $thing->['property2'];

	$lang->new_key = $lang->sprintf($lang->existing_key_with_placeholders, $id, $value1, $value2);

	echo "<a href=\"url.com\">{$lang->new_key}</a>";
}

(2017-12-30, 09:44 PM)Whiteneo Wrote: Thanks for your time.

You're welcome. Smile


RE: Language variables with spritf keeps first value and all language vars. - Whiteneo - 2017-12-31

yep at the end i will use that for new templates with the row value inside lang name declaration all other code must be the same, i was used a new post var for postbit but for new templates i will use that trick.

I mean i will use the count to set a value, then i will use this:

$lang->new_key[$item_no] = $lang->sprintf($lang->new_key, $id, $value1, $value2);

I think this one whould work either due i will assign new record value for every listed items into my sql rows.

I suppose this should work, but otherwise i will use foreach as you recommend, and thanks seriously for this aclaration, now i wont waste the time trying to make a function works where they are not Smile


RE: Language variables with spritf keeps first value and all language vars. - Wildcard - 2017-12-31

You're welcome. Cool