adab18e3fb
- Remove locale check for give command (Now handled in the localeService itself) - Fix variable names/spelling/lowercase in LocaleService.getPlatformForServerLocale (should be no change in behaviour) - Rewrite LocaleService.getPlatformForClientLocale to not depend on serverSupportedLocales, and instead just use the tables for validity checking - Expand fallback of client locale handling to include the region code (Handles Czech and Korean locales) - Write tests for locales Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/332 Co-authored-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com> Co-committed-by: DrakiaXYZ <drakiaxyz@noreply.dev.sp-tarkov.com>
125 lines
3.8 KiB
TypeScript
125 lines
3.8 KiB
TypeScript
import { container } from "tsyringe";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { LocaleService } from "@spt-aki/services/LocaleService";
|
|
|
|
describe("LocaleService", () =>
|
|
{
|
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
let localeService: any; // Using "any" to access private/protected methods without type errors.
|
|
|
|
beforeEach(() =>
|
|
{
|
|
localeService = container.resolve<LocaleService>("LocaleService");
|
|
});
|
|
|
|
afterEach(() =>
|
|
{
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("ValidateServerLocale", () =>
|
|
{
|
|
it("should return 'en' for 'en-US'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("en-US");
|
|
};
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("en");
|
|
});
|
|
|
|
it("should return 'ko' for 'ko-KR'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("ko-KR");
|
|
};
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("ko");
|
|
});
|
|
|
|
it("should return 'pt-pt' for 'pt-PT'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("pt-PT");
|
|
};
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("pt-pt");
|
|
});
|
|
|
|
it("should return 'pt-br' for 'pt-BR'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("pt-BR");
|
|
};
|
|
|
|
expect(localeService.getPlatformForServerLocale()).toBe("pt-br");
|
|
});
|
|
});
|
|
|
|
describe("ValidateClientLocale", () =>
|
|
{
|
|
it("should return 'en' for 'en-US'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("en-US");
|
|
};
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("en");
|
|
});
|
|
|
|
it("should return 'kr' for 'ko-KR'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("ko-KR");
|
|
};
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("kr");
|
|
});
|
|
|
|
it("should return 'es-mx' for 'es-MX'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("es-MX");
|
|
};
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("es-mx");
|
|
});
|
|
|
|
it("should return 'cz' for 'cs-CZ'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("cs-CZ");
|
|
};
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("cz");
|
|
});
|
|
|
|
it("should return 'ge' for 'de-DE'", () =>
|
|
{
|
|
// Override the get locale so we control what the input is
|
|
localeService.getPlatformLocale = () =>
|
|
{
|
|
return new Intl.Locale("de-DE");
|
|
};
|
|
|
|
expect(localeService.getPlatformForClientLocale()).toBe("ge");
|
|
});
|
|
});
|
|
});
|