Skip to content

Instantly share code, notes, and snippets.

@stek29
Last active March 9, 2021 12:14
Show Gist options
  • Save stek29/38188860d2e0a55e17f727bab7ed1dfa to your computer and use it in GitHub Desktop.
Save stek29/38188860d2e0a55e17f727bab7ed1dfa to your computer and use it in GitHub Desktop.
Get TG langpack with Telethon
from telethon.tl.functions import InitConnectionRequest, InvokeWithLayerRequest
from telethon.tl.functions import langpack # GetLangPackRequest, GetDifference
from telethon.tl import all_tlobjects # for LAYER
from telethon.tl.types import LangPackString, LangPackStringPluralized, LangPackStringDeleted
from telethon.tl.functions import InitConnectionRequest, InvokeWithLayerRequest
from telethon.tl.functions import langpack # GetLangPackRequest, GetDifference
from telethon.tl import all_tlobjects
from telethon.tl.types import LangPackString, LangPackStringPluralized, LangPackStringDeleted
def get_lang_pack(client, lang_pack, lang_code='en', from_version=None):
if from_version is not None:
query = langpack.GetDifference(from_version=from_version)
else:
query = langpack.GetLangPackRequest(lang_code=lang_code)
init_connection_request = InitConnectionRequest(
api_id=client.api_id,
lang_pack=lang_pack,
query=query,
lang_code=lang_code,
**{x: 'shitfuck' for x in ['device_model', 'system_version', 'app_version', 'system_lang_code']}
)
return client(InvokeWithLayerRequest(layer=all_tlobjects.LAYER, query=init_connection_request))
def parse_lang_pack(pack):
parsed = dict()
for lpstr in pack.strings:
if isinstance(lpstr, LangPackString):
parsed[lpstr.key] = lpstr.value
elif isinstance(lpstr, LangPackStringPluralized):
for mode in ['zero', 'one', 'two', 'few', 'many', 'other']:
val = getattr(lpstr, mode + '_value')
if val is not None:
parsed['%s#%s' % (lpstr.key, mode)] = val
elif isinstance(lpstr, LangPackStringDeleted):
# HOW DO I HANDLE THIS LOL
pass
return parsed
"""
# Usage example:
from telethon import TelegramClient
from pprint import pprint
from langpacker import *
# see telethon docs
client = TelegramClient(session_name, api_id, api_hash)
client.connect()
pprint(parse_lang_pack(get_lang_pack(client, 'android', 'ru')))
client.disconnect()
"""
@Muaath5
Copy link

Muaath5 commented Mar 1, 2021

How to get a langpack for desktop?

@MasterGroosha
Copy link

MasterGroosha commented Mar 9, 2021

@Muaath5:

from telethon import TelegramClient
from telethon.tl.functions import InitConnectionRequest, InvokeWithLayerRequest
from telethon.tl.functions.langpack import GetDifferenceRequest
import config


async def get_langpack(client: TelegramClient, lang_code: str, platform_name: str, from_version: int = 0):
    """
    Gets langpack diff of full langpack if "from_version" set to 0
    :param client: TelegramClient instance
    :param lang_code: custom language identifier, e.g. "english", "mycustompack"
    :param platform_name: platform name, e.g. "android", "ios"
    :param from_version: Lower version to diff with
    :return: langpack difference
    """
    init_connection_request = InitConnectionRequest(
        api_id=client.api_id,
        lang_pack=platform_name,
        lang_code=lang_code,
        system_lang_code="ru",
        query=GetDifferenceRequest(from_version=from_version, lang_code=lang_code, lang_pack=platform_name),
        **{x: 'whatever' for x in ['device_model', 'system_version', 'app_version']}
    )
    lang_pack_difference = await client(InvokeWithLayerRequest(
        layer=config.current_layer, query=init_connection_request)
    )
    return lang_pack_difference

For TDesktop, the platform_code would be... TDesktop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment