如何使用Python翻译文本?

huangapple go评论54阅读模式
英文:

how to translate the texts with python?

问题

I have translated the code part you provided:

我正在尝试找到一种使用else if翻译一些文本的方法但我找不到如何做的方式
我尝试翻译的是代码的以下部分

rarity = {api["rarity"]}
if rarity:
    rarity="epico"
else:
    rarity="epic"

if rarity:
    rarity="un poco comun"
else:
    rarity="uncommon"

print(f'Rareza: {rarity}')

This is the complete code that I am using:

import json
import requests

args = input("Escribe: ")

response = requests.get("https://jose89fcb.es/apifortnite/fish.php")
data = response.json()

match_found = False
for api in data['fish']:
    
    if args == api["name"]: 
        print(f'Imagen: {api["image"]}')
        print(f'Descripción: {api["description"]}')

        rarity = {api["rarity"]}
        if rarity:
            rarity = "epico"
        else:
            rarity = "epic"

        if rarity:
            rarity = "un poco comun"
        else:
            rarity = "uncommon"

        print(f'Rareza: {rarity}')

        match_found = True
if not match_found:
    print("Error")

请注意,我已将代码进行了翻译,但你需要确保代码中的引号和缩进在翻译后仍然是有效的,以确保代码的正确性。

英文:

I'm trying to find a way to translate some texts with else if but I can't find the way to do it:
What I try to translate is the following part of the code:

rarity = {api["rarity"]}
        if rarity:
            rarity="epico"
        else:
            rarity="epic"



            if rarity:
                rarity="un poco comun"
            else:
                rarity="uncommon"   



        
        print(f'Rareza: {rarity}')

But I can't figure out how to do it

This is the complete code that I am using:

import json
import requests

args= input("Escribe: ")

response = requests.get("https://jose89fcb.es/apifortnite/fish.php")
data = response.json()


match_found = False
for api in data['fish']:
    
    if args == api["name"]: 
        print(f'Imagen: {api["image"]}')
        print(f'Descripción: {api["description"]}')



        rarity = {api["rarity"]}
        if rarity:
            rarity="epico"
        else:
            rarity="epic"



            if rarity:
                rarity="un poco comun"
            else:
                rarity="uncommon"   



        
        print(f'Rareza: {rarity}')


        match_found = True
if not match_found:
    print("Error")

Could someone give me a hand? Thank you very much in advance!

答案1

得分: 0

A dictionary that takes English keys and returns Spanish values is what you are likely after:

rarity_en_to_sp = {
    "epic": "epico",
    "uncommon": "poco común",
}

rarity_en = "uncommon"
rarity_sp = rarity_en_to_sp.get(rarity_en, rarity_en)
print(f'Rarity: {rarity_en} Rareza: {rarity_sp}')

rarity_en = "unknown"
rarity_sp = rarity_en_to_sp.get(rarity_en, rarity_en)
print(f'Rarity: {rarity_en} Rareza: {rarity_sp}')

The use of get() here allows us to fall back to whatever the English version was in the event we don't have a translation yet.

This code should give you:

Rarity: uncommon Rareza: poco común
Rarity: unknown Rareza: unknown
英文:

A dictionary that takes English keys and returns Spanish values is what you are likely after:

rarity_en_to_sp = {
    "epic": "epico",
    "uncommon": "un poco comun",
}

rarity_en = "uncommon"
rarity_sp = rarity_en_to_sp.get(rarity_en, rarity_en)
print(f'Rarity: {rarity_en} Rareza: { rarity_sp }')

rarity_en = "unknown"
rarity_sp = rarity_en_to_sp.get(rarity_en, rarity_en)
print(f'Rarity: {rarity_en} Rareza: { rarity_sp }')

The use of get() here allows us to fall back to whatever the English version was in the event we don't have a translation yet.

This code should give you:

Rarity: uncommon Rareza: un poco comun
Rarity: unknown Rareza: unknown

答案2

得分: 0

我认为你不理解if rarity的含义。这只是指检查其值是否被视为TrueFalse。如果字符串中有任何内容,这将被视为True。也许你想检查其内容是否等于英文版本,并将其翻译为西班牙文版本?

你可以通过if-elif来实现:

rarity = {api["rarity"]}
if rarity == "epic":
    rarity = "epico"
elif rarity == "uncommon":
    rarity = "un poco comun"

print(f'Rareza: {rarity}')

或者使用一个dict

translations = {
    "epic": "epico",
    "uncommon": "un poco comun"
}

rarity = translations[rarity]
print(f'Rareza: {rarity}')
英文:

I don't think you understand what if rarity means. That just means, check if its value is considered True or False. If a string has any content in it, this will be considered True. Perhaps you meant to check if its content was equal to the English version, and you want to translate it to the Spanish version?

You could do this via if-elif:

rarity = {api["rarity"]}
if rarity == "epic":
    rarity = "epico"
elif rarity == "uncommon":
    rarity = "un poco comun"

print(f'Rareza: {rarity}')

Or a dict:

translations = {
    "epic": "epico",
    "uncommon": "un poco comun"
}

rarity = translations[rarity]
print(f'Rareza: {rarity}')

答案3

得分: -1

You can use the Google Translate library if it's for a small-sized project.

Use pip to install googletrans library:

pip3 install googletrans

And then for example:

# import library
from googletrans import Translator, constants

# initialize translator instance
translator = Translator()

# translate something to English from Italian (English is the default)
translation = translator.translate("Ciao Mondo!")
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")

Output:
Ciao Mondo! (it) --> Hello World! (en)

Source: How to Translate Languages in Python

If you get an error similar to this: AttributeError: 'NoneType' object has no attribute 'group' (which I got too), use these commands to install the specific version of the library that is used in the code above:

pip3 uninstall googletrans
pip3 install googletrans==3.1.0a0
英文:

You can use the Google Translate library if it's for a small-sized project.

Use pip to install googletrans library:

pip3 install googletrans

And then for example:

# import library
from googletrans import Translator, constants

# initialize translator instance
translator = Translator()

# translate something to English from Italian (English is the default)
translation = translator.translate("Ciao Mondo!")
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")

Output:
Ciao Mondo! (it) --> Hello World! (en)

Source: How to Translate Languages in Python

If you get an error similar to this: AttributeError: 'NoneType' object has no attribute 'group' (which I got too), use these commands to install the specific version of the library that is used in the code above:

pip3 uninstall googletrans
pip3 install googletrans==3.1.0a0

huangapple
  • 本文由 发表于 2023年4月19日 23:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定