Skip to content

Commit

Permalink
chore: change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
caiocampos committed Jan 26, 2025
1 parent fc8e479 commit dc6c15b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 57 deletions.
21 changes: 10 additions & 11 deletions src/pages/home/[language]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,19 @@ export const getStaticProps: GetStaticProps<RepositoriesGetStaticProps> =
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 {
Expand Down
82 changes: 36 additions & 46 deletions src/utils/language-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TermTranslation> => {
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 (
Expand All @@ -147,7 +137,7 @@ export const getRepositoryTranslation = async (
export const getRepositoriesTranslation = async (
repositories: MinimalRepository[],
language: Language,
pageSize = 3
pageSize = defaultPageSize
): Promise<MinimalRepository[]> => {
const base = [...repositories];
if (isLanguagePT(language)) {
Expand Down

0 comments on commit dc6c15b

Please sign in to comment.