Edit detail for ConvertEsToEt revision 2 of 2

1 2
Editor: edreamleo
Time: 2007/12/05 08:29:57 GMT-8
Note:

added:
**A big Aha has made the following script unnecessary.**

Indeed, g.et is no longer necessary.
Instead, by convention, g.es will translate its first argument, and leave all other arguments untranslated.  Thus, we never need to use g.es.  Examples::

    g.es('',s)  # don't translate s
    g.es('%s%s' % (s1,s2)) # translate the concatenation of s1 and s2
    g.es(s,newline=false) ; g.es(s2) # translate s1 and s2 separately on the same line.


changed:
-It appears here and also in test.leo on cvs.
-
-The code is now fully undoable, but please use caution with this script!
-The test node has a few typical cases, but this has not been extensively tested.
-All comments welcome.
The code is now fully undoable, but please use it caution!

A big Aha has made the following script unnecessary.

Indeed, g.et is no longer necessary. Instead, by convention, g.es will translate its first argument, and leave all other arguments untranslated. Thus, we never need to use g.es. Examples:

g.es('',s)  # don't translate s
g.es('%s%s' % (s1,s2)) # translate the concatenation of s1 and s2
g.es(s,newline=false) ; g.es(s2) # translate s1 and s2 separately on the same line.

Here is a preliminary script to aid in I18n of Leo. The code is now fully undoable, but please use it caution!

To use, select the following text, copy it, then do the Paste Node command:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet ekr_stylesheet?>
<leo_file>
<leo_header file_format="1" tnodes="8" max_tnode_index="8"/>
<vnodes>
<v t="ekr.20060809084033" a="V"><vh>@button cvt to g.et</vh>
<v t="ekr.20060809104405"><vh>&lt;&lt; version history &gt;&gt;</vh></v>
<v t="ekr.20060809092023"><vh>test</vh>
<v t="ekr.20060809103738"><vh>test1</vh></v>
<v t="ekr.20060809103738.1"><vh>test2</vh></v>
</v>
<v t="ekr.20060809090508" a="E"><vh>replace</vh>
<v t="ekr.20060809091749.72"><vh>&lt;&lt; handle string &gt;&gt;</vh></v>
<v t="ekr.20060809091749.73"><vh>&lt;&lt; handle g.es &gt;&gt;</vh></v>
</v>
</v>
</vnodes>
<tnodes>
<t tx="ekr.20060809084033">'''A script to convert calls to g.es to g.et, and raw strings s to g._(s)'''

__version__ = 0.2
&lt;&lt; version history &gt;&gt;

@others

trace = False # For debugging.
doReplace = True # True: actually replace the body text.

if not doReplace: g.es_print('-' * 40)
c.beginUpdate()
try:
    u = c.undoer
    undoType = 'Convert g.es'
    u.beforeChangeGroup (p,undoType)
    for p in c.currentPosition().self_and_subtree_iter():
        replace(p)
    u.afterChangeGroup(p,undoType,reportFlag=True)
finally:
    c.endUpdate()</t>
<t tx="ekr.20060809090508"># Based on leoImport.scanPythonText.
def replace(p):
    '''Replace g.es by g.et and strings s by g._(s)'''
    s = p.bodyString()
    if not s.strip(): return
    result = [] ;  i = 0 ; count = 0
    while i &lt; len(s):
        progress = j = i
        ch = s[i]
        if ch == '\n' or ch == '\r':
            i = g.skip_nl(s,i)
            result.append(s[j:i])
        elif ch == '#':
            i = g.skip_to_end_of_line(s,i)
            result.append(s[j:i])
        elif ch == '"' or ch == "'":
            &lt;&lt; handle string &gt;&gt;
        elif g.is_c_id(ch):
            if g.match_word(s,i,'g.es'):
                &lt;&lt; handle g.es &gt;&gt;
            else:
                i = g.skip_c_id(s,i)
                result.append(s[j:i])
        else:
            i += 1
            result.append(s[j:i])
        assert(progress &lt; i)
    if count:
        result = ''.join(result)
        if doReplace:
            undoData = u.beforeChangeNodeContents(p)
            p.setBodyStringOrPane(result)
            p.v.t.setDirty()
            u.afterChangeNodeContents(p,'Change Body',undoData)
        else:
            g.trace('result...\n',result)</t>
<t tx="ekr.20060809091749.72">i = g.skip_python_string(s,i)
s2 = s[j:i].strip()
result.append('g._(%s)' % s2)
if trace: g.trace('string:',s2)
count += 1</t>
<t tx="ekr.20060809091749.73">i += 4 ; k1 = i
i = g.skip_ws(s,i)
found = False
if g.match(s,i,'('):
    k2 = i
    k3 = g.skip_parens(s,i)
    if g.match(s,k3,')'):
        # Only translate if there are exactly one string in the parens.
        s2 = s[k2+1:k3]
        if (
            s2 and s2.count(',') == 0 and
            (s2.count('"') == 2 or s2.count("'") == 2)
        ):
            i = k3
            if trace: g.trace('call g.es:',g.get_line(s,i))
            result.append('g.et')
            result.append(s[k1:i])
            count += 1 ; found = True
if not found:
    result.append(s[j:i])</t>
<t tx="ekr.20060809092023"></t>
<t tx="ekr.20060809103738">def test1():

    g.es('abc')
    g.es('abc',xyz)
    g.es(xyz)</t>
<t tx="ekr.20060809103738.1">def test2():

    x = 'abc' + 'xyz'
    y = abc</t>
<t tx="ekr.20060809104405">@nocolor
@
0.1 EKR: Initial version.
0.2 EKR: Support doReplace and made script fully undoable.</t>
</tnodes>
</leo_file>

Versions:

  • 0.1 EKR: Initial version.
  • 0.2 EKR: Support doReplace and made script fully undoable.

Examples:

Here are the results of running this script on the test tree. Before:

Node 1:
    g.es('abc')
    g.es('abc',xyz)
    g.es(xyz)
Node 2:
    x = 'abc' + 'xyz'
    y = abc

After:

Node 1:
    g.et('abc')
    g.es(g._('abc'),xyz)
    g.es(xyz)
Node 2:
    x = g._('abc') + g._('xyz')
    y = abc