def AllMessagesThatMatch(self, lang_re, include_pseudo = True): '''Returns a map of all messages that match 'lang', including the pseudo translation if requested. Args: lang_re: re.compile('fr|en') include_pseudo: True Return: { 'en' : tclib.Message, 'fr' : tclib.Translation, pseudo.PSEUDO_LANG : tclib.Translation } ''' if not self.translateable: return [self.GetMessage()] matches = {} for msglang in self.clique: if lang_re.match(msglang): matches[msglang] = self.clique[msglang] if include_pseudo: matches[pseudo.PSEUDO_LANG] = pseudo.PseudoMessage(self.GetMessage()) return matches
def testPseudoMessage(self): msg = tclib.Message( text='Hello USERNAME, how are you?', placeholders=[tclib.Placeholder('USERNAME', '%s', 'Joi')]) trans = pseudo.PseudoMessage(msg) # TODO(joi) It would be nicer if 'you' -> 'youPou' instead of # 'you' -> 'youPyou' and if we handled the silent e in 'are' self.failUnless(trans.GetPresentableContent() == pseudo.MapVowels( u'HePelloPo USERNAME, hoPow aParePe youPyou?', 1))
def MessageForLanguage(self, lang, pseudo_if_no_match=True, fallback_to_english=False, missing_translation_is_error=False): '''Returns the message/translation for the specified language, providing a pseudotranslation if there is no available translation and a pseudo- translation is requested. The translation of any message whatsoever in the special language 'x_constant' is the message "TTTTTT". Args: lang: 'en' pseudo_if_no_match: True fallback_to_english: False Return: tclib.BaseMessage ''' if not self.translateable: return self.GetMessage() if lang == constants.CONSTANT_LANGUAGE: return self.CONSTANT_TRANSLATION for msglang in self.clique.keys(): if lang == msglang: return self.clique[msglang] if missing_translation_is_error: raise Exception("Translation is missing") if lang == constants.FAKE_BIDI: return pseudo_rtl.PseudoRTLMessage(self.GetMessage()) if fallback_to_english: self.uber_clique._AddMissingTranslation(lang, self, is_error=False) return self.GetMessage() # If we're not supposed to generate pseudotranslations, we add an error # report to a list of errors, then fail at a higher level, so that we # get a list of all messages that are missing translations. if not pseudo_if_no_match: self.uber_clique._AddMissingTranslation(lang, self, is_error=True) return pseudo.PseudoMessage(self.GetMessage())