Ticket #3355: t3355_translate_json_files_with_context_v1.patch

File t3355_translate_json_files_with_context_v1.patch, 2.8 KB (added by elexis, 9 years ago)

Splitting the patch into many parts now. This patch allows translating JSON files with context. It tells the translation extractor to add the context, which is given as a parameter in the binaries/data/mods/mod/l10n/messages.json file.

  • source/tools/i18n/extractors/extractors.py

     
    288288    """
    289289
    290290    def extractFromFile(self, filepath):
    291291        with codecs.open(filepath, "r", encoding='utf-8-sig') as fileObject:
    292292            lineCount = 0
    293293            for line in [line.strip("\n\r") for line in fileObject.readlines()]:
    294294                lineCount += 1
    295295                if line:
    296296                    yield line, None, None, None, lineCount, []
    297297
    298298
    299299
    300300class json(Extractor):
    301301    """ Extract messages from JSON files.
    302302    """
    303303
    304304    def __init__(self, directoryPath=None, filemasks=[], options={}):
    305305        super(json, self).__init__(directoryPath, filemasks, options)
    306306        self.breadcrumbs = []
    307307        self.keywords = self.options.get("keywords", {})
     308        self.context = self.options.get("context", None)
    308309
    309310    def setOptions(self, options):
    310311        self.options = options
    311312        self.keywords = self.options.get("keywords", {})
     313        self.context = self.options.get("context", None)
    312314
    313315    @staticmethod
    314316    def formatBreadcrumbs(breadcrumbs):
    315317        firstPiece = breadcrumbs[0]
    316318        if isinstance(firstPiece, int): outputString = "[" + str(firstPiece) + "]"
    317319        else: outputString = firstPiece
    318320        for piece in breadcrumbs[1:]:
    319321            if isinstance(piece, int): outputString += "[" + str(piece) + "]"
    320322            else: outputString += "." + piece
    321323        return outputString
    322324
    323325    def extractFromFile(self, filepath):
    324326        with codecs.open(filepath, "r", 'utf-8') as fileObject:
    325327            for message, breadcrumbs in self.extractFromString(fileObject.read()):
    326                 yield message, None, None, self.formatBreadcrumbs(breadcrumbs), -1, []
     328                yield message, None, self.context, self.formatBreadcrumbs(breadcrumbs), -1, []
    327329
    328330    def extractFromString(self, string):
    329331        self.breadcrumbs = []
    330332        jsonDocument = jsonParser.loads(string)
    331333        if isinstance(jsonDocument, list):
    332334            for message, breadcrumbs in self.parseList(jsonDocument):
    333335                if message: # Skip empty strings.
    334336                    yield message, breadcrumbs
    335337        elif isinstance(jsonDocument, dict):
    336338            for message, breadcrumbs in self.parseDictionary(jsonDocument):
    337339                if message: # Skip empty strings.
    338340                    yield message, breadcrumbs
    339341        else:
    340342            raise Exception("Unexpected JSON document parent structure (not a list or a dictionary). You must extend the JSON extractor to support it.")
    341343
    342344    def parseList(self, itemsList):
    343345        index = 0
    344346        for listItem in itemsList:
    345347            self.breadcrumbs.append(index)
    346348            if isinstance(listItem, list):