mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-19 19:21:33 +10:00
feat(homepage): add new sections to homepage
This commit is contained in:
@ -1,34 +1,31 @@
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { Controller, Get, Header, Param } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
import { Config } from "../config/schema";
|
||||
import { UtilsService } from "../utils/utils.service";
|
||||
import { TranslationService } from "./translation.service";
|
||||
|
||||
@Controller("translation")
|
||||
export class TranslationController {
|
||||
constructor(
|
||||
private readonly httpService: HttpService,
|
||||
private readonly configService: ConfigService<Config>,
|
||||
private readonly translationService: TranslationService,
|
||||
private readonly utils: UtilsService,
|
||||
) {}
|
||||
|
||||
private async fetchTranslations(locale: string) {
|
||||
const distributionHash = this.configService.get("CROWDIN_DISTRIBUTION_HASH");
|
||||
const response = await this.httpService.axiosRef.get(
|
||||
`https://distributions.crowdin.net/${distributionHash}/content/${locale}/messages.json`,
|
||||
@Get("/languages")
|
||||
async languages() {
|
||||
return this.utils.getCachedOrSet(
|
||||
`translation:languages`,
|
||||
async () => this.translationService.fetchLanguages(),
|
||||
1000 * 60 * 60 * 24, // 24 hours
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
@Get("/:locale")
|
||||
@Header("Content-Type", "application/octet-stream")
|
||||
@Header("Content-Disposition", 'attachment; filename="messages.po"')
|
||||
async getTranslation(@Param("locale") locale: string) {
|
||||
async translation(@Param("locale") locale: string) {
|
||||
return this.utils.getCachedOrSet(
|
||||
`translation:${locale}`,
|
||||
async () => this.fetchTranslations(locale),
|
||||
async () => this.translationService.fetchTranslations(locale),
|
||||
1000 * 60 * 60 * 24, // 24 hours
|
||||
);
|
||||
}
|
||||
|
||||
@ -2,9 +2,11 @@ import { HttpModule } from "@nestjs/axios";
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { TranslationController } from "./translation.controller";
|
||||
import { TranslationService } from "./translation.service";
|
||||
|
||||
@Module({
|
||||
imports: [HttpModule],
|
||||
controllers: [TranslationController],
|
||||
providers: [TranslationService],
|
||||
})
|
||||
export class TranslationModule {}
|
||||
|
||||
53
apps/server/src/translation/translation.service.ts
Normal file
53
apps/server/src/translation/translation.service.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { LanguageDto } from "@reactive-resume/dto";
|
||||
|
||||
import { Config } from "../config/schema";
|
||||
|
||||
type CrowdinResponse = {
|
||||
data: {
|
||||
data: {
|
||||
language: { id: string; name: string; locale: string; editorCode: string };
|
||||
translationProgress: number;
|
||||
};
|
||||
}[];
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class TranslationService {
|
||||
constructor(
|
||||
private readonly httpService: HttpService,
|
||||
private readonly configService: ConfigService<Config>,
|
||||
) {}
|
||||
|
||||
async fetchTranslations(locale: string) {
|
||||
const distributionHash = this.configService.get("CROWDIN_DISTRIBUTION_HASH");
|
||||
const response = await this.httpService.axiosRef.get(
|
||||
`https://distributions.crowdin.net/${distributionHash}/content/${locale}/messages.json`,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async fetchLanguages() {
|
||||
const projectId = this.configService.get("CROWDIN_PROJECT_ID");
|
||||
const accessToken = this.configService.get("CROWDIN_ACCESS_TOKEN");
|
||||
|
||||
const response = await this.httpService.axiosRef.get(
|
||||
`https://api.crowdin.com/api/v2/projects/${projectId}/languages/progress?limit=100`,
|
||||
{ headers: { Authorization: `Bearer ${accessToken}` } },
|
||||
);
|
||||
const { data } = response.data as CrowdinResponse;
|
||||
|
||||
return data.map(({ data }) => {
|
||||
return {
|
||||
id: data.language.id,
|
||||
name: data.language.name,
|
||||
progress: data.translationProgress,
|
||||
editorCode: data.language.editorCode,
|
||||
locale: data.language.locale,
|
||||
} satisfies LanguageDto;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user