[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mozilla-i18n
Subject:    Re: message format and JavaScript
From:       Frank Yung-Fong Tang <ftang () netscape ! com>
Date:       2000-06-19 10:14:47
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


I do think using the same kind of format symbols, such as
"MyFormatStringCPlusPlus=String 1 (%1S) and String 2 (%2S)" to make it
consistence inside the property file is better. Using two different kind
of format symbols inside property file is indeed not good.

tao- let 's talk about this :)

Alec Flett wrote:

> I think it's totally crazy and hardcoded because it does not use any
> standard mechanism for formatting strings, and adds yet another
> codepath for formatting strings (in addition to the existing
> nsTextFormatter and PR_s*printf routines)...
>
> The more codepaths you add for doing the same things, the higher the
> risk of failure and the more time it takes for someone to learn Yet
> Another Way of doing the same thing...
>
> Anyway, now that nsTextFormatter is exposed from JS through the string
> bundles, I personally think that it is a more useful mechanism for
> formatting strings - it's one total call instead of 2 calls for every
> argument, and uses the same mechanism and formatting that one would
> use from C++. This also means that you can use the same formatting
> string in your string bundle from JS and from C++.
> i.e. I don't have to have
> MyFormatStringCPlusPlus=String 1 (%S) and String 2 (%S)
> MyFormatStringJS=String 1 (#1) and String 2 (#2)
>
>
>                                 Alec
>
> Yung-Fong Tang wrote:
>
>> Recently I saw some bug have problem of formatting string inside
>> JavaScript. Here is one paper I put down for the solution. This
>> method have been used by Bill Law as I described in the end of this
>> document.
>>
>> tao- do we have this kind of document in our l10n guideline or not?
>> If not, please put it into our l10n guide
>> tonyr@fbdesigns.com- you may want to use this to solve  Bugzilla bug
>> 39004
>> alecf- I have no idea why you think law's idea is "totally crazy and
>> hardcoded into his
>> progress dialog code" at all. It is just two JavaScript calls per
>> argument. There are nothing wrong with it. It might be slower but
>> all it need is standard JavaScript support and can be easily used in
>> the context while StringBundle is not present (for example, a web
>> site)
>>
>> file:///C|/msgfmt.html
>>
>> Message Formatting in JavaScript
>>
>> Author: Frank Tang ftang@netscape.com
>>
>> Explanation
>>
>> Assume we need to mix two variable with one message. The data is
>> data1 and data2. In different language, data1 and data2 may need to
>> be localized into different position, and more important, in
>> different order. We can use the following JavaScript technique to
>> perform it.
>>
>> var data1= "a book";
>> var data2= "MIT";
>>
>> Let's assume the English message should read as "This is a book
>> published by MIT.", probably most people will write it as the
>> following JavaScript.
>>
>> "This is " + data1 + " published by " + data2 + ".".
>>
>> Many people probably will extract "This is " , " published by " and
>> "." into string bundle as 3 separated string. This won't work
>> because there are cases that the order of data1 and data2 may need
>> to be swapped in some languages. This is even true if you have only
>> one data instead of two in this example.
>>
>> In order to make it correctly localizable, you need to put the whole
>> sentence into one string in the string bundle instead separate into
>> two or more string. You should put the string into string bundle as
>>
>> msg = "This is #1 published by #2." ; // assume we get it from string bundle here
>>
>> Then you need to perform some magic message formatting as the
>> following by using the regular expression in JavaScript before you
>> use the message.
>>
>> msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
>> msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2
>>
>> These two lines of JavaScript code will format the message as
>>
>> "This is a book published by MIT.",
>>
>> When we localized this string into a fake localization language, it
>> may read as
>>
>> "#2 published #1 is this."
>>
>> and the JavaScript code will format it as
>>
>> "MIT published a book is this.".
>>
>> ---------------------------------------------------------------------
>>
>> JavaScript Code
>>
>> var data1= "a book";
>> var data2= "MIT";
>> var msg = "This is #1 published by #2.";     // This is from the English string bundle
>>
>> msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
>> msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2
>>
>> document.write("<H3>English text:<H3>
>> document.write(msg);
>>
>> var msg = "#2 published #1 is this."; // This is from the localized string bundle
>> msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
>> msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2
>>
>> document.write("<H3>Fake localized text<H3>
>> document.write(msg);
>>
>> JavaScript Output
>>
>> Credit:
>>
>> This method is original developed by Bill Law law@netscape.com in
>> the mozilla/xpfe/components/xfer/resources/downloadProgress.js#82 as
>> the function replaceInsert().
>

[Attachment #5 (text/html)]

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
I do think using the same kind of format symbols, such as \
"MyFormatStringCPlusPlus=String 1 (%1S) and String 2 (%2S)" to make it consistence \
inside the property file is better. Using two different kind of format symbols inside \
property file is indeed not good.
<p>tao- let 's talk about this :)
<p>Alec Flett wrote:
<blockquote TYPE=CITE>I think it's totally crazy and hardcoded because
it does not use any standard mechanism for formatting strings, and adds
yet another codepath for formatting strings (in addition to the existing
nsTextFormatter and PR_s*printf routines)...
<p>The more codepaths you add for doing the same things, the higher the
risk of failure and the more time it takes for someone to learn Yet Another
Way of doing the same thing...
<p>Anyway, now that nsTextFormatter is exposed from JS through the string
bundles, I personally think that it is a more useful mechanism for formatting
strings - it's one total call instead of 2 calls for every argument, and
uses the same mechanism and formatting that one would use from C++. This
also means that you can use the same formatting string in your string bundle
from JS and from C++.
<br>i.e. I don't have to have
<br>MyFormatStringCPlusPlus=String 1 (%S) and String 2 (%S)
<br>MyFormatStringJS=String 1 (#1) and String 2 (#2)
<br>&nbsp;
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp \
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 Alec
<p>Yung-Fong Tang wrote:
<blockquote TYPE=CITE>Recently I saw some bug have problem of formatting
string inside JavaScript. Here is one paper I put down for the solution.
This method have been used by Bill Law as I described in the end of this
document.
<p>tao- do we have this kind of document in our l10n guideline or not?
If not, please put it into our l10n guide
<br>tonyr@fbdesigns.com- you may want to use this to solve&nbsp; <a \
href="http://bugzilla.mozilla.org/show_bug.cgi?id=39004">Bugzilla bug 39004</a>
<br>alecf- I have no idea why you think law's idea is "totally crazy and
hardcoded into his
<br>progress dialog code" at all. It is just two JavaScript calls per argument.
There are nothing wrong with it. It might be slower but all it need is
standard JavaScript support and can be easily used in the context while
StringBundle is not present (for example, a web site)
<p><a href="file:///C|/msgfmt.html">file:///C|/msgfmt.html</a>
<h1>
Message Formatting in JavaScript</h1>
Author: Frank Tang <a href="mailto:ftang@netscape.com">ftang@netscape.com</a>
<h2>
Explanation</h2>
Assume we need to mix two variable with one message. The data is <b>data1</b>
and <b>data2</b>. In different language, data1 and data2 may need to be
localized into different position, and more important, in different order.
We can use the following JavaScript technique to perform it.
<pre>var data1= "a book";
var data2= "MIT";</pre>
Let's assume the English message should read as "This is <b>a book</b>
published by <b>MIT</b>.", probably most people will write it as the following
JavaScript.
<pre>"This is " + <b>data1</b> + " published by " + <b>data2</b> + ".".</pre>
Many people probably will extract "This is " , " published by " and "."
into string bundle as 3 separated string. This won't work because there
are cases that the order of <b>data1</b> and <b>data2</b> may need to be
swapped in some languages. This is even true if you have only one data
instead of two in this example.
<p>In order to make it correctly localizable, you need to put the whole
sentence into one string in the string bundle instead separate into two
or more string. You should put the string into string bundle as
<pre>msg = "This is #1 published by #2." ; // assume we get it from string bundle \
here</pre> Then you need to perform some magic message formatting as the following
by using the regular expression in JavaScript before you use the message.
<pre>msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2</pre>
These two lines of JavaScript code will format the message as
<pre>"This is <b>a book</b> published by <b>MIT</b>.",</pre>
When we localized this string into a <i><u>fake localization language</u></i>,
it may read as
<pre>"#2 published #1 is this."</pre>
and the JavaScript code will format it as
<pre>"<b>MIT</b> published <b>a book</b> is this.".</pre>

<hr>
<h2>
JavaScript Code</h2>

<pre>var data1= "a book";
var data2= "MIT";
var msg = "This is #1 published by #2.";&nbsp;&nbsp;&nbsp;&nbsp; // This is from the \
English string bundle&nbsp;

msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2

document.write("&lt;H3>English text:&lt;H3>
document.write(msg);

var msg = "#2 published #1 is this."; // This is from the localized string bundle
msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2

document.write("&lt;H3>Fake localized text&lt;H3>
document.write(msg);</pre>

<h2>
JavaScript Output<script>
var data1= "a book";
var data2= "MIT";
var msg = "This is #1 published by #2.";     // This is from the English string \
bundle 

msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2

document.write("<H3>English text:</H3>");
document.write(msg);

var msg = "#2 published #1 is this."; // This is from the localized string bundle
msg = msg.replace(eval("/#1/"), data1); // replace #1 with data1
msg = msg.replace(eval("/#2/"), data2); // replace #2 with data2

document.write("<H3>Fake localized text</H3>");
document.write(msg);

</script>
</h2>

<h2>
Credit:</h2>
This method is original developed by Bill Law <a \
href="mailto:law@netscape.com">law@netscape.com</a> in the <a \
href="http://lxr.mozilla.org/seamonkey/source/xpfe/components/xfer/resources/downloadProgress.js">mozilla/xpfe/components/xfer/resources/downloadProgress.js#82
 </a>as
the function replaceInsert().</blockquote>
</blockquote>
</html>


["ftang.vcf" (text/x-vcard)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic