sualfred / script.embuary.info

11 stars 15 forks source link

tmdb_get_translation #68

Closed caperucitaferoz closed 3 years ago

caperucitaferoz commented 3 years ago

The language_code settings of the language-country type are actually a combination of the iso_639_1 and iso_3166_1 standards. Therefore, in this case, the translation sought is not returned as this conditional is never met https://github.com/sualfred/script.embuary.info/blob/7dc874783c355d473fdf60d9a21e97f99574840f/resources/lib/tmdb.py#L520

I propose for (Matrix and Leia):

def tmdb_get_translation(item,key,language):
    try:
        key_value_iso_639_1 = ""
        language_iso_639_1 = language[:2]
        language_iso_3166_1 = language[3:] if len(language)>3 else None

        for translation in item['translations']['translations']:
            if translation.get('iso_639_1') == language_iso_639_1 and translation['data'][key]:
                key_value = translation['data'][key]
                if key_value:
                    key_value = key_value.replace('&', '&').strip()
                    if not language_iso_3166_1 or language_iso_3166_1 == translation.get('iso_3166_1'):
                        return key_value
                    else:
                        key_value_iso_639_1 = key_value

    except Exception:
        pass

    return key_value_iso_639_1
sualfred commented 3 years ago

Sorry, I'm a little bit tired at the moment. So some languages are using 639 while others do use 3166 because of specials characters or something?

caperucitaferoz commented 3 years ago

First of all, thanks for your work and sorry for my bad English. The problem is that in your script, for the "language_code" settings, values ​​such as: es-ES, es-MX, pt-BR, pt-PT, ... are allowed and this is not the ISO 639-1 codes that returns the tmdb API, therefore the conditional of its line 520 is never met. The APi returns for the list of translations the language code (ISO 639-1) and the country code (ISO 3166-1) as you can see in the API documentation. Example: "Coming 2 America"

My code looks for the translation corresponding to the language and country selected in the settings (respecting the list of possible values ​​that you have prefixed). In the case of not finding the specific translation of the country, but if any other in the same language will also show it. Another option would have been to change its valid values ​​for "language_code" setting only valid values ​​from ISO 639-1 and use the "country_code" setting to compare with the ISO 3166-1 field returned by the API. But there are countries that do not have an age certification system and take the US or any other as a reference. Also this could lead to illogical combinations like "es-DE" for example.

sualfred commented 3 years ago

@caperucitaferoz

Ah, now I get you. You are totally right. Thanks. Since I'm in the office with restricted permissions, could you prepare a PR for me?

caperucitaferoz commented 3 years ago

OK no problem. Although I see that your last commits are only in the matrix branch, have you stopped updating the version for leia? Should I send a PR for each branch?

sualfred commented 3 years ago

Just for Matrix, thanks. I'll cherry pick the changes for Leia.