|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import List, Optional |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import enums, raw, types, utils |
| 23 | + |
| 24 | + |
| 25 | +class TranslateText: |
| 26 | + async def translate_text( |
| 27 | + self: "pyrogram.Client", |
| 28 | + text: str, |
| 29 | + to_language_code: str, |
| 30 | + parse_mode: Optional["enums.ParseMode"] = None, |
| 31 | + entities: List["types.MessageEntity"] = None, |
| 32 | + ) -> "types.FormattedText": |
| 33 | + """Translate a text to the given language. |
| 34 | +
|
| 35 | + If the current user is a Telegram Premium user, then text formatting is preserved. |
| 36 | +
|
| 37 | + .. include:: /_includes/usable-by/users.rst |
| 38 | +
|
| 39 | + Parameters: |
| 40 | + text (``str``): |
| 41 | + Text to translate. |
| 42 | +
|
| 43 | + to_language_code (``str``): |
| 44 | + Language code of the language to which the message is translated. |
| 45 | + Must be one of "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh-CN", "zh", "zh-Hans", "zh-TW", "zh-Hant", "co", "hr", "cs", "da", "nl", "en", "eo", "et", |
| 46 | + "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "iw", "hi", "hmn", "hu", "is", "ig", "id", "in", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko", |
| 47 | + "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr", |
| 48 | + "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "ji", "yo", "zu" |
| 49 | +
|
| 50 | + Returns: |
| 51 | + :obj:`~pyrogram.types.FormattedText`: On success, information about the translated text is returned. |
| 52 | +
|
| 53 | + Example: |
| 54 | + .. code-block:: python |
| 55 | +
|
| 56 | + await app.translate_text("Hello!", "ru") |
| 57 | + """ |
| 58 | + message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values() |
| 59 | + |
| 60 | + r = await self.invoke( |
| 61 | + raw.functions.messages.TranslateText( |
| 62 | + to_lang=to_language_code, |
| 63 | + text=[ |
| 64 | + raw.types.TextWithEntities( |
| 65 | + text=message, |
| 66 | + entities=entities or [] |
| 67 | + ) |
| 68 | + ] |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + return types.FormattedText._parse(self, r.result[0]) |
0 commit comments