File size: 1,788 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { HttpService } from "@nestjs/axios";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Language, languages } from "@reactive-resume/utils";

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 fetchLanguages() {
    try {
      const projectId = this.configService.getOrThrow("CROWDIN_PROJECT_ID");
      const accessToken = this.configService.getOrThrow("CROWDIN_PERSONAL_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;

      // Add English Locale
      data.push({
        data: {
          language: {
            id: "en-US",
            locale: "en-US",
            editorCode: "en",
            name: "English",
          },
          translationProgress: 100,
        },
      });

      data.sort((a, b) => a.data.language.name.localeCompare(b.data.language.name));

      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 Language;
      });
    } catch {
      return languages;
    }
  }
}