Salome HOME
merge Nizhny
[tools/eficas.git] / ts / translator.py
1
2 import copy
3 from tsparser import *
4 from Accas import *
5 from Noyau import *
6 from dicoparser import *
7
8 def normalize( theTranslation ):
9         if '_' in theTranslation:
10                 return '__'.join( theTranslation.split( '_' ) )
11         else:
12                 return theTranslation
13
14 def translate( theDicoFile, theCataFile, theTSFile, theNotTranslatedFile = '' ):
15         
16         SPECIAL = ['into', 'defaut']
17         DICO = DicoParser( theDicoFile, 'NOM', 'NOM1' )
18         PARSER = TSParser()
19         
20         def is_ok( theName, theObject ):
21                 ok = isinstance( theObject, N_ENTITE.ENTITE ) or theName in SPECIAL
22                 return ok
23
24         def name_to_attr( theName ):
25                 if theName=='into':
26                         return 'CHOIX'
27                 else:
28                         return theName.upper()
29                 
30         def dico_tr( theObject, theName, theParentObj, theParentName ):
31                 #print "dicotr:", theObject, theName, theParentObj, theParentName
32                 aTrans = ''
33                 if theName in SPECIAL:
34                         if theObject!=None:
35                                 #print theName, theObject
36                                 anAttrTr = name_to_attr( theName )
37                                 anAttr = anAttrTr + '1'
38                                 if isinstance( theObject, basestring ):
39                                         #print theParentName, theName, theObject, '=>',
40                                         aTrans = DICO.translate( theParentName, '', theObject, anAttr, anAttrTr )
41                                         #print aTrans
42                                 elif isinstance( theObject, tuple ):
43                                         #print theParentName, theName
44                                         for anItem in theObject:
45                                                 #print anItem
46                                                 if isinstance( anItem, basestring ) and '=' in anItem:
47                                                         aList = DICO.convert_to_tuples( DICO.parse_tokens( anItem ) )
48                                                         #print "   ", aList
49                                                         for aListItem in aList:
50                                                                 if isinstance( aListItem, tuple ):
51                                                                         aKey, aValue = aListItem
52                                                                         #print "     ", aValue, '=>', 
53                                                                         aTrans = DICO.translate( theParentName, '', aValue, anAttr, anAttrTr )
54                                                                         #print aTrans
55                                         #print
56                                         pass
57                                         
58                 else:
59                         #print theName, '=>',
60                         aTrans = DICO.translate( theName, 'NOM' )
61                         #print aTrans
62                         
63                 aTrans = normalize( aTrans )
64                 return '', aTrans
65
66         def sub( theObject ):
67                 aDict = {}
68                 if hasattr( theObject, 'entites' ):
69                         aDict = copy.copy( theObject.entites );
70                 for s in SPECIAL:
71                         if hasattr( theObject, s ):
72                                 aDict[s] = getattr( theObject, s )
73                 return aDict
74         
75         PARSER.check_object = is_ok
76         PARSER.translation = dico_tr
77         PARSER.sub_objects = sub
78         PARSER.parse( theCataFile, theTSFile )
79         
80         if len( theNotTranslatedFile ) > 0:
81                 PARSER.saveNotTranslated( theNotTranslatedFile )
82         
83         #print
84         #print "Translations:"
85         #for key, value in p.data[''].iteritems():
86         #       print key, "=>", value
87