Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does gettext handle dynamic content?

In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content?

I have 2 cases in mind.

1) Let's say I have <?=$user1?> poked John <?=$user2?>. Maybe in some language the order of the words is different. How does gettext handle that? (no, I'm not building facebook, that was just an example)

2) Let's say I store some categories in a database. They rarely, but they are store in a database. What would happen if I do <?php echo gettext($data['name']); ?> ? I would like the translators to translate those category names too, but does it have to be done in the database itself?

Thanks

like image 571
Nathan H Avatar asked Nov 20 '09 20:11

Nathan H


People also ask

What is dynamic content and how does it work?

Dynamic content (aka adaptive content) refers to web content that changes based on the behavior, preferences, and interests of the user. It refers to websites as well as e-mail content and is generated at the moment a user requests a page.

Which parts of text can be transformed into dynamic content?

Some common parts of the text that can be transformed into dynamic content are the user’s location, weather & temperature, system details (browser, device type), etc. These are all pieces of information that can immediately be used even during first-time visits on the website.

What is innertext in Selenium WebDriver?

That is the reason I used exactly “innerText” everywhere above and same in getText () description by Selenium WebDriver developer. So the innerText is the property of the HTMLElement interface represents the “rendered” text content of a node and its descendants.

How to create a SharePoint file from dynamic content?

Choose a value – Select Name from dynamic content (that indicates the file name) Choose a value – Select Name from dynamic content (that indicates the file name) IF the condition got satisfied then it will move to the if yes part and create a new file in the specified SharePoint folder.


2 Answers

Your best option is to use sprintf() function. Then you would use printf notation to handle dynamic content in your strings. Here is a function I found on here a while ago to handle this easily for you:

function translate()
{
    $args = func_get_args();
    $num = func_num_args();
    $args[0] = gettext($args[0]);

    if($num <= 1)
      return $args[0];

    return call_user_func_array('sprintf', $args);

}

Now for example 1, you would want to change the string to:

%s poked %s

Which you would input into the translate() function like this:

<?php echo translate('%s poked %s', $user1, $user2); ?>

You would parse out all translate() functions with poEdit. and then translate the string "%s poked %s" into whatever language you wanted, without modifying the %s string placeholders. Those would get replace upon output by the translate() function with user1 and user2 respectively. You can read more on sprintf() in the PHP Manual for more advanced usages.

For issue #2. You would need to create a static file which poEdit could parse containing the category names. For example misctranslations.php:

<?php

_('Cars');
_('Trains');
_('Airplanes');

Then have poEdit parse misctranslations.php. You would then be able to output the category name translation using <?php echo gettext($data['name']); ?>

like image 89
Mark Avatar answered Sep 28 '22 02:09

Mark


If you have somewhere in your code: <?=sprintf(_('%s poked %s'), $user1, $user2)?> and one of your languages needs to swap the arguments it is very simple. Simply translate your code like this: msgid "%s poked %s" msgstr "%2$s translation_of_poked %1$s"

like image 33
Richard Brinkman Avatar answered Sep 28 '22 04:09

Richard Brinkman