Skip to content

Commit

Permalink
Fix: enhance translation model (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu authored Dec 14, 2023
1 parent 21ea5e7 commit 14e8a83
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 30 deletions.
41 changes: 11 additions & 30 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from api_client import api
from constants import *
from provider_id import ProviderID
from translator import translate_text

try: # Python 2
from urllib import unquote
Expand Down Expand Up @@ -130,47 +131,27 @@ def convert_to_real_actor_names(m):
Log.Warn('Convert to real actor names error: {number} ({error})'.format(number=m.number, error=e))

@staticmethod
def translate_text(text, lang, fallback=None):
translated_text = fallback

if Prefs[KEY_TRANSLATION_MODE] == TRANSLATION_MODE_DISABLED:
Log.Warn('Translation is disabled')
return translated_text

if lang == Locale.Language.Japanese:
Log.Warn('Translation not applied to Japanese')
return translated_text

engine = Prefs[KEY_TRANSLATION_ENGINE]
params = utils.parse_table(Prefs[KEY_TRANSLATION_ENGINE_PARAMETERS], origin_key=True)

try:
translated_text = api.translate(q=text, to=lang, engine=engine,
**params).translated_text
except Exception as e:
Log.Warn('Translate error: {error}'.format(error=e))
finally:
return translated_text

def translate_movie_info(self, m, lang):
def translate_movie_info(m, lang):
mode = Prefs[KEY_TRANSLATION_MODE]

if TRANSLATION_MODE_ENUMS[mode] & \
TRANSLATION_MODE_ENUMS[TRANSLATION_MODE_TITLE]:
m.title = self.translate_text(m.title, lang=lang, fallback=m.title)
TRANSLATION_MODE_ENUMS[TRANSLATION_MODE_TITLE] and m.title:
m.title = translate_text(m.title, lang=lang, fallback=m.title)

if TRANSLATION_MODE_ENUMS[mode] & \
TRANSLATION_MODE_ENUMS[TRANSLATION_MODE_SUMMARY]:
m.summary = self.translate_text(m.summary, lang=lang, fallback=m.summary)
TRANSLATION_MODE_ENUMS[TRANSLATION_MODE_SUMMARY] and m.summary:
m.summary = translate_text(m.summary, lang=lang, fallback=m.summary)

def translate_reviews(self, reviews, lang):
@staticmethod
def translate_reviews(reviews, lang):
mode = Prefs[KEY_TRANSLATION_MODE]

if TRANSLATION_MODE_ENUMS[mode] & \
TRANSLATION_MODE_ENUMS[TRANSLATION_MODE_REVIEWS]:
for review in reviews:
review.comment = self.translate_text(review.comment, lang=lang,
fallback=review.comment)
if review.comment:
review.comment = translate_text(review.comment, lang=lang,
fallback=review.comment)

def search(self, results, media, lang, manual=False):

Expand Down
57 changes: 57 additions & 0 deletions Contents/Code/translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
import threading
import time

import utils
from api_client import api
from constants import *

# plex debugging
try:
import plexhints # noqa: F401
except ImportError:
pass
else: # the code is running outside of Plex
from plexhints.locale_kit import Locale # locale kit
from plexhints.log_kit import Log # log kit
from plexhints.prefs_kit import Prefs # prefs kit

TRANSLATOR_LOCK = threading.Lock()


def translate_text(text, lang, fallback=None):
with TRANSLATOR_LOCK:

translated_text = fallback

if not text:
Log.Warn('Translation text is empty')
return translated_text

if Prefs[KEY_TRANSLATION_MODE] == TRANSLATION_MODE_DISABLED:
Log.Warn('Translation is disabled')
return translated_text

if lang == Locale.Language.Japanese:
Log.Warn('Translation not applied to Japanese')
return translated_text

engine = Prefs[KEY_TRANSLATION_ENGINE]
params = utils.parse_table(Prefs[KEY_TRANSLATION_ENGINE_PARAMETERS], origin_key=True)

forced_lang = params.pop('to', None)
if forced_lang:
Log.Warn('Force translation language from {lang} to {forced_lang}'
.format(lang=lang, forced_lang=forced_lang))
lang = forced_lang

# limit translate request rate to 1 rps.
time.sleep(1.0)

try:
translated_text = api.translate(q=text, to=lang, engine=engine,
**params).translated_text
except Exception as e:
Log.Warn('Translate error: {error}'.format(error=e))
finally:
return translated_text

0 comments on commit 14e8a83

Please sign in to comment.