From dc6c15b721a98fddae3f78ee50961eb9f1e6b598 Mon Sep 17 00:00:00 2001 From: Caio Campos <20524962+caiocampos@users.noreply.github.com> Date: Sun, 26 Jan 2025 17:59:24 -0300 Subject: [PATCH] chore: change logic --- src/pages/home/[language]/index.tsx | 21 ++++---- src/utils/language-utils.ts | 82 +++++++++++++---------------- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/pages/home/[language]/index.tsx b/src/pages/home/[language]/index.tsx index f7a19b9c6..8e66f8a2f 100644 --- a/src/pages/home/[language]/index.tsx +++ b/src/pages/home/[language]/index.tsx @@ -60,20 +60,19 @@ export const getStaticProps: GetStaticProps = if (!hasLanguage) { language = LanguageEnum.Portuguese; } - const termTranslation: TermTranslation = await getTermTranslation(language); - const repositoriesBruteData: MinimalRepository[] = ( - await getRepositoriesTranslation( - await GithubServices.getCachedAllUserRepos( - configuration.user_login, - configuration.orgs_login - ), - language - ) + const repositoriesBruteData: MinimalRepository[] = + await GithubServices.getCachedAllUserRepos( + configuration.user_login, + configuration.orgs_login + ); + const repositoriesTranslatedData: MinimalRepository[] = ( + await getRepositoriesTranslation(repositoriesBruteData, language) ).sort(repositoryComparison); + const termTranslation: TermTranslation = await getTermTranslation(language); const repositories: RepositoryData[] = - repositoriesBruteData.map(parseRepositoryData); + repositoriesTranslatedData.map(parseRepositoryData); const wordDictionary = createRepositoryWordDictionary( - repositoriesBruteData, + repositoriesTranslatedData, termTranslation ); return { diff --git a/src/utils/language-utils.ts b/src/utils/language-utils.ts index e885fc69b..4f9584e7c 100644 --- a/src/utils/language-utils.ts +++ b/src/utils/language-utils.ts @@ -74,56 +74,46 @@ export const generateParams = (langs: Language[]): PageParams[] => language, })); +const keys: (keyof TermTranslation)[] = [ + "archived", + "page", + "search", + "source", + "autotranslated", + "other", + "toggleTheme", + "light", + "dark", + "system", +]; + +const defaultPageSize = 2; + export const getTermTranslation = async ( - language: Language + language: Language, + pageSize = defaultPageSize ): Promise => { if (isLanguagePT(language)) { return termTranslationPT; } - const keys: (keyof TermTranslation)[] = [ - "archived", - "page", - "search", - "source", - "autotranslated", - "other", - "toggleTheme", - "light", - "dark", - "system", - ]; - const promises = keys.map((key) => - LibretranslateServices.translate( - termTranslationPTForTranslation[key], - PT, - language - ) - ); - - const [ - archived, - page, - search, - source, - autotranslated, - other, - toggleTheme, - light, - dark, - system, - ] = await Promise.all(promises); - return { - archived, - page, - search, - source, - autotranslated, - other, - toggleTheme, - light, - dark, - system, - }; + const base = [...keys]; + const out: TermTranslation = { ...termTranslationPT }; + while (base.length > 0) { + const page = base.splice(0, pageSize); + const promises = page.map(async (key) => ({ + key, + value: await LibretranslateServices.translate( + termTranslationPTForTranslation[key], + PT, + language + ), + })); + const translatedPage = await Promise.all(promises); + translatedPage.forEach(({ key, value }) => { + out[key] = value; + }); + } + return out; }; export const getRepositoryTranslation = async ( @@ -147,7 +137,7 @@ export const getRepositoryTranslation = async ( export const getRepositoriesTranslation = async ( repositories: MinimalRepository[], language: Language, - pageSize = 3 + pageSize = defaultPageSize ): Promise => { const base = [...repositories]; if (isLanguagePT(language)) {