Beispiel #1
0
    def __parse_xliff(self, contents):
        ns = {"xliff": "urn:oasis:names:tc:xliff:document:1.2"}

        tree = ElementTree.parse(self.xliff_path)
        root = tree.getroot()

        for file in root:
            localized_contents = LocalizedContents()
            localized_contents.file = file.get("original")
            localized_contents.source = file.get("source-language")
            localized_contents.target = file.get("target-language")

            header = file.find("xliff:header", ns)
            header_tool = header.find("xliff:tool", ns)
            tool = Tool()
            tool.id = header_tool.get("tool-id")
            tool.name = header_tool.get("tool-name")
            tool.version = header_tool.get("tool-version")
            tool.build = header_tool.get("build-num")
            localized_contents.tool = tool

            for trans_unit in (body := file.find("xliff:body", ns)):
                translation = Translation()
                translation.id = trans_unit.get("id")

                if (source_tag := trans_unit.find("xliff:source",
                                                  ns)) is not None:
                    translation.source = source_tag.text

                if (target_tag := trans_unit.find("xliff:target",
                                                  ns)) is not None:
                    translation.target = target_tag.text
Beispiel #2
0
class BasicTranslatableModelManagerTestCase(unittest.TestCase):
    def setUp(self):
        # Translatable
        self.model = Translatable()
        self.model.save()
        # English translation
        self.translation_en = Translation()
        self.translation_en.model = self.model
        self.translation_en.language = "en"
        self.translation_en.field = "Hello!"
        self.translation_en.save()
        # Spanish translation
        self.translation_es = Translation()
        self.translation_es.model = self.model
        self.translation_es.language = "es"
        self.translation_es.field = "Hola!"
        self.translation_es.save()

    def tearDown(self):
        self.translation_en.delete()
        self.translation_es.delete()
        self.model.delete()

    def test_translated(self):
        self.assertEquals(Translatable.objects.translated()[0], self.model)
        self.assertEquals(Translatable.objects.translated("en")[0], self.model)
        self.assertEquals(Translatable.objects.translated("es")[0], self.model)
        self.assertFalse(Translatable.objects.translated("it").exists())
Beispiel #3
0
class BasicTranslatableModelManagerTestCase(unittest.TestCase):
    def setUp(self):
        # Translatable
        self.model = Translatable()
        self.model.save()
        # English translation
        self.translation_en = Translation()
        self.translation_en.model = self.model
        self.translation_en.language = 'en'
        self.translation_en.field = "Hello!"
        self.translation_en.save()
        # Spanish translation
        self.translation_es = Translation()
        self.translation_es.model = self.model
        self.translation_es.language = 'es'
        self.translation_es.field = "Hola!"
        self.translation_es.save()

    def tearDown(self):
        self.translation_en.delete()
        self.translation_es.delete()
        self.model.delete()

    def test_translated(self):
        self.assertEquals(Translatable.objects.translated()[0], self.model)
        self.assertEquals(Translatable.objects.translated('en')[0], self.model)
        self.assertEquals(Translatable.objects.translated('es')[0], self.model)
        self.assertFalse(Translatable.objects.translated('it').exists())
Beispiel #4
0
def translate():
    """
        Makes a request to the Unbabel API and, if succeeds, inserts a new translation register in the db
        with the first status of the flow, 'requested'.

    :return: redirect the page to the homepage
    """

    DATA.update({'text': request.json.get('source-text')})
    r = requests.post(URL, data=json.dumps(DATA), headers=HEADERS)
    if r.status_code == 201:
        translation = Translation(uid=r.json()['uid'],
                                  source_language_id=1,
                                  input_text=r.json()['text'],
                                  target_language_id=2,
                                  output_text='',
                                  status_id=1)
        try:
            db.session.add(translation)
            db.session.commit()
            success = True
        except:
            db.session.rollback()
            success = False
        if success:
            return jsonify({'success': True, 'uid': r.json()['uid']}), 201
        else:
            return jsonify({'success': False}), 500
    else:
        return jsonify({'success': False}), r.status_code
Beispiel #5
0
 def setUp(self):
     # Translatable
     self.model = Translatable()
     self.model.save()
     # English translation
     self.translation_en = Translation()
     self.translation_en.model = self.model
     self.translation_en.language = "en"
     self.translation_en.field = "Hello!"
     self.translation_en.save()
     # Spanish translation
     self.translation_es = Translation()
     self.translation_es.model = self.model
     self.translation_es.language = "es"
     self.translation_es.field = "Hola!"
     self.translation_es.save()
    def inserts(self):
        """Do some initial inserts for the test"""
        en = Language(id=1, language='en')
        self.db.session.add(en)

        es = Language(id=2, language='es')
        self.db.session.add(es)

        requested = TranslationStatus(id=1, status='requested')
        self.db.session.add(requested)

        pending = TranslationStatus(id=2, status='pending')
        self.db.session.add(pending)

        translated = TranslationStatus(id=3, status='translated')
        self.db.session.add(translated)

        translation = Translation(uid='336f7c6064',
                                  source_language_id=1,
                                  input_text='hello world',
                                  target_language_id=2,
                                  output_text='hola mundo',
                                  status_id=1)
        self.db.session.add(translation)
        self.db.session.commit()
        self.db.session.close()
Beispiel #7
0
 def setUp(self):
     # Translatable
     self.model = Translatable()
     self.model.save()
     # English translation
     self.translation_en = Translation()
     self.translation_en.model = self.model
     self.translation_en.language = 'en'
     self.translation_en.field = "Hello!"
     self.translation_en.save()
     # Spanish translation
     self.translation_es = Translation()
     self.translation_es.model = self.model
     self.translation_es.language = 'es'
     self.translation_es.field = "Hola!"
     self.translation_es.save()
Beispiel #8
0
def post_request():
    data = {}
    data['source_language'] = 'en'
    data['target_language'] = 'es'
    data['source_text'] = request.form['translation_text']

    translation = Translation(
        source_text=data['source_text'],
        target_text=None,
        source_language=data['source_language'],
        target_language=data['target_language'],
        uid=None,
        status=None,
    )

    db.session.add(translation)
    db.session.commit()
    get_translation_task.delay(translation.id, data)

    return jsonify(translation.serialize())
Beispiel #9
0
    def get_translation(self):
        """Get translations from API or database."""

        existing_translation = Translation.get_by_source_word(
            self.source_code, self.translate_code, self.source_word)

        if existing_translation == None:

            api_translation = query_translation_api(self.source_word,
                                                    self.source_code,
                                                    self.translate_code)

            new_translation = Translation.add_translation(
                self.source_code, self.translate_code, self.source_word,
                api_translation)
            print(f"NEW: {new_translation}", file=sys.stderr)

            return new_translation.translated_word

        else:
            print(f"EXISTING: {existing_translation}", file=sys.stderr)
            return existing_translation.translated_word
def insert_translation(translation_dict):
    try:
        translation = Translation(translation_dict['text'],
                                  translation_dict['timestamp'],
                                  translation_dict['uid'],
                                  translation_dict['status'],
                                  translation_dict['translation'])
        db.session.add(translation)
        db.session.commit()
        return True
    except Exception as e:
        print('Failed to insert translation %s' % e)
        return False
    def __parse_translation_sheets(self, contents, wb):
        # Get metadata
        index = wb["_metadata_"]
        temp_localized_contents = []
        for i in range(4, index.max_row + 1):
            localized_contents = LocalizedContents()

            localized_contents.file = index["C" + str(i)].value
            localized_contents.source = index["D" + str(i)].value
            localized_contents.target = index["E" + str(i)].value

            tool = Tool()
            tool.id = index["F" + str(i)].value
            tool.name = index["G" + str(i)].value
            tool.version = index["H" + str(i)].value
            tool.build = index["I" + str(i)].value
            localized_contents.tool = tool

            temp_localized_contents.append(localized_contents)

        # Parse all sheets
        for sheetname in wb.sheetnames:
            if sheetname == "_metadata_":
                continue

            ws = wb[sheetname]
            row = 1

            if ws["A" + str(row)].value != "No":
                row += 1

            if ws["A" + str(row)].value != "No":
                print("Sheet format is invalidated : {}".format(sheetname))
                continue

            row += 1

            # Get localized content with metadata
            filtered_localized_contents = list(
                filter(lambda c: c.basename == sheetname,
                       temp_localized_contents))
            if filtered_localized_contents == False:
                print("Sheet metadata not found: {}".format(sheetname))
                continue

            localized_contents = filtered_localized_contents[0]

            # Add translations
            for i in range(row, ws.max_row + 1):
                translation = Translation()
                translation.id = ws["B" + str(i)].value
                translation.source = ws["C" + str(i)].value
                translation.target = ws["D" + str(i)].value
                translation.note = ws["E" + str(i)].value

                localized_contents.translations.append(translation)

            contents.localized_contents.append(localized_contents)
Beispiel #12
0
from models import db, Translation, User

app = Flask(__name__, template_folder="templates/", static_folder="templates/")
DB_URI = 'sqlite:///test.db'
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SECRET_KEY'] = 'super secret key'
app.secret_key = 'blabla'
sess = Session()
db.init_app(app)
if not database_exists(DB_URI):
    with app.app_context():
        db.drop_all()
        db.create_all()
        for i in range(1, 1000):
            db.session.add(Translation(src=f"test {i}"))
        db.session.add(User(name="Djamel"))
        db.session.add(User(name="Yahya"))
        db.session.commit()


@app.route('/choise')
def index():
    return render_template('index.html')


@app.route('/about')
def about():
    return render_template('about.html')

Beispiel #13
0
                try:
                    if entry.name in known_failures:
                        continue

                    if not entry.is_icelandic:
                        continue

                    if not entry.part_of_speech:
                        # TODO: Don't yield entries we don't understand
                        continue

                    lemma = Lemma.create(**entry.to_dict(),
                                         frequency=frequencies.get(entry.name)
                                         or 0)
                    translations = [
                        Translation(**translation, lemma=lemma)
                        for translation in entry.translations
                    ]

                    declensions = d.get_declensions(entry.name)
                    forms = [
                        Form(**declension, lemma=lemma)
                        for declension in declensions
                    ]

                    with sqldb.atomic():
                        if forms:
                            Form.bulk_create(forms)

                        if translations:
                            Translation.bulk_create(translations)
Beispiel #14
0
def storeTranslation(request, sourceText, translatedText):
    if request.user.is_authenticated():
        translation = Translation.create(request.user, sourceText, translatedText)
        translation.save()
Beispiel #15
0
 def items(self):
     return Translation.objects.filter(Translation.permission_to_view_Q())
Beispiel #16
0
class BasicTranslatableModelTestCase(unittest.TestCase):
    def setUp(self):
        # Translatable
        self.model = Translatable()
        self.model.save()
        # English translation
        self.translation_en = Translation()
        self.translation_en.model = self.model
        self.translation_en.language = "en"
        self.translation_en.field = "Hello!"
        self.translation_en.save()
        # Spanish translation
        self.translation_es = Translation()
        self.translation_es.model = self.model
        self.translation_es.language = "es"
        self.translation_es.field = "Hola!"
        self.translation_es.save()

    def tearDown(self):
        self.translation_en.delete()
        self.translation_es.delete()
        self.model.delete()

    def test_has_translations(self):
        self.assertTrue(self.model.has_translations())

    def test_has_translation_default_language(self):
        activate_language("en")
        self.assertTrue(self.model.has_translation())
        activate_language("es")
        self.assertTrue(self.model.has_translation())
        activate_language("it")
        self.assertFalse(self.model.has_translation())

    def test_has_translation_given_language(self):
        self.assertFalse(self.model.has_translation("it"))
        self.assertTrue(self.model.has_translation("en"))
        self.assertTrue(self.model.has_translation("es"))

    def test_get_translation_default_language_no_fallback(self):
        activate_language("en")
        self.assertEquals(self.model.get_translation(fallback=False), self.translation_en)
        activate_language("es")
        self.assertEquals(self.model.get_translation(fallback=False), self.translation_es)
        activate_language("it")
        with self.assertRaises(translatable.exceptions.MissingTranslation):
            self.model.get_translation(fallback=False)

    def test_get_translation_default_language_fallback(self):
        activate_language("en")
        self.assertEquals(self.model.get_translation(), self.translation_en)
        activate_language("es")
        self.assertEquals(self.model.get_translation(), self.translation_es)
        activate_language("it")
        self.assertEquals(self.model.get_translation(), self.translation_en)

    def test_get_translation_given_language_no_fallback(self):
        activate_language("es")
        self.assertEquals(self.model.get_translation("en", fallback=False), self.translation_en)
        activate_language("en")
        self.assertEquals(self.model.get_translation("es", fallback=False), self.translation_es)
        activate_language("en")
        with self.assertRaises(translatable.exceptions.MissingTranslation):
            self.model.get_translation("it", fallback=False)

    def test_get_translation_given_language_fallback(self):
        activate_language("es")
        self.assertEquals(self.model.get_translation("en"), self.translation_en)
        activate_language("en")
        self.assertEquals(self.model.get_translation("es"), self.translation_es)
        activate_language("en")
        self.assertEquals(self.model.get_translation("it"), self.translation_en)

    def test_translated_default_language_no_fallback(self):
        activate_language("en")
        self.assertEquals(self.model.translated("field", fallback=False), "Hello!")
        activate_language("es")
        self.assertEquals(self.model.translated("field", fallback=False), "Hola!")
        activate_language("it")
        self.assertIsNone(self.model.translated("field", fallback=False))
        self.assertEquals(self.model.translated("field", "default", fallback=False), "default")

    def test_translated_default_language_fallback(self):
        activate_language("en")
        self.assertEquals(self.model.translated("field"), "Hello!")
        activate_language("es")
        self.assertEquals(self.model.translated("field"), "Hola!")
        activate_language("it")
        self.assertEquals(self.model.translated("field"), "Hello!")
        self.assertEquals(self.model.translated("field", "default"), "Hello!")

    def test_translated_given_language_no_fallback(self):
        activate_language("es")
        self.assertEquals(self.model.translated("field", language="en", fallback=False), "Hello!")
        activate_language("en")
        self.assertEquals(self.model.translated("field", language="es", fallback=False), "Hola!")
        activate_language("en")
        self.assertIsNone(self.model.translated("field", language="it", fallback=False))
        self.assertEquals(self.model.translated("field", "default", language="it", fallback=False), "default")

    def test_translated_given_language_fallback(self):
        activate_language("es")
        self.assertEquals(self.model.translated("field", language="en"), "Hello!")
        activate_language("en")
        self.assertEquals(self.model.translated("field", language="es"), "Hola!")
        activate_language("es")
        self.assertEquals(self.model.translated("field", language="it"), "Hello!")
        self.assertEquals(self.model.translated("field", "default", language="it"), "Hello!")
Beispiel #17
0
 def items(self):
     return Translation.objects.filter(Translation.permission_to_view_Q())
Beispiel #18
0
class BasicTranslatableModelTestCase(unittest.TestCase):
    def setUp(self):
        # Translatable
        self.model = Translatable()
        self.model.save()
        # English translation
        self.translation_en = Translation()
        self.translation_en.model = self.model
        self.translation_en.language = 'en'
        self.translation_en.field = "Hello!"
        self.translation_en.save()
        # Spanish translation
        self.translation_es = Translation()
        self.translation_es.model = self.model
        self.translation_es.language = 'es'
        self.translation_es.field = "Hola!"
        self.translation_es.save()

    def tearDown(self):
        self.translation_en.delete()
        self.translation_es.delete()
        self.model.delete()

    def test_has_translations(self):
        self.assertTrue(self.model.has_translations())

    def test_has_translation_default_language(self):
        activate_language('en')
        self.assertTrue(self.model.has_translation())
        activate_language('es')
        self.assertTrue(self.model.has_translation())
        activate_language('it')
        self.assertFalse(self.model.has_translation())

    def test_has_translation_given_language(self):
        self.assertFalse(self.model.has_translation('it'))
        self.assertTrue(self.model.has_translation('en'))
        self.assertTrue(self.model.has_translation('es'))

    def test_get_translation_default_language_no_fallback(self):
        activate_language('en')
        self.assertEquals(self.model.get_translation(fallback=False),
                          self.translation_en)
        activate_language('es')
        self.assertEquals(self.model.get_translation(fallback=False),
                          self.translation_es)
        activate_language('it')
        with self.assertRaises(translatable.exceptions.MissingTranslation):
            self.model.get_translation(fallback=False)

    def test_get_translation_default_language_fallback(self):
        activate_language('en')
        self.assertEquals(self.model.get_translation(), self.translation_en)
        activate_language('es')
        self.assertEquals(self.model.get_translation(), self.translation_es)
        activate_language('it')
        self.assertEquals(self.model.get_translation(), self.translation_en)

    def test_get_translation_given_language_no_fallback(self):
        activate_language('es')
        self.assertEquals(self.model.get_translation('en', fallback=False),
                          self.translation_en)
        activate_language('en')
        self.assertEquals(self.model.get_translation('es', fallback=False),
                          self.translation_es)
        activate_language('en')
        with self.assertRaises(translatable.exceptions.MissingTranslation):
            self.model.get_translation('it', fallback=False)

    def test_get_translation_given_language_fallback(self):
        activate_language('es')
        self.assertEquals(self.model.get_translation('en'),
                          self.translation_en)
        activate_language('en')
        self.assertEquals(self.model.get_translation('es'),
                          self.translation_es)
        activate_language('en')
        self.assertEquals(self.model.get_translation('it'),
                          self.translation_en)

    def test_translated_default_language_no_fallback(self):
        activate_language('en')
        self.assertEquals(self.model.translated('field', fallback=False),
                          "Hello!")
        activate_language('es')
        self.assertEquals(self.model.translated('field', fallback=False),
                          "Hola!")
        activate_language('it')
        self.assertIsNone(self.model.translated('field', fallback=False))
        self.assertEquals(
            self.model.translated('field', "default", fallback=False),
            "default")

    def test_translated_default_language_fallback(self):
        activate_language('en')
        self.assertEquals(self.model.translated('field'), "Hello!")
        activate_language('es')
        self.assertEquals(self.model.translated('field'), "Hola!")
        activate_language('it')
        self.assertEquals(self.model.translated('field'), "Hello!")
        self.assertEquals(self.model.translated('field', "default"), "Hello!")

    def test_translated_given_language_no_fallback(self):
        activate_language('es')
        self.assertEquals(
            self.model.translated('field', language='en', fallback=False),
            "Hello!")
        activate_language('en')
        self.assertEquals(
            self.model.translated('field', language='es', fallback=False),
            "Hola!")
        activate_language('en')
        self.assertIsNone(
            self.model.translated('field', language='it', fallback=False))
        self.assertEquals(
            self.model.translated('field',
                                  "default",
                                  language='it',
                                  fallback=False), "default")

    def test_translated_given_language_fallback(self):
        activate_language('es')
        self.assertEquals(self.model.translated('field', language='en'),
                          "Hello!")
        activate_language('en')
        self.assertEquals(self.model.translated('field', language='es'),
                          "Hola!")
        activate_language('es')
        self.assertEquals(self.model.translated('field', language='it'),
                          "Hello!")
        self.assertEquals(
            self.model.translated('field', "default", language='it'), "Hello!")