Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

language.tsx 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. mode: 'submenu', // Explicitly set submenu mode
  32. async search(args: string, _locale: string = 'en') {
  33. // Return language options directly, regardless of parameters
  34. return buildLanguageCommands(args)
  35. },
  36. register(deps: LanguageDeps) {
  37. registerCommands({
  38. 'i18n.set': async (args) => {
  39. const locale = args?.locale
  40. if (locale)
  41. await deps.setLocale?.(locale)
  42. },
  43. })
  44. },
  45. unregister() {
  46. unregisterCommands(['i18n.set'])
  47. },
  48. }