Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

language.tsx 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { SlashCommandHandler } from './types'
  2. import type { CommandSearchResult } from '../types'
  3. import { languages } from '@/i18n-config/language'
  4. import i18n from '@/i18n-config/i18next-config'
  5. import { registerCommands, unregisterCommands } from './command-bus'
  6. // Language dependency types
  7. type LanguageDeps = {
  8. setLocale?: (locale: string) => Promise<void>
  9. }
  10. const buildLanguageCommands = (query: string): CommandSearchResult[] => {
  11. const q = query.toLowerCase()
  12. const list = languages.filter(item => item.supported && (
  13. !q || item.name.toLowerCase().includes(q) || String(item.value).toLowerCase().includes(q)
  14. ))
  15. return list.map(item => ({
  16. id: `lang-${item.value}`,
  17. title: item.name,
  18. description: i18n.t('app.gotoAnything.actions.languageChangeDesc'),
  19. type: 'command' as const,
  20. data: { command: 'i18n.set', args: { locale: item.value } },
  21. }))
  22. }
  23. /**
  24. * Language command handler
  25. * Integrates UI building, search, and registration logic
  26. */
  27. export const languageCommand: SlashCommandHandler<LanguageDeps> = {
  28. name: 'language',
  29. aliases: ['lang'],
  30. description: 'Switch between different languages',
  31. async search(args: string, _locale: string = 'en') {
  32. // Return language options directly, regardless of parameters
  33. return buildLanguageCommands(args)
  34. },
  35. register(deps: LanguageDeps) {
  36. registerCommands({
  37. 'i18n.set': async (args) => {
  38. const locale = args?.locale
  39. if (locale)
  40. await deps.setLocale?.(locale)
  41. },
  42. })
  43. },
  44. unregister() {
  45. unregisterCommands(['i18n.set'])
  46. },
  47. }