Beispiel #1
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 #2
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
    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 #4
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
    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)
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
Beispiel #7
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 #8
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 #9
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)