예제 #1
0
파일: managers.py 프로젝트: lohrun/weblate
    def update_from_unit(self, translation, unit, pos, template=None):
        """
        Process translation toolkit unit and stores/updates database entry.
        """
        if template is None:
            src = get_source(unit)
            ctx = get_context(unit)
        else:
            src = get_target(template)
            ctx = get_context(template)
        checksum = msg_checksum(src, ctx)

        # Try getting existing unit
        from weblate.trans.models import Unit

        dbunit = None
        try:
            dbunit = self.get(translation=translation, checksum=checksum)
            force = False
        except Unit.MultipleObjectsReturned:
            # Some inconsistency (possibly race condition), try to recover
            self.filter(translation=translation, checksum=checksum).delete()
        except Unit.DoesNotExist:
            pass

        # Create unit if it does not exist
        if dbunit is None:
            dbunit = Unit(translation=translation, checksum=checksum, source=src, context=ctx)
            force = True

        # Update all details
        dbunit.update_from_unit(unit, pos, force, template)

        # Return result
        return dbunit, force
예제 #2
0
 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)
예제 #3
0
    def update_from_unit(self, translation, unit, pos):
        """
        Process translation toolkit unit and stores/updates database entry.
        """
        if hasattr(unit.source, "strings"):
            src = join_plural(unit.source.strings)
        else:
            src = unit.source
        ctx = unit.getcontext()
        checksum = msg_checksum(src, ctx)
        from weblate.trans.models import Unit

        dbunit = None
        try:
            dbunit = self.get(translation=translation, checksum=checksum)
            force = False
        except Unit.MultipleObjectsReturned:
            # Some inconsistency (possibly race condition), try to recover
            self.filter(translation=translation, checksum=checksum).delete()
        except Unit.DoesNotExist:
            pass

        if dbunit is None:
            dbunit = Unit(translation=translation, checksum=checksum, source=src, context=ctx)
            force = True

        dbunit.update_from_unit(unit, pos, force)
        return dbunit
예제 #4
0
 def test_description(self):
     unit = Unit(source="string $URL$", target="string")
     unit.__dict__["all_flags"] = Flags("placeholders:$URL$")
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Translation is missing some placeholders: $URL$",
     )
예제 #5
0
 def test_description(self):
     unit = Unit(source="string $URL$", target="string")
     unit.__dict__["all_flags"] = Flags("placeholders:$URL$")
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Following format strings are missing: $URL$",
     )
예제 #6
0
 def test_description(self):
     unit = Unit(source="string URL", target="string")
     unit.__dict__["all_flags"] = Flags("regex:URL")
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Does not match regular expression <code>URL</code>.",
     )
예제 #7
0
 def test_regexp(self):
     unit = Unit(source="string $URL$", target="string $FOO$")
     unit.__dict__["all_flags"] = Flags(r"""placeholders:r"\$[^$]*\$" """)
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Following format strings are missing: $URL$"
         "<br />Following format strings are extra: $FOO$",
     )
예제 #8
0
 def test_description(self):
     unit = Unit(source="string $URL$", target="string")
     unit.__dict__["all_flags"] = Flags("placeholders:$URL$")
     check = Check(unit=unit)
     self.assertHTMLEqual(
         self.check.get_description(check),
         """
         Following format strings are missing:
         <span class="hlcheck" data-value="$URL$">$URL$</span>
         """,
     )
예제 #9
0
    def test_fixup(self):
        unit = Unit(
            source="[My Home Page](http://example.com)",
            target="[Moje stránka] (http://example.com)",
        )

        self.assertEqual(self.check.get_fixup(unit), [(r"\] +\(", "](")])

        unit = Unit(
            source="[My Home Page](http://example.com)",
            target="[Moje stránka]",
        )

        self.assertEqual(self.check.get_fixup(unit), None)
예제 #10
0
 def test_regexp(self):
     unit = Unit(source="string $URL$", target="string $FOO$")
     unit.__dict__["all_flags"] = Flags(
         r"""placeholders:r"(\$)([^$]*)(\$)" """)
     check = Check(unit=unit)
     self.assertHTMLEqual(
         self.check.get_description(check),
         """
         Following format strings are missing:
         <span class="hlcheck" data-value="$URL$">$URL$</span>
         <br />
         Following format strings are extra:
         <span class="hlcheck" data-value="$FOO$">$FOO$</span>
         """,
     )
예제 #11
0
 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,
         )
     )
예제 #12
0
 def test_description(self):
     unit = Unit(source="string", target="I have two two lemons lemons")
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Text contains the same word twice in a row: lemons, two",
     )
예제 #13
0
def show_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)
    obj.stats.ensure_all()
    last_changes = Change.objects.prefetch().order().filter(
        translation=obj)[:10]
    user = request.user

    # Get form
    form = get_upload_form(user, obj)

    search_form = SearchForm(request.user)

    return render(
        request,
        'translation.html',
        {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj.component.project,
            'form':
            form,
            'download_form':
            DownloadForm(auto_id="id_dl_%s"),
            'autoform':
            optional_form(
                AutoForm, user, 'translation.auto', obj, obj=obj.component),
            'search_form':
            search_form,
            'replace_form':
            optional_form(ReplaceForm, user, 'unit.edit', obj),
            'bulk_state_form':
            optional_form(BulkStateForm,
                          user,
                          'translation.auto',
                          obj,
                          user=user,
                          obj=obj),
            'new_unit_form':
            NewUnitForm(user,
                        initial={'value': Unit(translation=obj, id_hash=-1)}),
            'whiteboard_form':
            optional_form(WhiteboardForm, user, 'component.edit', obj),
            'delete_form':
            optional_form(DeleteForm, user, 'translation.delete', obj,
                          obj=obj),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode(obj.get_reverse_url_kwargs()),
            'other_translations':
            prefetch_stats(Translation.objects.prefetch().filter(
                component__project=obj.component.project,
                language=obj.language).exclude(pk=obj.pk)),
            'exporters':
            list_exporters(obj),
        },
    )
예제 #14
0
 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)
예제 #15
0
 def setUp(self):
     self.unit = Unit(
         translation=Translation(
             subproject=SubProject()
         )
     )
     self.profile = Profile()
예제 #16
0
 def test_glossary_space(self):
     self.assertHTMLEqual(
         format_translation(
             "text  Hello world",
             self.component.source_language,
             glossary=[
                 Unit(source="hello",
                      target="ahoj",
                      translation=self.translation)
             ],
         )["items"][0]["content"],
         """
         text
         <span class="hlspace">
             <span class="space-space">
                 <span class="sr-only"> </span>
             </span>
             <span class="space-space">
                 <span class="sr-only"> </span>
             </span>
         </span>
         <span class="glossary-term"
             title="Glossary translation: ahoj">Hello</span>
         world
         """,
     )
예제 #17
0
 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)
예제 #18
0
 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)
예제 #19
0
 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))
예제 #20
0
 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,
         )
     )
예제 #21
0
 def test_description(self):
     unit = Unit(source="${foo}", target="${bar}", extra_flags="es-format",)
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Following format strings are missing: ${foo}<br />"
         "Following format strings are extra: ${bar}",
     )
예제 #22
0
 def test_description(self):
     unit = Unit(
         source="%(count)d", target="%(languages)d", extra_flags="python-format",
     )
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Following format strings are missing: %(count)d<br />"
         "Following format strings are extra: %(languages)d",
     )
예제 #23
0
 def do_check(self, sources, targets, translation):
     return self.check.check_target_unit(
         sources,
         targets,
         Unit(
             translation=translation,
             source=join_plural(sources),
             target=join_plural(targets),
         ),
     )
예제 #24
0
 def test_glossary_multi(self):
     self.assertHTMLEqual(
         format_translation(
             "Hello glossary",
             self.component.source_language,
             glossary=[
                 Unit(source="hello", target="ahoj", translation=self.translation),
                 Unit(
                     source="glossary", target="glosář", translation=self.translation
                 ),
             ],
         )["items"][0]["content"],
         """
         <span class="glossary-term"
             title="Glossary translation: ahoj">Hello</span>
         <span class="glossary-term"
             title="Glossary translation: glosář">glossary</span>
         """,
     )
예제 #25
0
 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",
     )
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "You need to pair up an apostrophe with another one.",
     )
예제 #26
0
 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()
예제 #27
0
 def test_description_nolocation(self):
     unit = Unit(
         source="%d %s",
         target="%s %d",
         extra_flags="python-format",
     )
     check = Check(unit=unit)
     self.assertEqual(
         self.check.get_description(check),
         "Following format strings are wrongly ordered: %d, %s",
     )
예제 #28
0
 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)
예제 #29
0
    def update_from_unit(self, translation, unit, pos):
        '''
        Process translation toolkit unit and stores/updates database entry.
        '''
        if hasattr(unit.source, 'strings'):
            src = join_plural(unit.source.strings)
        else:
            src = unit.source
        ctx = unit.getcontext()
        checksum = msg_checksum(src, ctx)

        # Try getting existing unit
        from weblate.trans.models import Unit
        dbunit = None
        try:
            dbunit = self.get(
                translation = translation,
                checksum = checksum)
            force = False
        except Unit.MultipleObjectsReturned:
            # Some inconsistency (possibly race condition), try to recover
            self.filter(
                translation = translation,
                checksum = checksum).delete()
        except Unit.DoesNotExist:
            pass

        # Create unit if it does not exist
        if dbunit is None:
            dbunit = Unit(
                translation = translation,
                checksum = checksum,
                source = src,
                context = ctx)
            force = True

        # Update all details
        dbunit.update_from_unit(unit, pos, force)

        # Return result
        return dbunit, force
예제 #30
0
 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)
예제 #31
0
 def test_glossary_format(self):
     unit = Unit()
     unit.all_flags = {"php-format"}
     self.assertHTMLEqual(
         format_translation(
             "%3$sHow",
             self.component.source_language,
             glossary=[
                 Unit(source="show",
                      target="zobrazit",
                      translation=self.translation),
             ],
             unit=unit,
         )["items"][0]["content"],
         """
         <span class="hlcheck" data-value="%3$s">
         <span class="highlight-number"></span>
         %3$s
         </span>
         How
         """,
     )
예제 #32
0
 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)
예제 #33
0
 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",
     )