def RunPostSubstitutionGatherer(self, debug=False): if not self.should_load_: return root = self.GetRoot() defs = {} if hasattr(root, 'defines'): defs = root.defines xtb_file = open(self.ToRealPath(self.GetInputPath())) try: lang = xtb_reader.Parse( xtb_file, self.UberClique().GenerateXtbParserCallback(self.attrs['lang'], debug=debug), defs=defs) except: print "Exception during parsing of %s" % self.GetInputPath() raise # We special case 'he' and 'iw' because the translation console uses 'iw' # and we use 'he'. assert ( lang == self.attrs['lang'] or (lang == 'iw' and self.attrs['lang'] == 'he')), ( 'The XTB file you ' 'reference must contain messages in the language specified\n' 'by the \'lang\' attribute.')
def RunPostSubstitutionGatherer(self, debug=False): if not self.should_load_: return root = self.GetRoot() defs = getattr(root, 'defines', {}) target_platform = getattr(root, 'target_platform', '') xtb_file = open(self.ToRealPath(self.GetInputPath())) try: lang = xtb_reader.Parse( xtb_file, self.UberClique().GenerateXtbParserCallback(self.attrs['lang'], debug=debug), defs=defs, target_platform=target_platform) except: print("Exception during parsing of %s" % self.GetInputPath()) raise # Translation console uses non-standard language codes 'iw' and 'no' for # Hebrew and Norwegian Bokmal instead of 'he' and 'nb' used in Chrome. # Note that some Chrome's .grd still use 'no' instead of 'nb', but 'nb' is # always used for generated .pak files. ALTERNATIVE_LANG_CODE_MAP = {'he': 'iw', 'nb': 'no'} assert ( lang == self.attrs['lang'] or lang == ALTERNATIVE_LANG_CODE_MAP[self.attrs['lang']] ), ('The XTB file you reference must contain messages in the language ' 'specified\nby the \'lang\' attribute.')
def testParsing(self): xtb_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE translationbundle> <translationbundle lang="fr"> <translation id="5282608565720904145">Bingo.</translation> <translation id="2955977306445326147">Bongo longo.</translation> <translation id="238824332917605038">Hullo</translation> <translation id="6629135689895381486"><ph name="PROBLEM_REPORT"/> peut <ph name="START_LINK"/>utilisation excessive de majuscules<ph name="END_LINK"/>.</translation> <translation id="7729135689895381486">Hello this is another line and another and another after a blank line.</translation> </translationbundle>''') messages = [] def Callback(id, structure): messages.append((id, structure)) xtb_reader.Parse(xtb_file, Callback) self.failUnless(len(messages[0][1]) == 1) self.failUnless(messages[3][1][0]) # PROBLEM_REPORT placeholder self.failUnless(messages[4][0] == '7729135689895381486') self.failUnless( messages[4][1][7][1] == 'and another after a blank line.')
def testParsingIntoMessages(self): grd = grd_reader.Parse( StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <messages> <message name="ID_MEGA">Fantastic!</message> <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex>Joi</ex></ph></message> </messages>'''), dir='.', flexible_root=True) clique_mega = grd.children[0].GetCliques()[0] msg_mega = clique_mega.GetMessage() clique_hello_user = grd.children[1].GetCliques()[0] msg_hello_user = clique_hello_user.GetMessage() xtb_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE translationbundle> <translationbundle lang="is"> <translation id="%s">Meirihattar!</translation> <translation id="%s">Saelir <ph name="USERNAME"/></translation> </translationbundle>''' % (msg_mega.GetId(), msg_hello_user.GetId())) xtb_reader.Parse(xtb_file, grd.UberClique().GenerateXtbParserCallback('is')) self.failUnless( clique_mega.MessageForLanguage('is').GetRealContent() == 'Meirihattar!') self.failUnless( clique_hello_user.MessageForLanguage('is').GetRealContent() == 'Saelir %s')
def testParseLargeFile(self): def Callback(id, structure): pass with open(util.PathFromRoot( 'grit/testdata/generated_resources_fr.xtb')) as xtb: xtb_reader.Parse(xtb, Callback)
def testParseLargeFile(self): def Callback(id, structure): pass xtb = file(util.PathFromRoot('grit/test/data/fr.xtb')) xtb_reader.Parse(xtb, Callback) xtb.close()
def testParsingIntoMessages(self): root = util.ParseGrdForUnittest(''' <messages> <message name="ID_MEGA">Fantastic!</message> <message name="ID_HELLO_USER">Hello <ph name="USERNAME">%s<ex>Joi</ex></ph></message> </messages>''') msgs, = root.GetChildrenOfType(empty.MessagesNode) clique_mega = msgs.children[0].GetCliques()[0] msg_mega = clique_mega.GetMessage() clique_hello_user = msgs.children[1].GetCliques()[0] msg_hello_user = clique_hello_user.GetMessage() xtb_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE translationbundle> <translationbundle lang="is"> <translation id="%s">Meirihattar!</translation> <translation id="%s">Saelir <ph name="USERNAME"/></translation> </translationbundle>''' % (msg_mega.GetId(), msg_hello_user.GetId())) xtb_reader.Parse(xtb_file, msgs.UberClique().GenerateXtbParserCallback('is')) self.assertEqual('Meirihattar!', clique_mega.MessageForLanguage('is').GetRealContent()) self.failUnless( 'Saelir %s', clique_hello_user.MessageForLanguage('is').GetRealContent())
def Run(self, globopt, args): self.SetOptions(globopt) assert len( args ) == 2, "Need exactly two arguments, the XTB file and the output file" xtb_file = args[0] output_file = args[1] grd = grd_reader.Parse(self.o.input, debug=self.o.extra_verbose) grd.OnlyTheseTranslations([]) # don't load translations grd.RunGatherers(recursive=True) xtb = {} def Callback(msg_id, parts): msg = [] for part in parts: if part[0]: msg = [] break # it had a placeholder so ignore it else: msg.append(part[1]) if len(msg): xtb[msg_id] = ''.join(msg) f = file(xtb_file) xtb_reader.Parse(f, Callback) f.close() translations = [ ] # list of translations as per transl2tc.WriteTranslations for node in grd: if node.name == 'structure' and node.attrs['type'] == 'menu': assert len(node.GetCliques()) == 1 message = node.GetCliques()[0].GetMessage() translation = [] contents = message.GetContent() for part in contents: if isinstance(part, types.StringTypes): id = grit.extern.tclib.GenerateMessageId(part) if id not in xtb: print "WARNING didn't find all translations for menu %s" % node.attrs[ 'name'] translation = [] break translation.append(xtb[id]) else: translation.append(part.GetPresentation()) if len(translation): translations.append( [message.GetId(), ''.join(translation)]) f = util.WrapOutputStream(file(output_file, 'w')) transl2tc.TranslationToTc.WriteTranslations(f, translations) f.close()
def testIfNodesWithUseNameForId(self): root = util.ParseGrdForUnittest(''' <messages> <message name="ID_BINGO" use_name_for_id="true">Bingo!</message> </messages>''') msgs, = root.GetChildrenOfType(empty.MessagesNode) clique = msgs.children[0].GetCliques()[0] msg = clique.GetMessage() xtb_file = io.BytesIO(b'''<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE translationbundle> <translationbundle lang="is"> <if expr="is_linux"> <translation id="ID_BINGO">Bongo!</translation> </if> <if expr="not is_linux"> <translation id="ID_BINGO">Congo!</translation> </if> </translationbundle>''') xtb_reader.Parse(xtb_file, msgs.UberClique().GenerateXtbParserCallback('is'), target_platform='darwin') self.assertEqual('Congo!', clique.MessageForLanguage('is').GetRealContent())