使用gettext在pystache模板中提取关键字

时间:2014-03-24 15:54:13

标签: internationalization gettext poedit

我的项目中有html-template,它们包含pystache代码,例如

{{#_}}Word{{\_}} 

我想知道,我如何通过PoEditor解析器提取这些单词

2 个答案:

答案 0 :(得分:0)

您可以使用正则表达式来获取它们,然后删除您不想要的内容:

import re
regex=re.compile("\{\{\#\_\}\}.+\{\\\_\}\} ")
words=re.findall(regex, data) 
#To remove it use re.split or simply now searching for [A-Z].

答案 1 :(得分:0)

现在我只是使用这段代码制作一个文件,用于在PoEdit中进行扫描和提取关键字:

def makeTempLang():
    fs = getFiles('templates/')
    words = []

    regex =re.compile("\{\{\#\_\}\}(.+)\{\{/\_\}\}")

    for f in fs:
        data=open(f,'r').read()
        fwords=re.findall(regex, data)
        words.extend(fwords)

    clean = (words[4:])
    data='from core import config\n_=config.i18n\n'
    for c in clean:
        data = "%s_('%s')\n"%(data,c)
    open('locale/temp2.py','w+').write(data)

    pass

def getFiles(spath=''):
    res =[]

    arr = os.listdir(spath)

    for d in arr:
        dpath =os.path.join(spath,d)

        if d.endswith('.htm'):
            res.append(dpath)
        if os.path.isdir(dpath):
            sub=getFiles(dpath)
            if len(sub) > 0 :
                res.extend(sub)
    return res
相关问题