f5976a2d74
* feat: moved tl-runtime to esm and native ArrayBuffers * feat: migration to esm * fix(core): web-related fixes * test: finally, some good fucking e2e * chore: fixed linters etc * ci: added e2e to ci * build(tl): fixed gen-code on node 20 * fix: codegen Uint8Array, not Buffer never `git reset --hard` kids * build: only do type-aware linting for `packages/*` * build: ignore no-unresolved in ci for e2e * fix: node 16 doesn't have subtle crypto apparently? * fix(tests): use Uint8Array for gods sake please can i just merge this already * ci: don't parallel tasks in ci because machines are utter garbage and it may just randomly break * ci: pass secrets to e2e tests * ci: separate cli command for ci apparently im retarded * fix: run codegen in e2e im actually retarded * ci: more fixes for e2e * ci: debugging stuff * ci: still debugging * ci: hopefully fix ci???
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import type * as dispatcherNs from '@mtcute/dispatcher'
|
|
|
|
import { I18nStrings, I18nValue } from './types.js'
|
|
|
|
/**
|
|
* Create an index of i18n strings delimited by "."
|
|
*
|
|
* @param strings Strings object
|
|
*/
|
|
export function createI18nStringsIndex(strings: I18nStrings): Record<string, I18nValue> {
|
|
const ret: Record<string, I18nValue> = {}
|
|
|
|
function add(obj: I18nStrings, prefix: string) {
|
|
for (const key in obj) {
|
|
const val = obj[key]
|
|
|
|
if (typeof val === 'object' && !('value' in val)) {
|
|
add(val, prefix + key + '.')
|
|
} else {
|
|
ret[prefix + key] = val as string
|
|
}
|
|
}
|
|
}
|
|
|
|
add(strings, '')
|
|
|
|
return ret
|
|
}
|
|
|
|
/**
|
|
* Extract language from `@mtcute/dispatcher` context.
|
|
* Can be used for customized adapters or external i18n libraries.
|
|
*
|
|
* @param update Update to extract language from
|
|
*/
|
|
export function extractLanguageFromUpdate(update: dispatcherNs.UpdateContextType): string | null | undefined {
|
|
if (!('_name' in update)) return null
|
|
|
|
// for whatever reason eslint doesn't like this
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-return */
|
|
switch (update._name) {
|
|
case 'new_message': {
|
|
const { sender } = update
|
|
|
|
return sender.type === 'user' ? sender.language : null
|
|
}
|
|
case 'poll_vote': {
|
|
const { peer } = update
|
|
|
|
return peer?.type === 'user' ? peer.language : null
|
|
}
|
|
case 'chat_member':
|
|
case 'inline_query':
|
|
case 'chosen_inline_result':
|
|
case 'callback_query':
|
|
case 'bot_stopped':
|
|
case 'bot_chat_join_request':
|
|
return update.user.language
|
|
}
|
|
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-return */
|
|
|
|
return null
|
|
}
|