def getalttrans(self, origin=None): """Returns <alt-trans> for the given origin as a list of units. No origin means all alternatives. """ translist = [] for node in self.xmlelement.iterdescendants(self.namespaced("alt-trans")): if self.correctorigin(node, origin): # We build some mini units that keep the xmlelement. This # makes it easier to delete it if it is passed back to us. newunit = base.TranslationUnit(self.source) # the source tag is optional sourcenode = node.iterdescendants(self.namespaced("source")) try: newunit.source = lisa.getText(next(sourcenode), getXMLspace(node, self._default_xml_space)) except StopIteration: pass # must have one or more targets targetnode = node.iterdescendants(self.namespaced("target")) newunit.target = lisa.getText(next(targetnode), getXMLspace(node, self._default_xml_space)) # TODO: support multiple targets better # TODO: support notes in alt-trans newunit.xmlelement = node translist.append(newunit) return translist
def extendtm(self, units, store=None, sort=True): """Extends the memory with extra unit(s). :param units: The units to add to the TM. :param store: Optional store from where some metadata can be retrieved and associated with each unit. :param sort: Optional parameter that can be set to False to supress sorting of the candidates list. This should probably only be used in :meth:`matcher.inittm`. """ if isinstance(units, base.TranslationUnit): units = [units] for candidate in (unit for unit in units if self.usable(unit)): simpleunit = base.TranslationUnit("") # We need to ensure that we don't pass multistrings futher, since # some modules (like the native Levenshtein) can't use it. if isinstance(candidate.source, multistring): if len(candidate.source.strings) > 1: simpleunit.orig_source = candidate.source simpleunit.orig_target = candidate.target simpleunit.source = str(candidate.source) simpleunit.target = str(candidate.target) else: simpleunit.source = candidate.source simpleunit.target = candidate.target # If we now only get translator comments, we don't get programmer # comments in TM suggestions (in Pootle, for example). If we get all # notes, pot2po adds all previous comments as translator comments # in the new po file simpleunit.addnote(candidate.getnotes(origin="translator")) simpleunit.fuzzy = candidate.isfuzzy() self.candidates.units.append(simpleunit) if sort: self.candidates.units.sort(key=sourcelen, reverse=self.sort_reverse)
def add_unit(uid, slang, tlang): from translate.storage import base data = request.json unit = base.TranslationUnit(data['source']) unit.target = data['target'] current_app.tmdb.add_unit(unit, slang, tlang) return ""
def update_unit(self, environ, start_response, uid, slang, tlang): start_response("200 OK", [('Content-type', 'text/plain')]) #uid = unicode(urllib.unquote_plus(uid), "utf-8") data = json.loads(environ['wsgi.input'].read(int(environ['CONTENT_LENGTH']))) unit = base.TranslationUnit(data['source']) unit.target = data['target'] self.tmdb.add_unit(unit, slang, tlang) return [""]
def add_unit(self, environ, start_response, uid, slang, tlang): start_response("200 OK", [("Content-type", "text/plain")]) # uid = unicode(urllib.unquote_plus(uid), "utf-8") data = json.loads(environ["wsgi.input"].read( int(environ["CONTENT_LENGTH"]))) unit = base.TranslationUnit(data["source"]) unit.target = data["target"] self.tmdb.add_unit(unit, slang, tlang) return [""]
def update_unit(slang, tlang, uid): """Update an existing unit.""" from translate.storage import base # FIXME: This is exactly the same code as in the add_unit() view, which # only adds units, but doesn't update them. data = request.json unit = base.TranslationUnit(data['source']) unit.target = data['target'] current_app.tmdb.add_unit(unit, slang, tlang) return ""
def test_keep_target(self): """Test that we use the target for rewriting if it exists.""" unit = base.TranslationUnit(u"blie") unit.target = u"bla" debugger = podebug.podebug(rewritestyle="xxx") unit = debugger.convertunit(unit, "") assert unit.target == u"xxxblaxxx" unit.target = u"d%d" debugger = podebug.podebug(rewritestyle="flipped") unit = debugger.convertunit(unit, "") assert unit.target == u"\u202ep%d"
def quality_check(original, string, locale, ignore): """Check for obvious errors like blanks and missing interpunction.""" if not ignore: original = lang_data.normalized_unicode(original) string = lang_data.normalized_unicode(string) unit = storage_base.TranslationUnit(original) unit.target = string checker = checks.StandardChecker(checkerconfig=checks.CheckerConfig( targetlanguage=locale.code)) warnings = checker.run_filters(unit) if warnings: # https://github.com/translate/pootle/ check_names = { 'accelerators': 'Accelerators', 'acronyms': 'Acronyms', 'blank': 'Blank', 'brackets': 'Brackets', 'compendiumconflicts': 'Compendium conflict', 'credits': 'Translator credits', 'doublequoting': 'Double quotes', 'doublespacing': 'Double spaces', 'doublewords': 'Repeated word', 'emails': 'E-mail', 'endpunc': 'Ending punctuation', 'endwhitespace': 'Ending whitespace', 'escapes': 'Escapes', 'filepaths': 'File paths', 'functions': 'Functions', 'gconf': 'GConf values', 'kdecomments': 'Old KDE comment', 'long': 'Long', 'musttranslatewords': 'Must translate words', 'newlines': 'Newlines', 'nplurals': 'Number of plurals', 'notranslatewords': 'Don\'t translate words', 'numbers': 'Numbers', 'options': 'Options', 'printf': 'printf()', 'puncspacing': 'Punctuation spacing', 'purepunc': 'Pure punctuation', 'sentencecount': 'Number of sentences', 'short': 'Short', 'simplecaps': 'Simple capitalization', 'simpleplurals': 'Simple plural(s)', 'singlequoting': 'Single quotes', 'startcaps': 'Starting capitalization', 'startpunc': 'Starting punctuation', 'startwhitespace': 'Starting whitespace', 'tabs': 'Tabs', 'unchanged': 'Unchanged', 'untranslated': 'Untranslated', 'urls': 'URLs', 'validchars': 'Valid characters', 'variables': 'Placeholders', 'xmltags': 'XML tags', } warnings_array = [] for key in warnings.keys(): warning = check_names.get(key, key) warnings_array.append(warning) return HttpResponse(json.dumps({ 'warnings': warnings_array, }), mimetype='application/json')
def test_ignore_gtk(self): """Test operation of GTK message ignoring""" unit = base.TranslationUnit("default:LTR") assert self.debug.ignore_gtk(unit)
def run_checks(original, string, locale_code, disabled_checks=None): """Check for obvious errors like blanks and missing interpunction.""" original = lang_data.normalized_unicode(original) string = lang_data.normalized_unicode(string) disabled_checks = disabled_checks or [] unit = storage_base.TranslationUnit(original) unit.target = string checker = checks.StandardChecker( checkerconfig=checks.CheckerConfig(targetlanguage=locale_code), excludefilters=disabled_checks, ) warnings = checker.run_filters(unit) if not warnings: return {} check_names = { "accelerators": "Accelerators", "blank": "Blank", "brackets": "Brackets", "compendiumconflicts": "Compendium conflict", "credits": "Translator credits", "doublequoting": "Double quotes", "doublespacing": "Double spaces", "doublewords": "Repeated word", "emails": "E-mail", "endpunc": "Ending punctuation", "endwhitespace": "Ending whitespace", "escapes": "Escapes", "filepaths": "File paths", "functions": "Functions", "long": "Long", "musttranslatewords": "Must translate words", "newlines": "Newlines", "nplurals": "Number of plurals", "notranslatewords": "Don't translate words", "numbers": "Numbers", "options": "Options", "printf": "Printf format string mismatch", "puncspacing": "Punctuation spacing", "purepunc": "Pure punctuation", "sentencecount": "Number of sentences", "short": "Short", "simplecaps": "Simple capitalization", "simpleplurals": "Simple plural(s)", "singlequoting": "Single quotes", "startcaps": "Starting capitalization", "startpunc": "Starting punctuation", "startwhitespace": "Starting whitespace", "tabs": "Tabs", "unchanged": "Unchanged", "urls": "URLs", "validchars": "Valid characters", "variables": "Placeholders", "xmltags": "XML tags", } warnings_array = [] for key in warnings.keys(): warning = check_names.get(key, key) warnings_array.append(warning) return { "ttWarnings": warnings_array, }
def run_checks(original, string, locale_code, disabled_checks=None): """Check for obvious errors like blanks and missing interpunction.""" original = lang_data.normalized_unicode(original) string = lang_data.normalized_unicode(string) disabled_checks = disabled_checks or [] unit = storage_base.TranslationUnit(original) unit.target = string checker = checks.StandardChecker( checkerconfig=checks.CheckerConfig(targetlanguage=locale_code), excludefilters=disabled_checks) warnings = checker.run_filters(unit) if not warnings: return {} check_names = { 'accelerators': 'Accelerators', 'blank': 'Blank', 'brackets': 'Brackets', 'compendiumconflicts': 'Compendium conflict', 'credits': 'Translator credits', 'doublequoting': 'Double quotes', 'doublespacing': 'Double spaces', 'doublewords': 'Repeated word', 'emails': 'E-mail', 'endpunc': 'Ending punctuation', 'endwhitespace': 'Ending whitespace', 'escapes': 'Escapes', 'filepaths': 'File paths', 'functions': 'Functions', 'long': 'Long', 'musttranslatewords': 'Must translate words', 'newlines': 'Newlines', 'nplurals': 'Number of plurals', 'notranslatewords': 'Don\'t translate words', 'numbers': 'Numbers', 'options': 'Options', 'printf': 'Printf format string mismatch', 'puncspacing': 'Punctuation spacing', 'purepunc': 'Pure punctuation', 'sentencecount': 'Number of sentences', 'short': 'Short', 'simplecaps': 'Simple capitalization', 'simpleplurals': 'Simple plural(s)', 'singlequoting': 'Single quotes', 'startcaps': 'Starting capitalization', 'startpunc': 'Starting punctuation', 'startwhitespace': 'Starting whitespace', 'tabs': 'Tabs', 'unchanged': 'Unchanged', 'untranslated': 'Untranslated', 'urls': 'URLs', 'validchars': 'Valid characters', 'variables': 'Placeholders', 'xmltags': 'XML tags', } warnings_array = [] for key in warnings.keys(): warning = check_names.get(key, key) warnings_array.append(warning) return { 'ttWarnings': warnings_array, }