Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to get native language names from the list? #22

Closed
mkinney opened this issue Mar 14, 2022 · 6 comments
Closed

Any way to get native language names from the list? #22

mkinney opened this issue Mar 14, 2022 · 6 comments

Comments

@mkinney
Copy link

mkinney commented Mar 14, 2022

For instance, in the README it shows this snippet:

for language in translator.get_source_languages():
    print(f"{language.code} ({language.name})")  # Example: "DE (German)"

It would be wonderful if the "native" translation of their language could be shown, too.

In other words, "DE (German)" would also print "Deutsch". Most of the languages I can figure out, but not really sure what the "ZH" equivalent would be.

I'm using the free api level, so I'm hoping that could be added to that level.

@daniel-jones-deepl
Copy link
Member

daniel-jones-deepl commented Mar 14, 2022

Hi @mkinney, thanks for the question.

We don't have this information in the languages list. You could instead use the translator itself to translate the native language name:

Edited to add: warning, this approach to determine native names does not always yield correct results, see below.

for language in translator.get_source_languages():
    # Handle source language codes that are not accepted as target languages
    target_lang = "EN-US" if language.code == "EN" else ("PT-PT" if language.code == "PT" else language.code)
    native_name = translator.translate_text(language.name, source_lang="EN", target_lang=target_lang)
    print(f"{language.code} ({language.name}) - {native_name}")  # Example: "ZH (Chinese) - 中国"

Edited to add: After reviewing this code above, it might not work correctly in some cases. DeepL translates best when the input text includes context; translating singular words may lead to mistakes. The example above for Chinese "中国" has a closer meaning to "China" (the country). This approach might have similar problems for other languages due to the lack of context.

@mkinney
Copy link
Author

mkinney commented Mar 14, 2022

That's great!

Do you want a PR to update the README example with that updated code snippet?

@daniel-jones-deepl
Copy link
Member

I edited the message above after reviewing: the approach above is wrong in some cases e.g. Chinese. As the approach is flawed I would not add it to the README.

Coming back to your original question, it seems we don't have an easy way to get native language names. Some languages and/or UI frameworks (e.g. .NET) include a locale framework that defines native language names, but unfortunately after a cursory search I could not find something suitable in Python.

@mkinney
Copy link
Author

mkinney commented Mar 17, 2022

Ok. I would be content with the list of languages in native language somewhere.

For context, this is what I’m using as a language picker in a list.

@daniel-jones-deepl
Copy link
Member

Hi again, I do not have an update regarding our API, however this StackOverflow post might provide a good solution using the ICU Python package: https://stackoverflow.com/questions/10405915/how-to-get-the-language-name-in-native-language.

@daniel-jones-deepl
Copy link
Member

Hi @mkinney, the team discussed this topic: we won't add the native language names. Various platforms offer alternatives you can use to get the native names, for example ICU Python package above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants