From 9c28d1d98e1775ac46a3d3d983080fbea039632b Mon Sep 17 00:00:00 2001 From: Zefanja Jobse Date: Thu, 26 Oct 2023 22:02:48 +0200 Subject: [PATCH] add country info --- package.json | 1 + pnpm-lock.yaml | 14 ++++++++++++++ src/api/marneApi.tsx | 5 ++++- src/locales/config.ts | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a8a4549..d05f2d1 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "chartjs-plugin-zoom": "^2.0.1", "date-fns": "^2.30.0", "file-saver": "^2.0.5", + "i18n-iso-countries": "^7.7.0", "i18next": "^23.5.1", "i18next-browser-languagedetector": "^7.1.0", "react-chartjs-2": "^5.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b0df2e0..c115650 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ dependencies: file-saver: specifier: ^2.0.5 version: 2.0.5 + i18n-iso-countries: + specifier: ^7.7.0 + version: 7.7.0 i18next: specifier: ^23.5.1 version: 23.5.1 @@ -3842,6 +3845,10 @@ packages: resolution: {integrity: sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==} dev: true + /diacritics@1.3.0: + resolution: {integrity: sha512-wlwEkqcsaxvPJML+rDh/2iS824jbREk6DUMUKkEaSlxdYHeS43cClJtsWglvw2RfeXGm6ohKDqsXteJ5sP5enA==} + dev: false + /diff-sequences@29.6.3: resolution: {integrity: sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -4999,6 +5006,13 @@ packages: resolution: {integrity: sha512-ygGZLjmXfPHj+ZWh6LwbC37l43MhfztxetbFCoYTM2VjkIUpeHgSNn7QIyVFj7YQ1Wl9Cbw5sholVJPzWvC2MQ==} dev: false + /i18n-iso-countries@7.7.0: + resolution: {integrity: sha512-07zMatrSsR1Z+cnxW//7s14Xf4v5g6U6ORHPaH8+Ox4uPqV+y46Uq78veYV8H1DKTr76EfdjSeaTxHpnaYq+bw==} + engines: {node: '>= 12'} + dependencies: + diacritics: 1.3.0 + dev: false + /i18next-browser-languagedetector@7.1.0: resolution: {integrity: sha512-cr2k7u1XJJ4HTOjM9GyOMtbOA47RtUoWRAtt52z43r3AoMs2StYKyjS3URPhzHaf+mn10hY9dZWamga5WPQjhA==} dependencies: diff --git a/src/api/marneApi.tsx b/src/api/marneApi.tsx index 6daacd5..363617b 100644 --- a/src/api/marneApi.tsx +++ b/src/api/marneApi.tsx @@ -1,5 +1,7 @@ +import { getCurrentCountry } from "../locales/config"; import JsonClient from "./Json"; import { ServerSearch } from "./ReturnTypes"; +import { getName } from "i18n-iso-countries"; export interface PlayerReturn { name: string; @@ -183,7 +185,6 @@ export class ApiProvider extends JsonClient { "Xpack4/Levels/MP/MP_River/MP_River": "https://cdn.gametools.network/maps/bf1/MP_River_LandscapeLarge-21443ae9.jpg", }; - if ( this.serverCacheAge === undefined || // update only once every 30 seconds @@ -193,6 +194,7 @@ export class ApiProvider extends JsonClient { this.serverCache = await r.json(); this.serverCacheAge = Date.now(); } + const country = await getCurrentCountry(); const servers = this.serverCache.servers .map((server) => { return { @@ -205,6 +207,7 @@ export class ApiProvider extends JsonClient { official: false, ownerId: 0, region: server.region, + country: getName(server.country, country), platform: "pc", playerAmount: server.currentPlayers, maxPlayerAmount: server.maxPlayers, diff --git a/src/locales/config.ts b/src/locales/config.ts index 7a8c9b2..f7a05d3 100644 --- a/src/locales/config.ts +++ b/src/locales/config.ts @@ -9,6 +9,7 @@ import { initReactI18next } from "react-i18next"; import LanguageDetector from "i18next-browser-languagedetector"; import { format, formatDistanceToNowStrict } from "date-fns"; import { enUS, tr, zhCN, nl, ru, de } from "date-fns/locale"; +import { registerLocale } from "i18n-iso-countries"; const locales = { "en-US": enUS, @@ -72,6 +73,15 @@ export const apiLanguage = { "tr-TR": "en-US", }; +export const apiCountry = { + "en-us": "en", + "zh-cn": "zh", + "nl-nl": "nl", + "tr-tr": "tr", + "ru-ru": "ru", + "de-de": "de", +}; + export const getLanguage = (): string => { let language = window.localStorage.i18nextLng.toLowerCase(); if (language in apiLanguage) { @@ -80,4 +90,18 @@ export const getLanguage = (): string => { return language; }; +export const getCurrentCountry = (): Promise => { + const language = window.localStorage.i18nextLng.toLowerCase(); + let country = "en"; + if (language in apiCountry) { + country = apiCountry[language]; + } + return import(`i18n-iso-countries/langs/${country}.json`).then( + (countries) => { + registerLocale(countries); + return country; + }, + ); +}; + export default i18n;