Skip to content

Commit

Permalink
fix and cleanup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jooy2 committed Sep 16, 2024
1 parent bd511f4 commit 8bd2e6c
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,43 +95,6 @@ export default class VitePressI18n {
return result;
}

private static getDefaultLabelValue(locale: string): string {
const findIndex = PLUGIN_SUPPORT_LOCALES.findIndex((x) => x.value === locale);
const fallbackLocaleIndex = PLUGIN_SUPPORT_LOCALES.findIndex(
(x) => x.value === FALLBACK_LOCALE
);

return findIndex !== -1
? PLUGIN_SUPPORT_LOCALES[findIndex].label
: PLUGIN_SUPPORT_LOCALES[fallbackLocaleIndex].label;
}

private static getDefaultLangValue(
options: Partial<VitePressI18nLocalesOptions>,
label: string,
locale: string
): object {
if (options.lang?.[label]) {
return { lang: options.lang?.[label] };
}

if (options.disableAutoSetLangValue) {
return {};
}

const findIndex = PLUGIN_SUPPORT_LOCALES.findIndex((x) => x.value === locale);
const fallbackLocaleIndex = PLUGIN_SUPPORT_LOCALES.findIndex(
(x) => x.value === FALLBACK_LOCALE
);

return {
lang:
findIndex !== -1
? PLUGIN_SUPPORT_LOCALES[findIndex].lang
: PLUGIN_SUPPORT_LOCALES[fallbackLocaleIndex].lang
};
}

static generateI18nSearch(
options: VitePressI18nSearchOptions
):
Expand All @@ -141,7 +104,7 @@ export default class VitePressI18n {
throw new Error(`You must pass 1 argument, see the documentation for details.`);
}

const result:
const tempResult:
| Record<string, Partial<Omit<LocalSearchOptions, 'locales'>>>
| Record<string, Partial<DocSearchProps>> = {};

Expand All @@ -154,17 +117,19 @@ export default class VitePressI18n {
}

if (options.provider === 'local') {
result[label === options.rootLocale ? 'root' : label] = LOCAL_SEARCH_TRANSLATIONS[locale];
tempResult[label === options.rootLocale ? 'root' : label] =
LOCAL_SEARCH_TRANSLATIONS[locale];
} else {
result[label === options.rootLocale ? 'root' : label] = ALGOLIA_SEARCH_TRANSLATIONS[locale];
tempResult[label === options.rootLocale ? 'root' : label] =
ALGOLIA_SEARCH_TRANSLATIONS[locale];
}
}

const result = {
provider: options.provider,
options: {
...options.options,
locales: result
locales: tempResult
}
};

Expand All @@ -178,6 +143,43 @@ export default class VitePressI18n {

return result;
}

private static getDefaultLabelValue(locale: string): string {
const findIndex = PLUGIN_SUPPORT_LOCALES.findIndex((x) => x.value === locale);
const fallbackLocaleIndex = PLUGIN_SUPPORT_LOCALES.findIndex(
(x) => x.value === FALLBACK_LOCALE
);

return findIndex !== -1
? PLUGIN_SUPPORT_LOCALES[findIndex].label
: PLUGIN_SUPPORT_LOCALES[fallbackLocaleIndex].label;
}

private static getDefaultLangValue(
options: Partial<VitePressI18nLocalesOptions>,
label: string,
locale: string
): object {
if (options.lang?.[label]) {
return { lang: options.lang?.[label] };
}

if (options.disableAutoSetLangValue) {
return {};
}

const findIndex = PLUGIN_SUPPORT_LOCALES.findIndex((x) => x.value === locale);
const fallbackLocaleIndex = PLUGIN_SUPPORT_LOCALES.findIndex(
(x) => x.value === FALLBACK_LOCALE
);

return {
lang:
findIndex !== -1
? PLUGIN_SUPPORT_LOCALES[findIndex].lang
: PLUGIN_SUPPORT_LOCALES[fallbackLocaleIndex].lang
};
}
}

export { VitePressI18n };
Expand Down

0 comments on commit 8bd2e6c

Please sign in to comment.