Default languages with env & langcodes typed (#94)

This commit is contained in:
David
2022-02-24 19:53:31 +01:00
committed by GitHub
parent 5e4db73a9f
commit 5856776785
15 changed files with 89 additions and 50 deletions

View File

@@ -1,6 +1,8 @@
import languagesJson from "./languages.json";
const { languages, exceptions, mappings } = languagesJson;
export type LangCode = keyof typeof languages;
const checkTypes = {
exception: exceptions,
mapping: mappings
@@ -20,10 +22,10 @@ const isKeyOf = <T extends object>(obj: T) => (key: keyof any): key is keyof T =
export function replaceBoth(
checkType: CheckType,
langs: {
[key in LangType]: string
[key in LangType]: LangCode
}
): {
[key in LangType]: string
[key in LangType]: LangCode
} {
const [source, target] = langTypes.map(langType => {
const object = checkTypes[checkType][langType];
@@ -33,8 +35,8 @@ export function replaceBoth(
return { source, target };
}
export function retrieveFromType(type?: LangType): [string, string][] {
const langEntries = Object.entries(languages);
export function retrieveFromType(type?: LangType) {
const langEntries = Object.entries(languages) as [LangCode, string][];
if (!type)
return langEntries;
@@ -43,6 +45,10 @@ export function retrieveFromType(type?: LangType): [string, string][] {
));
}
export function getName(code: string): string | null {
return isKeyOf(languages)(code) ? languages[code] : null;
export function isValid(code: string | null | undefined): code is LangCode {
return !!code && isKeyOf(languages)(code);
}
export function getName(code: string): string | null {
return isValid(code) ? languages[code] : null;
}

View File

@@ -1,16 +1,26 @@
import { replaceBoth } from "./language";
import { replaceBoth, isValid, LangCode } from "./language";
export const initialState = {
source: "auto",
target: "en",
const defaultSourceLang = process.env["NEXT_PUBLIC_DEFAULT_SOURCE_LANG"];
const defaultTargetLang = process.env["NEXT_PUBLIC_DEFAULT_TARGET_LANG"];
type State = {
source: LangCode,
target: LangCode,
query: string,
delayedQuery: string,
translation: string,
isLoading: boolean
}
export const initialState: State = {
source: isValid(defaultSourceLang) ? defaultSourceLang : "auto",
target: isValid(defaultTargetLang) ? defaultTargetLang : "en",
query: "",
delayedQuery: "",
translation: "",
isLoading: true
}
type State = typeof initialState;
export enum Actions {
SET_FIELD,
SET_ALL,
@@ -32,7 +42,7 @@ type Action = {
type: Actions.SWITCH_LANGS
}
export default function reducer(state: State, action: Action) {
export default function reducer(state: State, action: Action): State {
const { source, target } = replaceBoth("exception", {
source: state.target,
target: state.source

View File

@@ -1,10 +1,10 @@
import UserAgent from "user-agents";
import cheerio from "cheerio";
import { replaceBoth } from "./language";
import { replaceBoth, LangCode } from "./language";
export async function googleScrape(
source: string,
target: string,
source: LangCode,
target: LangCode,
query: string
): Promise<{
translationRes: string
@@ -57,8 +57,8 @@ export function extractSlug(slug: string[]): {
}
}
export async function textToSpeechScrape(lang: string, text: string) {
const { target: parsedLang } = replaceBoth("mapping", { source: "", target: lang });
export async function textToSpeechScrape(lang: LangCode, text: string) {
const { target: parsedLang } = replaceBoth("mapping", { source: "auto", target: lang });
const lastSpace = text.lastIndexOf(" ", 200);
const slicedText = text.slice(0, text.length > 200 && lastSpace !== -1 ? lastSpace : 200);