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