Skip to content

Commit

Permalink
Merge pull request #27 from cyxou/add-es-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
cyxou authored Nov 7, 2023
2 parents 25e979b + 4a4d5ac commit 4030159
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "firefly-iii-telegram-bot",
"description": "A Telegram bot for working with Firefly III with a supersonic speed",
"version": "2.0.0",
"version": "2.1.0",
"homepage": "https://github.com/cyxou/firefly-iii-telegram-bot#readme",
"license": "GPL-3.0-or-later",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions src/composers/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { table, getBorderCharacters } from 'table'
import type { Alignment } from 'table'

import type { MyContext } from '../types/MyContext'
import i18n from '../lib/i18n'
import i18n, { locales } from '../lib/i18n'
import {
listAccountsMapper as mapper,
createAccountsMenuKeyboard
Expand All @@ -22,9 +22,9 @@ const debug = Debug(`bot:accounts`)
const bot = new Composer<MyContext>()

// List transactions
bot.hears(i18n.t('en', 'labels.ACCOUNTS'), showAccounts)
bot.hears(i18n.t('ru', 'labels.ACCOUNTS'), showAccounts)
bot.hears(i18n.t('it', 'labels.ACCOUNTS'), showAccounts)
for (const locale of locales) {
bot.hears(i18n.t(locale, 'labels.ACCOUNTS'), showAccounts)
}
bot.callbackQuery(mapper.list.regex(), showAccounts)
bot.callbackQuery(mapper.close.regex(), closeHandler)

Expand Down
8 changes: 4 additions & 4 deletions src/composers/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Composer, InlineKeyboard } from 'grammy'
import { Router } from "@grammyjs/router"

import type { MyContext } from '../types/MyContext'
import i18n from '../lib/i18n';
import i18n, { locales } from '../lib/i18n';
import firefly from '../lib/firefly'
import { TransactionRead } from '../lib/firefly/model/transaction-read'
import { TRANSACTIONS_PAGE_LIMIT } from './constants'
Expand All @@ -32,9 +32,9 @@ const DECLINE_CATEGORIES_LIST = 'DECLINE_CATEGORIES_LIST'
const bot = new Composer<MyContext>()
const router = new Router<MyContext>((ctx) => ctx.session.step)

bot.hears(i18n.t('ru', 'labels.CATEGORIES'), listCategoriesCommandHandler)
bot.hears(i18n.t('en', 'labels.CATEGORIES'), listCategoriesCommandHandler)
bot.hears(i18n.t('it', 'labels.CATEGORIES'), listCategoriesCommandHandler)
for (const locale of locales) {
bot.hears(i18n.t(locale, 'labels.CATEGORIES'), listCategoriesCommandHandler)
}
bot.callbackQuery(CATEGORY_DETAILS, showCategoryDetails)
bot.callbackQuery(ADD_CATEGORIES, addCategoriesCbQH)
bot.callbackQuery(RENAME_CATEGORY, typeNewCategoryName)
Expand Down
8 changes: 4 additions & 4 deletions src/composers/reports.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Composer } from 'grammy'
import i18n from '../lib/i18n'
import i18n, { locales } from '../lib/i18n'

import type { MyContext } from '../types/MyContext'

// const rootLog = debug(`bot:reports`)

const bot = new Composer<MyContext>()

bot.hears(i18n.t('en', 'labels.REPORTS'), reportsHandler)
bot.hears(i18n.t('ru', 'labels.REPORTS'), reportsHandler)
bot.hears(i18n.t('it', 'labels.REPORTS'), reportsHandler)
for (const locale of locales) {
bot.hears(i18n.t(locale, 'labels.REPORTS'), reportsHandler)
}

async function reportsHandler(ctx: MyContext) {
await ctx.reply(ctx.i18n.t('reports.notImplemented'), { parse_mode: 'Markdown' })
Expand Down
10 changes: 5 additions & 5 deletions src/composers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Router } from "@grammyjs/router"
import { Menu, MenuRange } from '@grammyjs/menu'

import type { MyContext } from '../types/MyContext'
import i18n, { getLanguageIcon } from '../lib/i18n';
import i18n, { locales } from '../lib/i18n';
import { command, ACCOUNTS_PAGE_LIMIT } from './constants'
import firefly from '../lib/firefly'
import { AccountTypeFilter } from '../lib/firefly/model/account-type-filter'
Expand Down Expand Up @@ -68,7 +68,7 @@ const settingsMenu = new Menu<MyContext>('settings')
const langMenu = new Menu<MyContext>('switch-lang')
.dynamic(() => {
const range = new MenuRange<MyContext>()
for (const locale of ['ru', 'it', 'en']) {
for (const locale of locales) {
const langText = 'labels.' + `SWITCH_TO_${locale}`.toUpperCase()
range.text(
{
Expand Down Expand Up @@ -151,10 +151,10 @@ settingsMenu.register(cancelMenu)
bot.use(settingsMenu)
bot.use(requireSettings())

for (const locale of locales) {
bot.hears(i18n.t(locale, 'labels.SETTINGS'), settingsCommandHandler)
}
bot.command(command.SETTINGS, settingsCommandHandler)
bot.hears(i18n.t('en', 'labels.SETTINGS'), settingsCommandHandler)
bot.hears(i18n.t('ru', 'labels.SETTINGS'), settingsCommandHandler)
bot.hears(i18n.t('it', 'labels.SETTINGS'), settingsCommandHandler)

// Local routes and handlers
router.route('IDLE', (_, next) => next())
Expand Down
8 changes: 4 additions & 4 deletions src/composers/transactions/list-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Composer, InlineKeyboard } from 'grammy'
import { table, getBorderCharacters } from 'table'

import type { MyContext } from '../../types/MyContext'
import i18n from '../../lib/i18n'
import i18n, { locales } from '../../lib/i18n'
import {
listTransactionsMapper as mapper
} from '../helpers'
Expand All @@ -21,9 +21,9 @@ const debug = Debug(`bot:transactions:list`)
const bot = new Composer<MyContext>()

// List transactions
bot.hears(i18n.t('en', 'labels.TRANSACTIONS'), showTransactions)
bot.hears(i18n.t('ru', 'labels.TRANSACTIONS'), showTransactions)
bot.hears(i18n.t('it', 'labels.TRANSACTIONS'), showTransactions)
for (const locale of locales) {
bot.hears(i18n.t(locale, 'labels.TRANSACTIONS'), showTransactions)
}
bot.callbackQuery(mapper.list.regex(), showTransactions)
bot.callbackQuery(mapper.close.regex(), closeHandler)

Expand Down
15 changes: 5 additions & 10 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { I18n, pluralize } from '@grammyjs/i18n';

import dayjs from 'dayjs'
import localizedFormat from 'dayjs/plugin/localizedFormat'

import 'dayjs/locale/ru'
import 'dayjs/locale/en'
import 'dayjs/locale/es'
import 'dayjs/locale/it'

const defaultLanguage = 'en'

Expand All @@ -26,14 +29,6 @@ const params = {

const i18n = new I18n(params)

export default i18n

export function getLanguageIcon(language: string) {
const lang2icons = {
ru: '🇷🇺',
en: '🇬🇧',
it: '🇮🇹'
}
export const locales = ['ru', 'it', 'en', 'es']

return lang2icons[language]
}
export default i18n
39 changes: 13 additions & 26 deletions src/lib/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import debug from 'debug'

import i18n from './i18n'
import i18n, { locales } from './i18n'
import { command } from '../composers/constants'
import type { MyContext } from '../types/MyContext'
import { createMainKeyboard } from '../composers/helpers'
Expand All @@ -19,25 +19,14 @@ export function cleanup() {
if (ctx.msg!.from!.is_bot) return next()

const text = ctx.msg!.text || ''
const keyboardCommandList = [
i18n.t('ru', 'labels.SETTINGS'),
i18n.t('ru', 'labels.TRANSACTIONS'),
i18n.t('ru', 'labels.ACCOUNTS'),
i18n.t('ru', 'labels.REPORTS'),
i18n.t('ru', 'labels.CATEGORIES'),

i18n.t('en', 'labels.SETTINGS'),
i18n.t('en', 'labels.TRANSACTIONS'),
i18n.t('en', 'labels.ACCOUNTS'),
i18n.t('en', 'labels.REPORTS'),
i18n.t('en', 'labels.CATEGORIES'),

i18n.t('it', 'labels.SETTINGS'),
i18n.t('it', 'labels.TRANSACTIONS'),
i18n.t('it', 'labels.ACCOUNTS'),
i18n.t('it', 'labels.REPORTS'),
i18n.t('it', 'labels.CATEGORIES'),
]
const keyboardCommandList = []
for (const locale of locales) {
keyboardCommandList.push(i18n.t(locale, 'labels.SETTINGS'))
keyboardCommandList.push(i18n.t(locale, 'labels.TRANSACTIONS'))
keyboardCommandList.push(i18n.t(locale, 'labels.ACCOUNTS'))
keyboardCommandList.push(i18n.t(locale, 'labels.REPORTS'))
keyboardCommandList.push(i18n.t(locale, 'labels.CATEGORIES'))
}
log('keyboardCommandList: %O', keyboardCommandList)
log('keyboardCommandList.includes(text): %O', keyboardCommandList.includes(text))

Expand Down Expand Up @@ -70,12 +59,10 @@ export function requireSettings() {
log('text: %O', text)
// We allow only the commands routes to enter if Firefly URL or Firefly
// Token are not set
const whiteList = [
i18n.t('ru', 'labels.SETTINGS'),
i18n.t('en', 'labels.SETTINGS'),
i18n.t('it', 'labels.SETTINGS'),
...Object.values(command)
]
const whiteList = [ ...Object.values(command) ]
for (const locale of locales) {
whiteList.push(i18n.t(locale, 'labels.SETTINGS'))
}
log('whiteList: %O', whiteList)
log('callbackQuery: %O', ctx.callbackQuery)
const isCallbackQuery = !!ctx.callbackQuery
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ labels:
YES: ✅ Yes
SWITCH_LANG: 🌐 Language
SWITCH_TO_EN: 🇬🇧 English
SWITCH_TO_ES: 🇪🇸 Spanish
SWITCH_TO_RU: 🇷🇺 Russian
SWITCH_TO_IT: 🇮🇹 Italian

Expand Down
Loading

0 comments on commit 4030159

Please sign in to comment.