Zenoss中menu的汉化

最近在做对Zenoss的汉化工作,在汉化菜单的时候发现很多菜单的内容都是放在 souce\trunk\zenoss\Products\ZenModel\migrate\menus.py文件中,可是不管怎么改这个文件都不生效,后来终于在zenoss的开发文档中发现了奥秘

Adding a new menu item is fairly straightforward. Because menu items are persistent objects, modifications must
happen in a migrate script (or be included as XML in a ZenPack). The method ZenMenuable.buildMenus()accepts
a dictionary of menus, each of which is a list of dictionaries representing the attributes of menu items. Instructions
on writing migrate scripts can be found elsewhere in this guide


最后经总结汉化菜单的步骤如下:

1 确保menus.py文件中对应的字段的格式是_t('key')
2 确保zenoss.po中有对应的汉化
3 确保menus.py的MenuRelations类的version变量的值是当前zenoss的版本 例如4.2.4

4 确保menus.py在zenoss\Products\ZenModel\migrate\__init__.py中被导入

5 执行 zenmigrate --dont-commit,确保输出了INFO:zen.migrate:Installing MenuRelations (4.2.4)
6 执行zenmigrate


由于menus.py中有很多的菜单都没有采用_t(“key”)的格式,如果一个去添加会很麻烦,下面的脚本可以利用已有的menus.py生成一个新的文件,该文件所有的description都符合_t("key") 的格式,以及生成一个esult.txt,该文件包含了所有未在po文件中存在的空实现

__author__ = 'Eric.sunah'

#列出所有没有以_t开头的菜单,并且生成新的文件,新文件中所有的菜单都符合_t('key')格式
def i18NAllMenu(menusFile,newMenusFile):
    menus = open(menusFile)
    newmenus=open(newMenusFile,"w")
    DESC = 'description'
    no_i18N_key=[]
    for line in menus:
        if line.strip().find('\'description') != -1:
            #'description': 'Set Systems...',
            new_line = line[len(DESC):]
            if new_line.find('_t') != -1:
                newmenus.write(line)
            else:
                new_key=new_line.strip()[16:-2]
                #print(new_key)
                no_i18N_key.append(new_key)
                newmenus.write("                'description': _t('"+new_key+"'),"+"\n")
        else:
            newmenus.write(line)
    print(len(no_i18N_key))
    print(no_i18N_key)
    return no_i18N_key

#列出所有没有以_t开头的菜单,并且没有在zenoss.po中定义的key
def noI18NKeyInPO(i18Keys,poFile):
    f=open(poFile, encoding='utf-8')
    poKeys=[]
    for line in f:
        #msgid "Add to Graphs..."
        #msgstr "添加到图表..."
        if(line.strip().startswith('msgid')):
            poKeys.append(line[7:-2])
    #print(poKeys)
    nokeyi18n=[]
    resultfile=open('D:\soucecode\CorePython\zenoss\esult.txt','w')
    for no_i18key in i18Keys:
        if no_i18key not in poKeys:
            poKeys.append(no_i18key)
            nokeyi18n.append(no_i18key)
            #print(no_i18key)
            resultfile.write("msgid \""+no_i18key+"\""+"\n")
            resultfile.write("msgstr \"\"")
            resultfile.write("\n")
            resultfile.write("\n")

    print(nokeyi18n)
    print(len(nokeyi18n))


if __name__ == "__main__":
    no_i18N_key=i18NAllMenu('D:\soucecode\CorePython\zenoss\menus.py','D:\soucecode\CorePython\zenoss\ewmenus.py')
    noI18NKeyInPO(no_i18N_key,'D:\soucecode\CorePython\zenoss\zenoss.po');


你可能感兴趣的:(汉化,菜单,zenoss,Menus)