Skip to content

Commit 5ae52cd

Browse files
authored
🚀 i18n: add internationalization support (#31)
* 🚀 i18n: add internationalization support
1 parent d4fc651 commit 5ae52cd

File tree

11 files changed

+116
-7
lines changed

11 files changed

+116
-7
lines changed

src/commands/config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { homedir } from 'os';
66
import { intro, outro } from '@clack/prompts';
77
import chalk from 'chalk';
88
import { COMMANDS } from '../CommandsEnum';
9+
import { getI18nLocal } from '../i18n';
910

1011
export enum CONFIG_KEYS {
1112
OPENAI_API_KEY = 'OPENAI_API_KEY',
1213
description = 'description',
13-
emoji = 'emoji'
14+
emoji = 'emoji',
15+
language = 'language'
1416
}
1517

1618
export enum CONFIG_MODES {
@@ -64,6 +66,14 @@ export const configValidators = {
6466
);
6567

6668
return value;
69+
},
70+
[CONFIG_KEYS.language](value: any) {
71+
validateConfig(
72+
CONFIG_KEYS.language,
73+
getI18nLocal(value),
74+
`${value} is not supported yet`
75+
);
76+
return getI18nLocal(value);
6777
}
6878
};
6979

src/generateCommitMessageFromGitDiff.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import {
55
import { api } from './api';
66
import { getConfig } from './commands/config';
77
import { mergeStrings } from './utils/mergeStrings';
8+
import { i18n, I18nLocals } from './i18n';
89

910
const config = getConfig();
11+
const translation = i18n[config?.language as I18nLocals || 'en']
1012

1113
const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
1214
{
@@ -18,8 +20,8 @@ const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
1820
}, use the present tense. ${
1921
config?.description
2022
? 'Add a short description of what commit is about after the commit message. Don\'t start it with "This commit", just describe the changes.'
21-
: "Don't add any descriptions to the commit, only commit message."
22-
}`
23+
: 'Don\'t add any descriptions to the commit, only commit message.'
24+
} Use ${translation.localLanguage} to answer.}`
2325
},
2426
{
2527
role: ChatCompletionRequestMessageRoleEnum.User,
@@ -48,10 +50,9 @@ const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
4850
},
4951
{
5052
role: ChatCompletionRequestMessageRoleEnum.Assistant,
51-
// prettier-ignore
52-
content: `${config?.emoji ? '🐛 ' : ''}fix(server.ts): change port variable case from lowercase port to uppercase PORT
53-
${config?.emoji ? '✨ ' : ''}feat(server.ts): add support for process.env.PORT environment variable
54-
${config?.description ? 'The port variable is now named PORT, which improves consistency with the naming conventions as PORT is a constant. Support for an environment variable allows the application to be more flexible as it can now run on any available port specified via the process.env.PORT environment variable.' : ''}`
53+
content: `${config?.emoji ? '🐛 ' : ''}${translation.commitFix}
54+
${config?.emoji ? '✨ ' : ''}${translation.commitFeat}
55+
${config?.description ? translation.commitDescription : ''}`
5556
}
5657
];
5758

src/i18n/de.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "Deutsch",
3+
"commitFix": "fix(server.ts): Ändere die Groß- und Kleinschreibung der Port-Variable von Kleinbuchstaben auf Großbuchstaben PORT.",
4+
"commitFeat": "Funktion(server.ts): Unterstützung für die Umgebungsvariable process.env.PORT hinzufügen",
5+
"commitDescription": "Die Port-Variable heißt jetzt PORT, was die Konsistenz mit den Namenskonventionen verbessert, da PORT eine Konstante ist. Die Unterstützung für eine Umgebungsvariable ermöglicht es der Anwendung, flexibler zu sein, da sie jetzt auf jedem verfügbaren Port laufen kann, der über die Umgebungsvariable process.env.PORT angegeben wird."
6+
}

src/i18n/en.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "english",
3+
"commitFix": "fix(server.ts): change port variable case from lowercase port to uppercase PORT",
4+
"commitFeat": "feat(server.ts): add support for process.env.PORT environment variable",
5+
"commitDescription": "The port variable is now named PORT, which improves consistency with the naming conventions as PORT is a constant. Support for an environment variable allows the application to be more flexible as it can now run on any available port specified via the process.env.PORT environment variable."
6+
}

src/i18n/fr.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "française",
3+
"commitFix": "corriger(server.ts) : changer la casse de la variable de port de minuscules à majuscules (PORT)",
4+
"commitFeat": "fonctionnalité(server.ts) : ajouter la prise en charge de la variable d'environnement process.env.PORT",
5+
"commitDescription": "La variable de port est maintenant nommée PORT, ce qui améliore la cohérence avec les conventions de nommage car PORT est une constante. La prise en charge d'une variable d'environnement permet à l'application d'être plus flexible car elle peut maintenant s'exécuter sur n'importe quel port disponible spécifié via la variable d'environnement process.env.PORT."
6+
}

src/i18n/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import en from '../i18n/en.json' assert { type: 'json' };
2+
import de from '../i18n/de.json' assert { type: 'json' };
3+
import fr from '../i18n/fr.json' assert { type: 'json' };
4+
import it from '../i18n/it.json' assert { type: 'json' };
5+
import ko from '../i18n/ko.json' assert { type: 'json' };
6+
import zh_CN from '../i18n/zh_CN.json' assert { type: 'json' };
7+
import zh_TW from '../i18n/zh_TW.json' assert { type: 'json' };
8+
import ja from '../i18n/ja.json' assert { type: 'json' };
9+
10+
export enum I18nLocals {
11+
'en' = 'en',
12+
'zh_CN' = 'zh_CN',
13+
'zh_TW' = 'zh_TW',
14+
'ja' = 'ja',
15+
'de' = 'de',
16+
'fr' = 'fr',
17+
'it' = 'it',
18+
'ko' = 'ko'
19+
};
20+
21+
export const i18n = {
22+
en,
23+
zh_CN,
24+
zh_TW,
25+
ja,
26+
de,
27+
fr,
28+
it,
29+
ko,
30+
};
31+
32+
export const I18N_CONFIG_ALIAS: { [key: string]: string[] } = {
33+
zh_CN: ['zh_CN', '简体中文', '中文', '简体'],
34+
zh_TW: ['zh_TW', '繁體中文', '繁體'],
35+
ja: ['ja', 'Japanese', 'にほんご'],
36+
ko: ['ko', 'Korean', '한국어'],
37+
de: ['de', 'German' ,'Deutsch'],
38+
fr: ['fr', 'French', 'française'],
39+
it: ['it', 'Italian', 'italiano'],
40+
};
41+
42+
export function getI18nLocal(value: string): string | boolean {
43+
for (const key in I18N_CONFIG_ALIAS) {
44+
const aliases = I18N_CONFIG_ALIAS[key];
45+
if (aliases.includes(value)) {
46+
return key;
47+
}
48+
}
49+
return false;
50+
}

src/i18n/it.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "italiano",
3+
"commitFix": "fix(server.ts): cambia il caso della variabile di porta da minuscolo port a maiuscolo PORT",
4+
"commitFeat": "funzionalità(server.ts): aggiungi supporto per la variabile di ambiente process.env.PORT",
5+
"commitDescription": "La variabile di porta è ora chiamata PORT, il che migliora la coerenza con le convenzioni di denominazione in quanto PORT è una costante. Il supporto per una variabile di ambiente consente all'applicazione di essere più flessibile poiché ora può essere eseguita su qualsiasi porta disponibile specificata tramite la variabile di ambiente process.env.PORT."
6+
}

src/i18n/ja.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "にほんご",
3+
"commitFix": "修正(server.ts): ポート変数のケースを小文字のポートから大文字のPORTに変更",
4+
"commitFeat": "新機能(server.ts): process.env.PORT環境変数のサポートを追加する",
5+
"commitDescription": "ポート変数は現在PORTという名前になっており、PORTは定数であるため命名規則に一貫性があります。環境変数のサポートにより、アプリケーションはより柔軟になり、process.env.PORT環境変数で指定された任意の利用可能なポートで実行できるようになりまし"
6+
}

src/i18n/ko.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "한국어",
3+
"commitFix": "fix(server.ts): 포트 변수를 소문자 port에서 대문자 PORT로 변경",
4+
"commitFeat": "피트(server.ts): process.env.PORT 환경 변수 지원 추가",
5+
"commitDescription": "포트 변수는 이제 PORT로 이름이 지정되어 상수인 PORT와 일관성 있는 이름 규칙을 따릅니다. 환경 변수 지원을 통해 애플리케이션은 이제 process.env.PORT 환경 변수로 지정된 사용 가능한 모든 포트에서 실행할 수 있으므로 더 유연해졌습니다."
6+
}

src/i18n/zh_CN.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"localLanguage": "简体中文",
3+
"commitFix": "修复(server.ts):将端口变量从小写port改为大写PORT",
4+
"commitFeat": "功能(server.ts):添加对process.env.PORT环境变量的支持",
5+
"commitDescription": "现在端口变量被命名为PORT,这提高了命名约定的一致性,因为PORT是一个常量。环境变量的支持使应用程序更加灵活,因为它现在可以通过process.env.PORT环境变量在任何可用端口上运行。"
6+
}

0 commit comments

Comments
 (0)