mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-06-22 04:11:55 +10:00
refactor(web): dedupe isRTL via utils locale module
Re-export isRTL from @reactive-resume/utils/locale in the web locale helper and consolidate RTL detection tests in the utils package. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isLocale, isRTL, resolveLocale } from "./locale";
|
||||
import { isLocale, resolveLocale } from "./locale";
|
||||
|
||||
describe("isLocale", () => {
|
||||
it("returns true for known locale en-US", () => {
|
||||
@@ -44,46 +44,3 @@ describe("resolveLocale", () => {
|
||||
expect(resolveLocale("")).toBe("en-US");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isRTL", () => {
|
||||
it("returns true for Arabic", () => {
|
||||
expect(isRTL("ar-SA")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for Hebrew", () => {
|
||||
expect(isRTL("he-IL")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for Persian/Farsi", () => {
|
||||
expect(isRTL("fa-IR")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for Urdu", () => {
|
||||
expect(isRTL("ur-PK")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for English", () => {
|
||||
expect(isRTL("en-US")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for German", () => {
|
||||
expect(isRTL("de-DE")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for Chinese", () => {
|
||||
expect(isRTL("zh-CN")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for unknown locale", () => {
|
||||
expect(isRTL("xyz-XX")).toBe(false);
|
||||
});
|
||||
|
||||
it("matches prefix case-insensitively (lowercase prefix)", () => {
|
||||
expect(isRTL("AR-SA")).toBe(true);
|
||||
});
|
||||
|
||||
it("works with locale-only string (no region)", () => {
|
||||
expect(isRTL("ar")).toBe(true);
|
||||
expect(isRTL("en")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,9 @@ import type { Locale } from "@reactive-resume/utils/locale";
|
||||
import { i18n } from "@lingui/core";
|
||||
import { msg } from "@lingui/core/macro";
|
||||
import Cookies from "js-cookie";
|
||||
import { localeSchema } from "@reactive-resume/utils/locale";
|
||||
import { isRTL, localeSchema } from "@reactive-resume/utils/locale";
|
||||
|
||||
export { isRTL };
|
||||
|
||||
const storageKey = "locale";
|
||||
const defaultLocale: Locale = "en-US";
|
||||
@@ -75,24 +77,6 @@ export const resolveLocale = (locale: string): Locale => {
|
||||
return isLocale(locale) ? locale : defaultLocale;
|
||||
};
|
||||
|
||||
const RTL_LANGUAGES = new Set([
|
||||
"ar", // Arabic
|
||||
"ckb", // Kurdish (Sorani)
|
||||
"dv", // Dhivehi
|
||||
"fa", // Persian
|
||||
"he", // Hebrew
|
||||
"ps", // Pashto
|
||||
"sd", // Sindhi
|
||||
"ug", // Uyghur
|
||||
"ur", // Urdu
|
||||
"yi", // Yiddish
|
||||
]);
|
||||
|
||||
export function isRTL(locale: string): boolean {
|
||||
const language = locale.split("-")[0].toLowerCase();
|
||||
return RTL_LANGUAGES.has(language);
|
||||
}
|
||||
|
||||
export const getLocale = () => {
|
||||
const locale = Cookies.get(storageKey);
|
||||
if (!locale || !isLocale(locale)) return defaultLocale;
|
||||
|
||||
@@ -46,9 +46,16 @@ describe("isRTL", () => {
|
||||
["ar-SA", true],
|
||||
["he-IL", true],
|
||||
["fa-IR", true],
|
||||
["ur-PK", true],
|
||||
["en-US", false],
|
||||
["en-GB", false],
|
||||
["fr-FR", false],
|
||||
["de-DE", false],
|
||||
["zh-CN", false],
|
||||
["xyz-XX", false],
|
||||
["AR-SA", true],
|
||||
["ar", true],
|
||||
["en", false],
|
||||
])("returns %s → %s", (locale, expected) => {
|
||||
expect(isRTL(locale)).toBe(expected);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user