def Translate(self,
                  lang,
                  pseudo_if_not_available=True,
                  skeleton_gatherer=None,
                  fallback_to_english=False):
        if len(self.skeleton_) == 0:
            raise exception.NotReady()
        if skeleton_gatherer:
            assert len(skeleton_gatherer.skeleton_) == len(self.skeleton_)

        out = []
        for ix in range(len(self.skeleton_)):
            if isinstance(self.skeleton_[ix], six.string_types):
                if skeleton_gatherer:
                    # Make sure the skeleton is like the original
                    assert (isinstance(skeleton_gatherer.skeleton_[ix],
                                       six.string_types))
                    out.append(skeleton_gatherer.skeleton_[ix])
                else:
                    out.append(self.skeleton_[ix])
            else:
                if skeleton_gatherer:  # Make sure the skeleton is like the original
                    assert (not isinstance(skeleton_gatherer.skeleton_[ix],
                                           six.string_types))
                msg = self.skeleton_[ix].MessageForLanguage(
                    lang, pseudo_if_not_available, fallback_to_english)

                def MyEscape(text):
                    return self.Escape(text)

                text = msg.GetRealContent(escaping_function=MyEscape)
                out.append(text)
        return ''.join(out)
Esempio n. 2
0
 def Translate(self,
               lang,
               pseudo_if_not_available=True,
               skeleton_gatherer=None,
               fallback_to_english=False):
     if not self.have_parsed_:
         raise exception.NotReady()
     self.lang_ = lang
     self.pseudo_if_not_available_ = pseudo_if_not_available
     self.fallback_to_english_ = fallback_to_english
     tree = self.ProcessElement(False, [], '', self.original_tree_)
     return json.dumps(tree, sort_keys=True, indent=2)
Esempio n. 3
0
    def Translate(self,
                  lang,
                  pseudo_if_not_available=True,
                  skeleton_gatherer=None,
                  fallback_to_english=False):
        '''Returns this document with translateable messages filled with
    the translation for language 'lang'.

    Args:
      lang: 'en'
      pseudo_if_not_available: True

    Return:
      'ID_THIS_SECTION TYPE\n...BEGIN\n  "Translated message"\n......\nEND

    Raises:
      grit.exception.NotReady() if used before Parse() has been successfully
      called.
      grit.exception.NoSuchTranslation() if 'pseudo_if_not_available' is false
      and there is no translation for the requested language.
    '''
        if len(self.skeleton_) == 0:
            raise exception.NotReady()

        # TODO(joi) Implement support for skeleton gatherers here.

        out = []
        for item in self.skeleton_:
            if isinstance(item, types.StringTypes):
                out.append(item)
            else:
                msg = item.MessageForLanguage(lang, pseudo_if_not_available,
                                              fallback_to_english)
                for content in msg.GetContent():
                    if isinstance(content, tclib.Placeholder):
                        out.append(content.GetOriginal())
                    else:
                        # We escape " characters to increase the chance that attributes
                        # will be properly escaped.
                        out.append(util.EscapeHtml(content, True))

        return ''.join(out)