Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/i18n/src/constants/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export const SUPPORTED_LANGUAGES: ILanguageOption[] = [
{ label: "한국어", value: "ko" },
];

export const STORAGE_KEY = "userLanguage";
export const LANGUAGE_STORAGE_KEY = "userLanguage";
44 changes: 5 additions & 39 deletions packages/i18n/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import get from "lodash/get";
import merge from "lodash/merge";
import { makeAutoObservable, runInAction } from "mobx";
// constants
import { FALLBACK_LANGUAGE, SUPPORTED_LANGUAGES, STORAGE_KEY } from "../constants";
import { FALLBACK_LANGUAGE, SUPPORTED_LANGUAGES, LANGUAGE_STORAGE_KEY } from "../constants";
// core translations imports
import coreEn from "../locales/en/core.json";
// types
Expand Down Expand Up @@ -48,14 +48,14 @@ export class TranslationStore {
private initializeLanguage() {
if (typeof window === "undefined") return;

const savedLocale = localStorage.getItem(STORAGE_KEY) as TLanguage;
const savedLocale = localStorage.getItem(LANGUAGE_STORAGE_KEY) as TLanguage;
if (this.isValidLanguage(savedLocale)) {
this.setLanguage(savedLocale);
return;
}

const browserLang = this.getBrowserLanguage();
this.setLanguage(browserLang);
// Fallback to default language
this.setLanguage(FALLBACK_LANGUAGE);
}

/** Loads the translations for the current language */
Expand Down Expand Up @@ -175,40 +175,6 @@ export class TranslationStore {
return lang !== null && this.availableLanguages.some((l) => l.value === lang);
}

/** Checks if a language code is similar to any supported language */
private findSimilarLanguage(lang: string): TLanguage | null {
// Convert to lowercase for case-insensitive comparison
const normalizedLang = lang.toLowerCase();

// Find a supported language that includes or is included in the browser language
const similarLang = this.availableLanguages.find(
(l) => normalizedLang.includes(l.value.toLowerCase()) || l.value.toLowerCase().includes(normalizedLang)
);

return similarLang ? similarLang.value : null;
}

/** Gets the browser language based on the navigator.language */
private getBrowserLanguage(): TLanguage {
const browserLang = navigator.language;

// Check exact match first
if (this.isValidLanguage(browserLang)) {
return browserLang;
}

// Check base language without region code
const baseLang = browserLang.split("-")[0];
if (this.isValidLanguage(baseLang)) {
return baseLang as TLanguage;
}

// Try to find a similar language
const similarLang = this.findSimilarLanguage(browserLang) || this.findSimilarLanguage(baseLang);

return similarLang || FALLBACK_LANGUAGE;
}

/**
* Gets the cache key for the given key and locale
* @param key - the key to get the cache key for
Expand Down Expand Up @@ -293,7 +259,7 @@ export class TranslationStore {
}

if (typeof window !== "undefined") {
localStorage.setItem(STORAGE_KEY, lng);
localStorage.setItem(LANGUAGE_STORAGE_KEY, lng);
document.documentElement.lang = lng;
}

Expand Down
4 changes: 3 additions & 1 deletion web/core/store/root.store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { enableStaticRendering } from "mobx-react";
// plane imports
import { FALLBACK_LANGUAGE, LANGUAGE_STORAGE_KEY } from "@plane/i18n";
// plane web store
import { CommandPaletteStore, ICommandPaletteStore } from "@/plane-web/store/command-palette.store";
import { RootStore } from "@/plane-web/store/root.store";
Expand Down Expand Up @@ -97,7 +99,7 @@ export class CoreRootStore {
resetOnSignOut() {
// handling the system theme when user logged out from the app
localStorage.setItem("theme", "system");

localStorage.setItem(LANGUAGE_STORAGE_KEY, FALLBACK_LANGUAGE);
this.router = new RouterStore();
this.commandPalette = new CommandPaletteStore();
this.instance = new InstanceStore();
Expand Down