def test_target(self): tbxfile = tbx.tbxfile() tbxunit = tbxfile.addsourceunit("Concept") tbxunit.target = "Konsep" newfile = tbx.tbxfile.parsestring(bytes(tbxfile)) print(bytes(tbxfile)) assert newfile.findunit("Concept").target == "Konsep"
def test_setid(self): tbxfile = tbx.tbxfile() tbxunit = tbxfile.addsourceunit("Concept") tbxunit.setid("testid") newfile = tbx.tbxfile.parsestring(bytes(tbxfile)) print(bytes(tbxfile)) assert newfile.findunit("Concept").getid() == "testid"
def test_target(self): tbxfile = tbx.tbxfile() tbxunit = tbxfile.addsourceunit("Concept") tbxunit.target = "Konsep" newfile = tbx.tbxfile.parsestring(str(tbxfile)) print(str(tbxfile)) assert newfile.findunit("Concept").target == "Konsep"
def download_dictionary_ttkit(export_format, prj, lang, words): ''' Translate-toolkit builder for dictionary downloads. ''' # Use translate-toolkit for other formats if export_format == 'po': # Construct store from translate.storage.po import pofile store = pofile() # Export parameters mimetype = 'text/x-po' extension = 'po' has_lang = False # Set po file header site = Site.objects.get_current() store.updateheader( add=True, language=lang.code, x_generator='Weblate %s' % weblate.VERSION, project_id_version='%s (%s)' % (lang.name, prj.name), language_team='%s <http://%s%s>' % ( lang.name, site.domain, reverse( 'show_dictionary', kwargs={'project': prj.slug, 'lang': lang.code} ), ) ) else: # Construct store from translate.storage.tbx import tbxfile store = tbxfile() # Export parameters mimetype = 'application/x-tbx' extension = 'tbx' has_lang = True # Setup response and headers response = HttpResponse(mimetype='%s; charset=utf-8' % mimetype) filename = 'glossary-%s-%s.%s' % (prj.slug, lang.code, extension) response['Content-Disposition'] = 'attachment; filename=%s' % filename # Add words for word in words.iterator(): unit = store.UnitClass(word.source) if has_lang: unit.settarget(word.target, lang.code) else: unit.target = word.target store.addunit(unit) # Save to response store.savefile(response) return response
def test_source(self): tbxfile = tbx.tbxfile() tbxunit = tbxfile.addsourceunit("Concept") tbxunit.source = "Term" newfile = tbx.tbxfile.parsestring(str(tbxfile)) print(str(tbxfile)) assert newfile.findunit("Concept") is None assert newfile.findunit("Term") is not None
def test_source(self): tbxfile = tbx.tbxfile() tbxunit = tbxfile.addsourceunit("Concept") tbxunit.source = "Term" newfile = tbx.tbxfile.parsestring(bytes(tbxfile)) print(bytes(tbxfile)) assert newfile.findunit("Concept") is None assert newfile.findunit("Term") is not None
def download_dictionary_ttkit(export_format, prj, lang, words): ''' Translate-toolkit builder for dictionary downloads. ''' # Use translate-toolkit for other formats if export_format == 'po': # Construct store from translate.storage.po import pofile store = pofile() # Export parameters content_type = 'text/x-po' extension = 'po' has_lang = False # Set po file header store.updateheader(add=True, language=lang.code, x_generator='Weblate %s' % weblate.VERSION, project_id_version='%s (%s)' % (lang.name, prj.name), language_team='%s <%s>' % ( lang.name, get_site_url( reverse('show_dictionary', kwargs={ 'project': prj.slug, 'lang': lang.code })), )) else: # Construct store from translate.storage.tbx import tbxfile store = tbxfile() # Export parameters content_type = 'application/x-tbx' extension = 'tbx' has_lang = True # Setup response and headers response = HttpResponse(content_type='%s; charset=utf-8' % content_type) filename = 'glossary-%s-%s.%s' % (prj.slug, lang.code, extension) response['Content-Disposition'] = 'attachment; filename=%s' % filename # Add words for word in words.iterator(): unit = store.UnitClass(word.source) if has_lang: unit.settarget(word.target, lang.code) else: unit.target = word.target store.addunit(unit) # Save to response response.write(str(store)) return response
def converttbx(inputfile, outputfile, templatefile, charset=None, columnorder=None): """Reads in inputfile using tbx, converts using tbx2po, writes to outputfile """ inputstore = tbx.tbxfile(inputfile) convertor = tbx2po() outputstore = convertor.convertfile(inputstore) if len(outputstore.units) == 0: return 0 outputstore.serialize(outputfile) return 1
def test_basic(self): tbxfile = tbx.tbxfile() assert tbxfile.units == [] tbxfile.addsourceunit("Bla") assert len(tbxfile.units) == 1 newfile = tbx.tbxfile.parsestring(bytes(tbxfile)) print(bytes(tbxfile)) assert len(newfile.units) == 1 assert newfile.units[0].source == "Bla" assert newfile.findunit("Bla").source == "Bla" assert newfile.findunit("dit") is None
def test_basic(self): tbxfile = tbx.tbxfile() assert tbxfile.units == [] tbxfile.addsourceunit("Bla") assert len(tbxfile.units) == 1 newfile = tbx.tbxfile.parsestring(str(tbxfile)) print(str(tbxfile)) assert len(newfile.units) == 1 assert newfile.units[0].source == "Bla" assert newfile.findunit("Bla").source == "Bla" assert newfile.findunit("dit") is None
def download_dictionary_ttkit(export_format, prj, lang, words): """ Translate-toolkit builder for dictionary downloads. """ # Use translate-toolkit for other formats if export_format == "po": # Construct store from translate.storage.po import pofile store = pofile() # Export parameters mimetype = "text/x-po" extension = "po" has_lang = False # Set po file header store.updateheader( add=True, language=lang.code, x_generator="Weblate %s" % weblate.VERSION, project_id_version="%s (%s)" % (lang.name, prj.name), language_team="%s <%s>" % (lang.name, get_site_url(reverse("show_dictionary", kwargs={"project": prj.slug, "lang": lang.code}))), ) else: # Construct store from translate.storage.tbx import tbxfile store = tbxfile() # Export parameters mimetype = "application/x-tbx" extension = "tbx" has_lang = True # Setup response and headers response = HttpResponse(mimetype="%s; charset=utf-8" % mimetype) filename = "glossary-%s-%s.%s" % (prj.slug, lang.code, extension) response["Content-Disposition"] = "attachment; filename=%s" % filename # Add words for word in words.iterator(): unit = store.UnitClass(word.source) if has_lang: unit.settarget(word.target, lang.code) else: unit.target = word.target store.addunit(unit) # Save to response store.savefile(response) return response
def convertfile(self, thecsvfile): """converts a csvfile to a tbxfile, and returns it. uses templatepo if given at construction""" mightbeheader = True self.tbxfile = tbx.tbxfile() for thecsv in thecsvfile.units: if mightbeheader: # ignore typical header strings... mightbeheader = False if [item.strip().lower() for item in thecsv.comment, thecsv.source, thecsv.target] == \ ["comment", "original", "translation"]: continue if len(thecsv.comment.strip()) == 0 and thecsv.source.find("Content-Type:") != -1: continue term = tbx.tbxunit.buildfromunit(thecsv) # TODO: we might want to get the location or other information from CSV self.tbxfile.addunit(term)
def convertfile(self, thecsvfile): """converts a csvfile to a tbxfile, and returns it. uses templatepo if given at construction""" mightbeheader = True self.tbxfile = tbx.tbxfile() for thecsv in thecsvfile.units: if mightbeheader: # ignore typical header strings... mightbeheader = False if [item.strip().lower() for item in thecsv.comment, thecsv.source, thecsv.target] == \ ["comment", "original", "translation"]: continue if len(thecsv.comment.strip() ) == 0 and thecsv.source.find("Content-Type:") != -1: continue term = tbx.tbxunit.buildfromunit(thecsv) # TODO: we might want to get the location or other information from CSV self.tbxfile.addunit(term)
def convertfile(self, csvfile): """converts a csvfile to a tbxfile, and returns it. uses templatepo if given at construction""" mightbeheader = True self.tbxfile = tbx.tbxfile() for csvunit in csvfile.units: if mightbeheader: # ignore typical header strings... mightbeheader = False if csvunit.match_header(): continue if (len(csvunit.location.strip()) == 0 and csvunit.source.find("Content-Type:") != -1): continue term = tbx.tbxunit.buildfromunit(csvunit) # TODO: we might want to get the location or other information # from CSV self.tbxfile.addunit(term) return self.tbxfile
def test_indent(self): tbxfile = tbx.tbxfile() tbxunit = tbxfile.addsourceunit("Concept") tbxunit.setid("testid") assert (bytes(tbxfile).decode() == """<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE martif PUBLIC "ISO 12200:1999A//DTD MARTIF core (DXFcdV04)//EN" "TBXcdv04.dtd"> <martif type="TBX" xml:lang="en"> <martifHeader> <fileDesc> <sourceDesc> <p>Translate Toolkit</p> </sourceDesc> </fileDesc> </martifHeader> <text> <body> <termEntry id="testid"> <langSet xml:lang="en"><tig><term>Concept</term></tig></langSet> </termEntry> </body> </text> </martif> """)
def inittbx(inputfile, columnorder=None): return tbx.tbxfile(inputfile)
def get_storage(self): return tbxfile()