2023-09-03 02:37:51 +03:00
|
|
|
import type * as clientNs from '@mtcute/client'
|
2022-07-19 02:47:59 +03:00
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
import { I18nStrings, I18nValue } from './types'
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
let client: typeof clientNs
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2022-08-29 18:54:43 +03:00
|
|
|
try {
|
2023-09-03 02:37:51 +03:00
|
|
|
client = require('@mtcute/client') as typeof clientNs
|
2022-08-29 18:54:43 +03:00
|
|
|
} catch (e) {}
|
|
|
|
|
2022-09-12 15:01:43 +03:00
|
|
|
/**
|
|
|
|
* Create an index of i18n strings delimited by "."
|
|
|
|
*
|
|
|
|
* @param strings Strings object
|
|
|
|
*/
|
2023-09-24 01:32:22 +03:00
|
|
|
export function createI18nStringsIndex(strings: I18nStrings): Record<string, I18nValue> {
|
2022-07-19 02:47:59 +03:00
|
|
|
const ret: Record<string, I18nValue> = {}
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
function add(obj: I18nStrings, prefix: string) {
|
2022-07-19 02:47:59 +03:00
|
|
|
for (const key in obj) {
|
|
|
|
const val = obj[key]
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
if (typeof val === 'object' && !('value' in val)) {
|
2022-07-19 02:47:59 +03:00
|
|
|
add(val, prefix + key + '.')
|
|
|
|
} else {
|
2023-06-05 03:30:48 +03:00
|
|
|
ret[prefix + key] = val as string
|
2022-07-19 02:47:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
add(strings, '')
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-09-12 15:01:43 +03:00
|
|
|
/**
|
|
|
|
* Extract language from `@mtcute/client` update. Can be used for customized
|
|
|
|
* adapters or external i18n libraries.
|
|
|
|
*
|
|
|
|
* @param update Update to extract language from
|
|
|
|
*/
|
2023-09-24 01:32:22 +03:00
|
|
|
export function extractLanguageFromUpdate(update: clientNs.ParsedUpdate['data']): string | null | undefined {
|
2022-08-29 18:54:43 +03:00
|
|
|
if (!client) {
|
2023-09-24 01:32:22 +03:00
|
|
|
throw new Error('@mtcute/client is not installed, you must provide your own adapter')
|
2022-08-29 18:54:43 +03:00
|
|
|
}
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
switch (update.constructor) {
|
2022-08-29 18:54:43 +03:00
|
|
|
case client.Message:
|
2022-07-19 02:47:59 +03:00
|
|
|
// if sender is Chat it will just be undefined
|
2023-09-24 01:32:22 +03:00
|
|
|
return ((update as clientNs.Message).sender as clientNs.User).language
|
2023-09-03 02:37:51 +03:00
|
|
|
case client.PollVoteUpdate:
|
|
|
|
// if peer is Chat it will just be undefined
|
2023-09-24 01:32:22 +03:00
|
|
|
return ((update as clientNs.PollVoteUpdate).peer as clientNs.User).language
|
2022-08-29 18:54:43 +03:00
|
|
|
case client.ChatMemberUpdate:
|
|
|
|
case client.InlineQuery:
|
|
|
|
case client.ChosenInlineResult:
|
|
|
|
case client.CallbackQuery:
|
|
|
|
case client.BotStoppedUpdate:
|
|
|
|
case client.BotChatJoinRequestUpdate:
|
2023-06-05 03:30:48 +03:00
|
|
|
return (
|
|
|
|
update as
|
2023-09-03 02:37:51 +03:00
|
|
|
| clientNs.ChatMemberUpdate
|
|
|
|
| clientNs.InlineQuery
|
|
|
|
| clientNs.ChosenInlineResult
|
|
|
|
| clientNs.CallbackQuery
|
|
|
|
| clientNs.BotStoppedUpdate
|
|
|
|
| clientNs.BotChatJoinRequestUpdate
|
2023-06-05 03:30:48 +03:00
|
|
|
).user.language
|
2022-07-19 02:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|