lokalize 添加快捷键标签和转换中文标点

csslayer | 2011/11/26

来源:http://shuizhuyuanluo.blog.163.com/blog/static/7781812011102693427675/

两个很简单简单的 lokalize 的脚本,实际用下来 lokalize 的扩展性是相当强大的。

要正常使用得先安装 kdebindings 里面的 pykde4 和 kross python 解释器。把下面的文件复制到工程目录下的 lokalize-scripts 文件夹中就能用了。

不熟悉 python,功能也有缺陷的,比如快捷键标签只能无脑地插入在最后面,没有考虑点点点和专有名词;而转换标点的则没有转换引号,也没有考虑点点点。

就当是抛砖引玉吧。

add-acc.rc

<KrossScripting>
    <collection comment="Tools" name="tools" text="Tools">
        <script text="添加快捷键标签" name="add-acc" file="add-acc.py" interpreter="python" />
    </collection>
</KrossScripting>

add-acc.py

# -*- coding: utf-8 -*-
import Editor

def addacc():
    if not Editor.isValid():
        return

    entry = Editor.currentEntry()
    form = Editor.currentForm()
    source = Editor.entrySource(entry, form)
    target = Editor.entryTarget(entry, form)

    if not '&' in source:
        return

    amp = source.find("&")
    keyalnum = source[amp + 1]

    if not keyalnum.isalnum():
        return

    target = target + '(&' + keyalnum.upper() + ')'
    Editor.setEntryTarget(entry, form, target)

addacc()

conv-punc.rc

<KrossScripting>
    <collection comment="Tools" name="tools" text="Tools">
        <script text="转换标点符号" name="conv-punc" file="conv-punc.py" interpreter="python" />
    </collection>
</KrossScripting>

conv-punc.py

# -*- coding: utf-8 -*-
import Editor

def convpunc():
    if not Editor.isValid():
        return

    entry = Editor.currentEntry()
    form = Editor.currentForm()
    target = Editor.entryTarget(entry, form)

    intab = u",.?!"
    outtab = u",。?!"
    table = dict((ord(a), b) for a, b in zip(intab, outtab))

    utarget = target.decode('utf-8')
    Editor.setEntryTarget(entry, form, utarget.translate(table))

convpunc()
Tags:

4 FEEDBACKS

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).

This site uses Akismet to reduce spam. Learn how your comment data is processed.