Skip to content

Commit

Permalink
add country info
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Oct 26, 2023
1 parent 0cd4113 commit 9c28d1d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/api/marneApi.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/locales/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -80,4 +90,18 @@ export const getLanguage = (): string => {
return language;
};

export const getCurrentCountry = (): Promise<string> => {
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;

0 comments on commit 9c28d1d

Please sign in to comment.