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

List:       kde-core-devel
Subject:    Re: Review Request: Add extraction scripts for translatable strings
From:       "Stephen Kelly" <steveire () gmail ! com>
Date:       2011-01-23 20:00:55
Message-ID: 20110123200055.2627.64195 () vidsolbach ! de
[Download RAW message or body]

> On Jan. 23, 2011, 4:56 p.m., Chusslove Illich wrote:
> > 

Thanks for the review!


> On Jan. 23, 2011, 4:56 p.m., Chusslove Illich wrote:
> > /home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py, \
> > line 107 <http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line107>
> >  
> > What is the difference between _() and i18n()? The example shows one to be \
> > ordinary text and the other having a placeholder, but they should be synonyms.  
> > Since _() is native Gettext convention, other synonyms in the same vein would be \
> > p_() for i18nc(), n_() for i18np(), and pn_() for i18ncp(). 
> > (Just to be sure, are translations fetched later really through KDE i18n calls? \
> > Only then should calls have i18n*() names.) 

Well, there is no i18n() function in the template system. The _() syntax is not \
really a function call, but just a syntactic marker indicating that the string should \
be extracted and run through the translation system. The _() syntax can also not take \
any arguments, so it is always ordinary text and can't handle placeholders. This is \
similar in semantics to how the django template system works. 

{{ _("translatable string") }}

is a shortcut for the 'template tag'

{% i18n "translatable string" %}

Not the use of {{ }} in one case and {% %} in the other. These are the two types of \
parsable tokens in the syntax. {{ }} is simple value substitution and {% %} can be \
anything, like value substitution in the i18n case, or more complexity in the case of \
the 'if' tag:

{% if something %}It's true{% else %}It's false{% endif %}

Arguments to tags are separated by whitespace, so as you noticed we can have 

{% i18n "The time is %1" the_time %}

But we can't do the same with _().

Because _() is not a function call that can have arguments, the additional syntaxes \
you proposed (which require arguments) don't really belong.

The translations are fetched later through the use of KLocalizedString such as \
approximately:

QString translateString(const QString &input, const QDateTime &arg)
{
  KLocalizedString ls = ki18n(input.toLatin1());
  ls.subs(arg);
  return ls.toString(d->usedLocale);
}

And that is called with approximately:

QDateTime now = getNow();
QString result = translateString("The time is %1", now);

I'm simplifying this of course for this example because I don't fully understand the \
nature of your question. I'm not certain how this affects your comment about whether \
the tag should be called i18n or not.

I generated documentation for the development version and put it online. It may \
clarify this stuff further:

http://grantlee.org/apidox_i18n/i18n_l10n.html


> On Jan. 23, 2011, 4:56 p.m., Chusslove Illich wrote:
> > /home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py, \
> > line 358 <http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line358>
> >  
> > Is it guaranteed that the order of messages in the output file is exactly the \
> > same as the order in the template file?

Yes.

Additionally, the output file contains the concatenated results from multiple output \
files. I'm not certain if this is relevant to the question.


> On Jan. 23, 2011, 4:56 p.m., Chusslove Illich wrote:
> > /home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py, \
> > line 368 <http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line368>
> >  
> > The line number in the template file should also be recorded for each extracted \
> > message, so that this comment can be 
> > // i18n: file: <filename>:<line>
> > 

Oops, Pino mentioned this too but I forgot to do it.

Updated now.


> On Jan. 23, 2011, 4:56 p.m., Chusslove Illich wrote:
> > /home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py, \
> > line 386 <http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line386>
> >  
> > This means that first the shell will glob the arguments, and then the script will \
> > glob for the second time? Now that's weird :)

Good point. I was assuming I had to do the globbing myself. Fixed now.


- Stephen


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
http://svn.reviewboard.kde.org/r/6405/#review9687
-----------------------------------------------------------


On Jan. 23, 2011, 1:24 p.m., Stephen Kelly wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> http://svn.reviewboard.kde.org/r/6405/
> -----------------------------------------------------------
> 
> (Updated Jan. 23, 2011, 1:24 p.m.)
> 
> 
> Review request for kdelibs, Pino Toscano and Chusslove Illich.
> 
> 
> Summary
> -------
> 
> The new scripts allow the kde i18n infrastructure to extract translatable strings \
> from Grantlee templates. 
> An additional line is needed in the Messages.sh script
> 
> 
> Diffs
> -----
> 
> /home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py \
> PRE-CREATION  
> Diff: http://svn.reviewboard.kde.org/r/6405/diff
> 
> 
> Testing
> -------
> 
> Works locally. The code in playground/pim/contacts can be used to test this with \
> x-test once it's checked in. 
> 
> Thanks,
> 
> Stephen
> 
> 


[Attachment #3 (text/html)]

<html>
 <body>
  <div style="font-family: Verdana, Arial, Helvetica, Sans-Serif;">
   <table bgcolor="#f9f3c9" width="100%" cellpadding="8" style="border: 1px #c9c399 \
solid;">  <tr>
     <td>
      This is an automatically generated e-mail. To reply, visit:
      <a href="http://svn.reviewboard.kde.org/r/6405/">http://svn.reviewboard.kde.org/r/6405/</a>
  </td>
    </tr>
   </table>
   <br />











<pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: \
-pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Thanks for the \
review!</pre> <br />





<blockquote style="margin-left: 1em; border-left: 2px solid #d0d0d0; padding-left: \
10px;">  <p style="margin-top: 0;">On January 23rd, 2011, 4:56 p.m., <b>Chusslove \
Illich</b> wrote:</p>  <blockquote style="margin-left: 1em; border-left: 2px solid \
#d0d0d0; padding-left: 10px;">  



<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line107" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py</a>
  <span style="font-weight: normal;">

     (Diff revision 2)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
"></pre></td>

  </tr>
 </tbody>




 
 



 <tbody>

  <tr>
    <th bgcolor="#b1ebb0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2"></font></th>  <td bgcolor="#c5ffc4" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "></pre></td>  <th \
bgcolor="#b1ebb0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">107</font></th>  <td bgcolor="#c5ffc4" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; "><span \
class="c"># This is a {{ _(&quot;translatable string&quot;) }} in the \
template.</span></pre></td>  </tr>

 </tbody>

</table>

  <pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: \
-pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">What is the difference \
between _() and i18n()? The example shows one to be ordinary text and the other \
having a placeholder, but they should be synonyms. 

Since _() is native Gettext convention, other synonyms in the same vein would be p_() \
for i18nc(), n_() for i18np(), and pn_() for i18ncp().

(Just to be sure, are translations fetched later really through KDE i18n calls? Only \
then should calls have i18n*() names.) </pre>
 </blockquote>





</blockquote>
<pre style="margin-left: 1em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Well, there \
is no i18n() function in the template system. The _() syntax is not really a function \
call, but just a syntactic marker indicating that the string should be extracted and \
run through the translation system. The _() syntax can also not take any arguments, \
so it is always ordinary text and can&#39;t handle placeholders. This is similar in \
semantics to how the django template system works. 

{{ _(&quot;translatable string&quot;) }}

is a shortcut for the &#39;template tag&#39;

{% i18n &quot;translatable string&quot; %}

Not the use of {{ }} in one case and {% %} in the other. These are the two types of \
parsable tokens in the syntax. {{ }} is simple value substitution and {% %} can be \
anything, like value substitution in the i18n case, or more complexity in the case of \
the &#39;if&#39; tag:

{% if something %}It&#39;s true{% else %}It&#39;s false{% endif %}

Arguments to tags are separated by whitespace, so as you noticed we can have 

{% i18n &quot;The time is %1&quot; the_time %}

But we can&#39;t do the same with _().

Because _() is not a function call that can have arguments, the additional syntaxes \
you proposed (which require arguments) don&#39;t really belong.

The translations are fetched later through the use of KLocalizedString such as \
approximately:

QString translateString(const QString &amp;input, const QDateTime &amp;arg)
{
  KLocalizedString ls = ki18n(input.toLatin1());
  ls.subs(arg);
  return ls.toString(d-&gt;usedLocale);
}

And that is called with approximately:

QDateTime now = getNow();
QString result = translateString(&quot;The time is %1&quot;, now);

I&#39;m simplifying this of course for this example because I don&#39;t fully \
understand the nature of your question. I&#39;m not certain how this affects your \
comment about whether the tag should be called i18n or not.

I generated documentation for the development version and put it online. It may \
clarify this stuff further:

http://grantlee.org/apidox_i18n/i18n_l10n.html
</pre>
<br />

<blockquote style="margin-left: 1em; border-left: 2px solid #d0d0d0; padding-left: \
10px;">  <p style="margin-top: 0;">On January 23rd, 2011, 4:56 p.m., <b>Chusslove \
Illich</b> wrote:</p>  <blockquote style="margin-left: 1em; border-left: 2px solid \
#d0d0d0; padding-left: 10px;">  



<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line358" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py</a>
  <span style="font-weight: normal;">

     (Diff revision 2)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
"></pre></td>

  </tr>
 </tbody>




 
 



 <tbody>

  <tr>
    <th bgcolor="#b1ebb0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2"></font></th>  <td bgcolor="#c5ffc4" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "></pre></td>  <th \
bgcolor="#b1ebb0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">358</font></th>  <td bgcolor="#c5ffc4" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; ">        <span \
class="k">for</span> <span class="n">token</span> <span class="ow">in</span> <span \
class="n">tokenize</span><span class="p">(</span><span \
class="n">template_string</span><span class="p">):</span></pre></td>  </tr>

 </tbody>

</table>

  <pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: \
-pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Is it guaranteed that \
the order of messages in the output file is exactly the same as the order in the \
template file?</pre>  </blockquote>





</blockquote>
<pre style="margin-left: 1em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Yes.

Additionally, the output file contains the concatenated results from multiple output \
files. I&#39;m not certain if this is relevant to the question.</pre> <br />

<blockquote style="margin-left: 1em; border-left: 2px solid #d0d0d0; padding-left: \
10px;">  <p style="margin-top: 0;">On January 23rd, 2011, 4:56 p.m., <b>Chusslove \
Illich</b> wrote:</p>  <blockquote style="margin-left: 1em; border-left: 2px solid \
#d0d0d0; padding-left: 10px;">  



<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line368" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py</a>
  <span style="font-weight: normal;">

     (Diff revision 2)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
"></pre></td>

  </tr>
 </tbody>




 
 



 <tbody>

  <tr>
    <th bgcolor="#b1ebb0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2"></font></th>  <td bgcolor="#c5ffc4" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "></pre></td>  <th \
bgcolor="#b1ebb0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">368</font></th>  <td bgcolor="#c5ffc4" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; ">            \
<span class="n">outputfile</span><span class="o">.</span><span \
class="n">write</span><span class="p">(</span><span class="s">&quot;// i18n: file: \
&quot;</span> <span class="o">+</span> <span class="n">template_filename</span> <span \
class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span \
class="s">&quot;</span><span class="p">)</span></pre></td>  </tr>

 </tbody>

</table>

  <pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: \
-pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">The line number in the \
template file should also be recorded for each extracted message, so that this \
comment can be

// i18n: file: &lt;filename&gt;:&lt;line&gt;
</pre>
 </blockquote>





</blockquote>
<pre style="margin-left: 1em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Oops, Pino \
mentioned this too but I forgot to do it.

Updated now.</pre>
<br />

<blockquote style="margin-left: 1em; border-left: 2px solid #d0d0d0; padding-left: \
10px;">  <p style="margin-top: 0;">On January 23rd, 2011, 4:56 p.m., <b>Chusslove \
Illich</b> wrote:</p>  <blockquote style="margin-left: 1em; border-left: 2px solid \
#d0d0d0; padding-left: 10px;">  



<table width="100%" border="0" bgcolor="white" style="border: 1px solid #C0C0C0; \
border-collapse: collapse; margin: 2px padding: 2px;">  <thead>
  <tr>
   <th colspan="4" bgcolor="#F0F0F0" style="border-bottom: 1px solid #C0C0C0; \
font-size: 9pt; padding: 4px 8px; text-align: left;">  <a \
href="http://svn.reviewboard.kde.org/r/6405/diff/2/?file=44441#file44441line386" \
style="color: black; font-weight: bold; text-decoration: \
underline;">/home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py</a>
  <span style="font-weight: normal;">

     (Diff revision 2)

    </span>
   </th>
  </tr>
 </thead>

 <tbody style="background-color: #e4d9cb; padding: 4px 8px; text-align: center;">
  <tr>

   <td colspan="4"><pre style="font-size: 8pt; line-height: 140%; margin: 0; \
"></pre></td>

  </tr>
 </tbody>




 
 



 <tbody>

  <tr>
    <th bgcolor="#b1ebb0" style="border-right: 1px solid #C0C0C0;" \
align="right"><font size="2"></font></th>  <td bgcolor="#c5ffc4" width="50%"><pre \
style="font-size: 8pt; line-height: 140%; margin: 0; "></pre></td>  <th \
bgcolor="#b1ebb0" style="border-left: 1px solid #C0C0C0; border-right: 1px solid \
#C0C0C0;" align="right"><font size="2">386</font></th>  <td bgcolor="#c5ffc4" \
width="50%"><pre style="font-size: 8pt; line-height: 140%; margin: 0; ">  <span \
class="n">files</span> <span class="o">=</span> <span class="nb">reduce</span><span \
class="p">(</span><span class="n">operator</span><span class="o">.</span><span \
class="n">add</span><span class="p">,</span> <span class="nb">map</span><span \
class="p">(</span><span class="n">glob</span><span class="o">.</span><span \
class="n">glob</span><span class="p">,</span> <span class="n">sys</span><span \
class="o">.</span><span class="n">argv</span><span class="p">[</span><span \
class="mi">1</span><span class="p">:]))</span></pre></td>  </tr>

 </tbody>

</table>

  <pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: \
-pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">This means that first \
the shell will glob the arguments, and then the script will glob for the second time? \
Now that&#39;s weird :)</pre>  </blockquote>





</blockquote>
<pre style="margin-left: 1em; white-space: pre-wrap; white-space: -moz-pre-wrap; \
white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">Good point. \
I was assuming I had to do the globbing myself. Fixed now.</pre> <br />




<p>- Stephen</p>


<br />
<p>On January 23rd, 2011, 1:24 p.m., Stephen Kelly wrote:</p>






<table bgcolor="#fefadf" width="100%" cellspacing="0" cellpadding="8" \
style="background-image: \
url('http://svn.reviewboard.kde.orgrb/images/review_request_box_top_bg.png'); \
background-position: left top; background-repeat: repeat-x; border: 1px black \
solid;">  <tr>
  <td>

<div>Review request for kdelibs, Pino Toscano and Chusslove Illich.</div>
<div>By Stephen Kelly.</div>


<p style="color: grey;"><i>Updated Jan. 23, 2011, 1:24 p.m.</i></p>




<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Description </h1>
<table width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="10" style="border: \
1px solid #b8b5a0">  <tr>
  <td>
   <pre style="margin: 0; padding: 0; white-space: pre-wrap; white-space: \
-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: \
break-word;">The new scripts allow the kde i18n infrastructure to extract \
translatable strings from Grantlee templates.

An additional line is needed in the Messages.sh script</pre>
  </td>
 </tr>
</table>


<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Testing </h1>
<table width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="10" style="border: \
1px solid #b8b5a0">  <tr>
  <td>
   <pre style="margin: 0; padding: 0; white-space: pre-wrap; white-space: \
-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: \
break-word;">Works locally. The code in playground/pim/contacts can be used to test \
this with x-test once it&#39;s checked in.</pre>  </td>
 </tr>
</table>




<h1 style="color: #575012; font-size: 10pt; margin-top: 1.5em;">Diffs</b> </h1>
<ul style="margin-left: 3em; padding-left: 0;">

 <li>/home/kde-devel/kdesvn/trunk/KDE/kdesdk/scripts/grantlee_strings_extractor.py \
<span style="color: grey">(PRE-CREATION)</span></li>

</ul>

<p><a href="http://svn.reviewboard.kde.org/r/6405/diff/" style="margin-left: \
3em;">View Diff</a></p>




  </td>
 </tr>
</table>








  </div>
 </body>
</html>



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

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