Skip to content

Commit

Permalink
feat (rotor): Add localized names for continents, countries, regions,…
Browse files Browse the repository at this point in the history
… and cities (#1155)
  • Loading branch information
dmeremyanin authored Dec 17, 2024
1 parent 61c168e commit 344de67
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions libs/core-functions/src/functions/lib/udf_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export async function UDFTestRun({
},
region: {
code: "NY",
name: "New York",
},
location: {
latitude: 40.6808,
Expand Down
18 changes: 15 additions & 3 deletions services/rotor/src/lib/maxmind.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Reader, ReaderModel, City, Isp } from "@maxmind/geoip2-node";
import { Reader, ReaderModel, City, Isp, Names } from "@maxmind/geoip2-node";
import * as zlib from "zlib";
import * as tar from "tar";
import { Geo } from "@jitsu/protocols/analytics";
Expand All @@ -16,6 +16,8 @@ type Edition = PaidEdition | FreeEditions | "NotRequired" | "";

type LoadFunction = (edition: Edition) => Promise<Buffer>;

type GetLocalizedNameFunction = (names: Names) => string;

const composeURL = (licenseKeyOrURL: string, edition: Edition) => {
if (licenseKeyOrURL.startsWith("http")) {
return licenseKeyOrURL + (licenseKeyOrURL.endsWith("/") ? "" : "/") + edition + ".tar.gz";
Expand Down Expand Up @@ -68,6 +70,14 @@ export async function initMaxMindClient(opts: {
} else {
loadFunc = (edition: Edition) => loadFromURL(composeURL(licenseKey || url || "", edition));
}
let getLocalizedName: GetLocalizedNameFunction;
if (process.env.MAXMIND_LOCALE) {
getLocalizedName = (names: Names) => {
return names[process.env.MAXMIND_LOCALE!] || names.en;
};
} else {
getLocalizedName = (names: Names) => names.en;
}

let cityReader: ReaderModel | undefined;
let countryReader: ReaderModel | undefined;
Expand Down Expand Up @@ -136,25 +146,27 @@ export async function initMaxMindClient(opts: {
continent: geo.continent
? {
code: geo.continent.code,
name: getLocalizedName(geo.continent.names),
}
: undefined,
country: geo.country
? {
code: geo.country.isoCode,
name: geo.country.names.en,
name: getLocalizedName(geo.country.names),
isEU: !!geo.country.isInEuropeanUnion,
}
: undefined,
region: geo.subdivisions?.length
? {
code: geo.subdivisions[0].isoCode,
confidence: geo.subdivisions[0].confidence,
name: getLocalizedName(geo.subdivisions[0].names),
}
: undefined,
city: geo.city
? {
confidence: geo.city.confidence,
name: geo.city.names.en,
name: getLocalizedName(geo.city.names),
}
: undefined,
postalCode: geo.postal
Expand Down
14 changes: 14 additions & 0 deletions types/protocols/analytics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ export type WithConfidence<T> = T & {
export type Geo = {
continent?: {
code: "AF" | "AN" | "AS" | "EU" | "NA" | "OC" | "SA";
/**
* Localized name of the continent
*/
name: string;
};
country?: {
/**
* Two-letter country code (ISO 3166-1 alpha-2): https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
*/
code: string;
/**
* Localized name of the country
*/
name: string;
isEU: boolean;
};
Expand All @@ -27,8 +34,15 @@ export type Geo = {
* For USA it's two-letter capitaluzed state code (such as NY)
*/
code: string;
/**
* Localized name of the region
*/
name: string;
}>;
city?: WithConfidence<{
/**
* Localized name of the city
*/
name: string;
}>;

Expand Down

0 comments on commit 344de67

Please sign in to comment.