English 中文(简体)
More descriptive Language String placeholder?
原标题:
<?LANG(no_download,  you are not allowed to download )

instead of

$lang[no_download]

I have what I think is a better approach for embedding language strings in templates.

In almost all the PHP applications, the predominant language placeholder format is like <?=$lang[ no_download ]?> or {{no_download}}. Other designers/developers/translators will have a hard time deciphering what the placeholders represent without referring to the language file.

In order to make language placeholders more descriptive, why don t we include the original string together with the placeholder? e.g.

<?=lang(  no_download  ,  You are not allowed to download this file because you have exceeded your quota  )?>

The second parameter is a dummy and thus nothing is being done to it by the lang() function.

At a glance, one might think that it is too verbose, adding clutter to the template markup. But in my opinion it is not a valid argument since the language string would ve taken as much space as the placeholder if it weren t language aware.

I would like to hear your thoughts regarding this.

问题回答

EDIT Didnt see this was tagged Smarty. Disregard my answer if you want a Smarty specific answer.

I wouldn t agree that there is a predominant placeholder format. In general, the format depends on how you handle the translations and that can vary.

Apart from that, it is not uncommon to provide the default language strings as translation labels. For instance, in the Zend Framework manual, this is part of their usage example. Nothing wrong with that if your translation adapter supports that. If it requires IDs, then it s not an option.

Personally, I prefer IDs and try to name them according to their usage in the application, e.g. form.label.login.username or cart.checkout or flashmessage.notice.

Using IDs also provides another layer of separation. Given that texting might not fall into the responsibility of either developer or template designer, it s better to have IDs in your templates.

To my opinion, it s wiser to think about setting great descriptive "keys" for your entries... Your "no_download" key doesn t describe the message well because it lacks a verb.

Keys, must be named after a good naming convention, as well as variables and functions.

Your key should be for example :

{{user_quota_exeeded}}

or

{{user_quota_exeeded_alert}}

In the long run, if your "real string" gets changed and the "dummy parameter" in the template doesn t, it will fool some of your co-workers/clients/...

Furthermore, it forces you to type your messages twice.

More on naming conventions on wikipedia.

Gettext does this already. It goes something like this:

_( Gettext does this already. It goes something like this: );

Those were all great answers, great insights, guys.

Arno, I am very aware of naming conventions. My example language key was indeed not very aptly named but it was only done so to serve as an example. Throughout the translation process of my web application, I came across strings that were hard to summarize/describe with a few strings, thus the thought of including the default language string as translation labels. I m sure if you have worked on translation enough you will come across this scenario.

Anyway it is reassuring to see that Zend & Gettext have already adopted this approach.

Another question that popped into my head is the performance penalty.

Will using function calls , e.g. LANG() , take up significantly more resources than simply printing out $lang[] arrays?

What dya think?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签