escape Hebrew double quote on both php and javascript

I'm working on multi-language site based on php.

For supporting multi-language, I'm using localizing file like below.

[localize.en-US.php]

$lang_code = "en-US";
$is_rtl = false;

.
.
.

define("WORD_EMAIL", "e-mail");
define("WORD_NAME", "name");

.
.
.

Defined words used by two way like below.

[HTML]

<?=WORD_EMAIL?> : <input type="text" name="email"/>
<?=WORD_NAME?> : <input type="text" name="name"/>

[Javascript]

if(frm.email.value==="") {
 alert("<?=WORD_EMAIL?> required.");
 return false;
}

The problem occured when I'd working on Hebrew.

The word "e-mail" of Hebrew tanslation has a double quote in it.

I tried to escaping double quote.

To escape double quote,

PHP need one backslash, and javascript need one more and one another for backslash.

So I added 3 backslashes before double quote.

it shows propery on javascript alert. but on HTML backslash(for javascript) appears..

Yes, I Know using single quote can solve this simply.

But it occurs an exception among localize files(some French word uses single quote).

Can anyone help about this? Any clues welcome.


You always need to encode or escape values for the context you're embedding them in. When putting anything into HTML, you need to HTML-encode it unless you accidentally want the values interpreted as HTML. When putting anything into Javascript source code, you need to escape it properly there, for which JSON-encoding happens to be the right technique:

<?= htmlspecialchars(WORD_EMAIL, ENT_COMPAT, 'UTF-8'); ?> : <input type="text" name="email"/>
alert(<?= json_encode(WORD_EMAIL); ?> + " required.");

Also see The Great Escapism (Or: What You Need To Know To Work With Text Within Text).

I would argue that your i18n approach of pretty flawed though; "אִימֵיְיל required"* seems like a very insufficient localisation. You will want to look into proper tools like gettext and implementations/analogues of it for Javascript.

* Google translation, I don't speak Hebrew…

链接地址: http://www.djcxy.com/p/69560.html

上一篇: WordPress更改双引号的简单引号

下一篇: 在PHP和JavaScript上都希伯来语双引号