Prompt
How do I centralize app strings and locale updates?
Latest observation
A good way to centralize app strings and locale updates is to make one source of truth for text and one source of truth for language/locale changes, then have the rest of the app subscribe to them.
1) Centralize strings
Store all user-facing text in one place instead of scattering hardcoded strings throughout the app.
Common approaches
- Resource files:
- Android:
strings.xml - iOS:
Localizable.strings - Web/JS: JSON/YAML locale files
- Android:
- Constant keys + translation files:
app.titlelogin.buttonerror.network
Example
{
"app.title": "My App",
"login.button": "Log In"
}
Then use a translation helper everywhere:
t("login.button")
2) Centralize locale state
Keep the current locale in a single global place, such as:
- a store (Redux, Zustand, MobX, Vuex, etc.)
- app-wide context/provider
- dependency-injected locale service
When the locale changes, update that central state once, and all UI should re-render from it.
Example pattern
const LocaleContext = createContext();
function LocaleProvider({ children }) {
const [locale, setLocale] = useState("en");
const value = {
locale,
setLocale,
t: (key) => translations[locale][key] ?? key,
};
return (
<LocaleContext.Provider value={value}>
{children}
</LocaleContext.Provider>
);
}
Now any component can do:
const { t, setLocale } = useContext(LocaleContext);
3) Make locale changes reactive
When the user selects a new language:
- Save it centrally
- Persist it if needed
- Notify the app
- Re-render text based on the new locale
Persisting
- local storage / shared preferences / secure storage
- server profile setting if it should sync across devices
4) Separate “locale” from “translation”
Locale usually includes more than language:
- language:
en,fr - region:
en-US,fr-CA - formatting: dates, numbers, currency
- pluralization rules
So your locale service should ideally manage:
- current locale
- translation lookup
- date/number formatting
5) Avoid hardcoded strings in business logic
Even validation errors, toast messages, and notifications should use keys:
- bad:
"Network error" - good:
"error.network"
That keeps all copy editable in one place.
6) Use fallback behavior
If a translation is missing:
- fall back to default language
- or show the key name in development
Example:
function t(key) {
return translations[locale]?.[key]
?? translations["en"]?.[key]
?? key;
}
7) Recommended architecture
A clean structure is:
i18n/en.jsones.jsonfr.json
translationServicet(key)setLocale(locale)getLocale()
LocaleProvider/ store- UI components consume only
t(...)
If you tell me your stack (React, Flutter, Android, iOS, Vue, etc.), I can show the exact recommended implementation.