Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

1.3 KB
Raw Blame
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';

import type { Locale } from '../lib/i18n/types';

// Maps a BCP-47 tag prefix to one of our supported locales. Order matters:
// `in` (legacy Indonesian) must come after `id` (Bahasa) so neither shadows
// the other, and `en` sits last so it loses to a more specific match.
const PREFIX_TO_LOCALE: Array<[string, Locale]> = [
  ['zh', 'zh-CN'],
  ['hi', 'hi'],
  ['es', 'es'],
  ['ja', 'ja'],
  ['ko', 'ko'],
  ['ar', 'ar'],
  ['fr', 'fr'],
  ['bn', 'bn'],
  ['pt', 'pt'],
  ['de', 'de'],
  ['ru', 'ru'],
  ['id', 'id'],
  ['in', 'id'],
  ['it', 'it'],
  ['pl', 'pl'],
  ['tr', 'tr'],
  ['en', 'en'],
];

function detectLocale(): Locale {
  try {
    const normalized = navigator.language?.toLowerCase();
    if (!normalized) return 'en';
    for (const [prefix, locale] of PREFIX_TO_LOCALE) {
      if (normalized.startsWith(prefix)) return locale;
    }
  } catch {
    // browser API unavailable
  }
  return 'en';
}

interface LocaleState {
  current: Locale;
}

const initialState: LocaleState = { current: detectLocale() };

const localeSlice = createSlice({
  name: 'locale',
  initialState,
  reducers: {
    setLocale(state, action: PayloadAction<Locale>) {
      state.current = action.payload;
    },
  },
});

export const { setLocale } = localeSlice.actions;
export default localeSlice.reducer;