mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-20 14:37:43 +01:00
Write declaration file for new lib
This commit is contained in:
parent
97bd63ca39
commit
68c0444771
2 changed files with 28 additions and 25 deletions
21
src/@types/country-language.d.ts
vendored
Normal file
21
src/@types/country-language.d.ts
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
declare module "@ladjs/country-language" {
|
||||
export interface LanguageObj {
|
||||
countries: Array<{
|
||||
code_2: string;
|
||||
code_3: string;
|
||||
numCode: string;
|
||||
}>;
|
||||
direction: "RTL" | "LTR";
|
||||
name: string[];
|
||||
nativeName: string[];
|
||||
iso639_1: string;
|
||||
}
|
||||
|
||||
type Callback<T> = (err: null | string, result: null | T) => void;
|
||||
|
||||
declare namespace lib {
|
||||
function getLanguage(locale: string, cb: Callback<LanguageObj>): void;
|
||||
}
|
||||
|
||||
export = lib;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import countryLanguages from "@ladjs/country-language";
|
||||
import countryLanguages, { LanguageObj } from "@ladjs/country-language";
|
||||
import { getTag } from "@sozialhelden/ietf-language-tags";
|
||||
|
||||
const languageOrder = ["en", "hi", "fr", "de", "nl", "pt"];
|
||||
|
@ -48,18 +48,6 @@ export interface LocaleInfo {
|
|||
isRtl?: boolean;
|
||||
}
|
||||
|
||||
interface LanguageObj {
|
||||
countries: Array<{
|
||||
code_2: string;
|
||||
code_3: string;
|
||||
numCode: string;
|
||||
}>;
|
||||
direction: "RTL" | "LTR";
|
||||
name: string[];
|
||||
nativeName: string[];
|
||||
iso639_1: string;
|
||||
}
|
||||
|
||||
const extraLanguages: Record<string, LocaleInfo> = {
|
||||
pirate: {
|
||||
code: "pirate",
|
||||
|
@ -136,12 +124,9 @@ export function getCountryCodeForLocale(locale: string): string | null {
|
|||
const tag = getTag(locale, true);
|
||||
if (!tag?.language?.Subtag) return null;
|
||||
// this function isnt async, so its garuanteed to work like this
|
||||
countryLanguages.getLanguage(
|
||||
tag.language.Subtag,
|
||||
(_err: string, lang: LanguageObj) => {
|
||||
countryLanguages.getLanguage(tag.language.Subtag, (_err, lang) => {
|
||||
if (lang) output = lang;
|
||||
},
|
||||
);
|
||||
});
|
||||
if (!output) return null;
|
||||
if (output.countries.length === 0) return null;
|
||||
const priority = countryPriority[output.iso639_1.toLowerCase()];
|
||||
|
@ -169,12 +154,9 @@ export function getLocaleInfo(locale: string): LocaleInfo | null {
|
|||
|
||||
let output: LanguageObj | null = null as any as LanguageObj;
|
||||
// this function isnt async, so its garuanteed to work like this
|
||||
countryLanguages.getLanguage(
|
||||
tag.language.Subtag,
|
||||
(_err: string, lang: LanguageObj) => {
|
||||
countryLanguages.getLanguage(tag.language.Subtag, (_err, lang) => {
|
||||
if (lang) output = lang;
|
||||
},
|
||||
);
|
||||
});
|
||||
if (!output) return null;
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue