def check_unit(self, nplurals=3, template=None, source_info=None, **kwargs): if nplurals == 3: equation = "n==0 ? 0 : n==1 ? 1 : 2" else: equation = "0" lang = Language.objects.create(code="zz") plural = Plural.objects.create( language=lang, number=nplurals, equation=equation ) project = Project(slug="test", source_language=Language.objects.get(code="en")) component = Component( slug="comp", project=project, file_format="xliff", template=template ) translation = Translation(language=lang, component=component, plural=plural) # Fake file format to avoid need for actual files translation.store = EmptyFormat(BytesIOMode("", b"")) unit = Unit(translation=translation, id_hash=-1, **kwargs) if source_info: for key, value in source_info.items(): setattr(unit, key, value) unit.get_comments = fake_get_comments unit.__dict__["suggestions"] = [ Suggestion(target="Weblate translator suggestion") ] else: unit.get_comments = empty_get_comments exporter = self.get_exporter(lang, translation=translation) exporter.add_unit(unit) return self.check_export(exporter)
def check_unit(self, nplurals=3, template=None, **kwargs): if nplurals == 3: equation = 'n==0 ? 0 : n==1 ? 1 : 2' else: equation = '0' lang = Language.objects.create( code='zz', ) plural = Plural.objects.create( language=lang, number=nplurals, equation=equation ) project = Project( slug='test', source_language=Language.objects.get(code='en'), ) component = Component( slug='comp', project=project, file_format='xliff', template=template ) translation = Translation( language=lang, component=component, plural=plural, ) # Fake file format to avoid need for actual files translation.store = EmptyFormat(BytesIOMode('', b'')) unit = Unit(translation=translation, id_hash=-1, **kwargs) exporter = self.get_exporter(lang, translation=translation) exporter.add_unit(unit) return self.check_export(exporter)
def check_unit(self, nplurals=3, template=None, source_info=None, **kwargs): if nplurals == 3: formula = "n==0 ? 0 : n==1 ? 1 : 2" else: formula = "0" lang = Language.objects.create(code="zz") plural = Plural.objects.create(language=lang, number=nplurals, formula=formula) project = Project(slug="test") component = Component( slug="comp", project=project, file_format="xliff", template=template, source_language=Language.objects.get(code="en"), ) translation = Translation(language=lang, component=component, plural=plural) # Fake file format to avoid need for actual files translation.store = EmptyFormat(BytesIOMode("", b"")) unit = Unit(translation=translation, id_hash=-1, **kwargs) if source_info: for key, value in source_info.items(): setattr(unit, key, value) # The dashes need special handling in XML based formats unit.__dict__["unresolved_comments"] = [ Comment(comment="Weblate translator comment ---- ") ] unit.__dict__["suggestions"] = [ Suggestion(target="Weblate translator suggestion") ] else: unit.__dict__["unresolved_comments"] = [] unit.source_unit = unit exporter = self.get_exporter(lang, translation=translation) exporter.add_unit(unit) return self.check_export(exporter)
def test_non_format_singular_named(self): language = Language.objects.get(code="cs") translation = Translation(language=language, plural=language.plural) unit = Unit(translation=translation) self.assertFalse( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["%(count)s jablko", "%(count)s jablka", "%(count)s jablek"], unit, ) ) self.assertFalse( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablka", "%(count)s jablek"], unit, ) ) self.assertTrue( self.check.check_target_unit( ["One apple", "%(count)s apples"], ["Jedno jablko", "jablka", "%(count)s jablek"], unit, ) )
def test_non_format_singular(self): czech = Language.objects.get(code="cs") translation = Translation(language=czech, plural=czech.plural) unit = Unit(translation=translation) self.assertFalse( self.check.check_target_unit( ["One apple", "%d apples"], ["%d jablko", "%d jablka", "%d jablek"], unit, ) ) self.assertFalse( self.check.check_target_unit( ["One apple", "%d apples"], ["Jedno jablko", "%d jablka", "%d jablek"], unit, ) ) self.assertTrue( self.check.check_target_unit( ["One apple", "%d apples"], ["Jedno jablko", "jablka", "%d jablek"], unit, ) )
def setUp(self): self.unit = Unit( translation=Translation( subproject=SubProject() ) ) self.profile = Profile()
def check_unit(self, nplurals=3, **kwargs): if nplurals == 3: equation = 'n==0 ? 0 : n==1 ? 1 : 2' else: equation = '0' lang = Language.objects.create( code='zz', ) plural = Plural.objects.create( language=lang, number=nplurals, equation=equation ) project = Project( slug='test', source_language=Language.objects.get(code='en'), ) component = Component(slug='comp', project=project) unit = Unit( translation=Translation( language=lang, component=component, plural=plural, ), id_hash=-1, **kwargs ) exporter = self.get_exporter(lang) exporter.add_unit(unit) return self.check_export(exporter)
def validate_render_component(value, translation=None, **kwargs): from weblate.trans.models import Project, Component, Translation from weblate.lang.models import Language component = Component( project=Project( name='project', slug='project', id=-1, ), name='component', slug='component', branch='master', vcs='git', id=-1, ) if translation: kwargs['translation'] = Translation( id=-1, component=component, language_code='xx', language=Language(name='xxx', code='xx'), ) else: kwargs['component'] = component validate_render(value, **kwargs)
def test_arabic(self): arabic = Language.objects.get(code="ar") translation = Translation(language=arabic, plural=arabic.plural) unit = Unit(translation=translation) # Singular, correct format string self.assertFalse( self.check.check_target_unit(["hello %s"], ["hell %s"], unit)) # Singular, missing format string self.assertTrue( self.check.check_target_unit(["hello %s"], ["hell"], unit)) # Plural, correct format string self.assertFalse( self.check.check_target_unit(["hello %s"] * 2, ["hell %s"] * 6, unit)) # Plural, missing format string self.assertTrue( self.check.check_target_unit(["hello %s"] * 2, ["hell"] * 6, unit)) # Plural, correct format string (missing on single value plurals) self.assertFalse( self.check.check_target_unit(["hello %s"] * 2, ["hell"] * 3 + ["hello %s"] * 3, unit)) # Plural, missing format string on multi value plural self.assertTrue( self.check.check_target_unit(["hello %s"] * 2, ["hell"] * 4 + ["hello %s"] * 2, unit))
def check_unit(self, nplurals=3, **kwargs): if nplurals == 3: equation = 'n==0 ? 0 : n==1 ? 1 : 2' else: equation = '0' lang = Language( code='zz', nplurals=nplurals, pluralequation=equation ) project = Project( slug='test', source_language=Language.objects.get(code='en'), ) subproject = SubProject(slug='comp', project=project) unit = Unit( translation=Translation( language=lang, subproject=subproject ), **kwargs ) exporter = self.get_exporter(lang) exporter.add_unit(unit) return self.check_export(exporter)
def test_french_singular(self): language = Language.objects.get(code="fr") translation = Translation(language=language, plural=language.plural) self.assertFalse( self.do_check( ["One apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablek"], translation, )) self.assertFalse( self.do_check( ["%(count)s apple", "%(count)s apples"], ["%(count)s jablko", "%(count)s jablek"], translation, )) self.assertFalse( self.do_check( ["One apple", "%(count)s apples"], ["%(count)s jablko", "%(count)s jablek"], translation, )) self.assertFalse( self.do_check( ["%(count)s apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablek"], translation, ))
def test_non_format_singular_named_kab(self): language = Language.objects.get(code="kab") translation = Translation(language=language, plural=language.plural) self.assertFalse( self.do_check( ["One apple", "%(count)s apples"], ["Jedno jablko", "%(count)s jablka", "%(count)s jablek"], translation, ))
def setUp(self): self.unit = Unit(translation=Translation( component=Component( project=Project(source_language=Language(), slug="p", name="p"), slug="c", name="c", ), language=Language(), )) self.profile = Profile()
def check_unit(self, nplurals=3, template=None, source_info=None, **kwargs): if nplurals == 3: equation = 'n==0 ? 0 : n==1 ? 1 : 2' else: equation = '0' lang = Language.objects.create(code='zz', ) plural = Plural.objects.create(language=lang, number=nplurals, equation=equation) project = Project( slug='test', source_language=Language.objects.get(code='en'), ) component = Component(slug='comp', project=project, file_format='xliff', template=template) translation = Translation( language=lang, component=component, plural=plural, ) # Fake file format to avoid need for actual files translation.store = EmptyFormat(BytesIOMode('', b'')) unit = Unit(translation=translation, id_hash=-1, **kwargs) if source_info: for key, value in source_info.items(): setattr(unit, key, value) unit.get_comments = fake_get_comments unit.__dict__['suggestions'] = [ Suggestion(target='Weblate translator suggestion') ] else: unit.get_comments = empty_get_comments exporter = self.get_exporter(lang, translation=translation) exporter.add_unit(unit) return self.check_export(exporter)
def check_unit(self, nplurals=3, **kwargs): lang = Language(code='zz', nplurals=nplurals) project = Project( slug='test', source_language=Language.objects.get(code='en'), ) subproject = SubProject(slug='comp', project=project) unit = Unit(translation=Translation(language=lang, subproject=subproject), **kwargs) exporter = self.get_exporter() exporter.add_unit(unit) return self.check_export(exporter)
def test_description(self): unit = Unit( source="string", target="I have two two lemons lemons", translation=Translation( language=Language("cs"), component=Component(source_language=Language("en")), ), ) check = Check(unit=unit) self.assertEqual( self.check.get_description(check), "Text contains the same word twice in a row: lemons, two", )
def test_description(self): unit = Unit( source="{0}''s brush is {1} centimeters tall", target="{0}'s brush is {1} centimeters tall", extra_flags="java-messageformat", translation=Translation( component=Component( file_format="auto", source_language=Language("en"), ), language=Language("cs"), ), ) check = Check(unit=unit) self.assertEqual( self.check.get_description(check), "You need to pair up an apostrophe with another one.", )
def test_non_format_singular(self): czech = Language.objects.get(code="cs") translation = Translation(language=czech, plural=czech.plural) self.assertFalse( self.do_check( ["One apple", "%d apples"], ["%d jablko", "%d jablka", "%d jablek"], translation, )) self.assertFalse( self.do_check( ["One apple", "%d apples"], ["Jedno jablko", "%d jablka", "%d jablek"], translation, )) self.assertTrue( self.do_check( ["One apple", "%d apples"], ["Jedno jablko", "jablka", "%d jablek"], translation, ))
def validate_render_component(value, translation=None, **kwargs): from weblate.lang.models import Language from weblate.trans.models import Component, Project, Translation component = Component( project=Project(name="project", slug="project", id=-1), name="component", slug="component", branch="main", vcs="git", id=-1, ) if translation: kwargs["translation"] = Translation( id=-1, component=component, language_code="xx", language=Language(name="xxx", code="xx"), ) else: kwargs["component"] = component validate_render(value, **kwargs)
def download_translation_file( request, translation: Translation, fmt: Optional[str] = None, query_string: Optional[str] = None, ): if fmt is not None: try: exporter_cls = EXPORTERS[fmt] except KeyError: raise Http404("File format not supported") if not exporter_cls.supports(translation): raise Http404("File format not supported") exporter = exporter_cls(translation=translation) units = translation.unit_set.prefetch_full().order_by("position") if query_string: units = units.search(query_string).distinct() exporter.add_units(units) response = exporter.get_response( "{{project}}-{0}-{{language}}.{{extension}}".format( translation.component.slug)) else: # Force flushing pending units try: translation.commit_pending("download", None) except WeblateLockTimeout: report_error(cause="Download commit") filenames = translation.filenames if len(filenames) == 1: extension = ( os.path.splitext(translation.filename)[1] or f".{translation.component.file_format_cls.extension()}") if not os.path.exists(filenames[0]): raise Http404("File not found") # Create response response = FileResponse( open(filenames[0], "rb"), content_type=translation.component.file_format_cls.mimetype(), ) else: extension = ".zip" response = zip_download( translation.get_filename(), filenames, translation.full_slug.replace("/", "-"), ) # Construct filename (do not use real filename as it is usually not # that useful) project_slug = translation.component.project.slug component_slug = translation.component.slug language_code = translation.language.code filename = f"{project_slug}-{component_slug}-{language_code}{extension}" # Fill in response headers response["Content-Disposition"] = f"attachment; filename={filename}" if translation.stats.last_changed: response["Last-Modified"] = http_date( mktime(translation.stats.last_changed.timetuple())) return response
def setUp(self): self.unit = Unit(translation=Translation(component=Component())) self.profile = Profile()