Skip to content

Commit 260e4cc

Browse files
committed
feat: adding the possibility to choose on wich language you want to display informations
this is just a first version that is not yet efficient and must be implemented to be more efficient
1 parent 7a89071 commit 260e4cc

File tree

4 files changed

+122
-26
lines changed

4 files changed

+122
-26
lines changed

commitizen/cli.py

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ def __call__(
108108
"required": False,
109109
"help": "comma separated error codes that won't rise error, e.g: cz -nr 1,2,3 bump. See codes at https://commitizen-tools.github.io/commitizen/exit_codes/",
110110
},
111+
{
112+
"name": ["-language"],
113+
"type": str,
114+
"default": "en",
115+
"help": "language of the commit message (default: en). Available languages: enGLISH, frENCH. others are welcome",
116+
},
111117
],
112118
"subcommands": {
113119
"title": "commands",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
prefix_en=Select the type of change you are committing
2+
fix_en=A bug fix. Correlates with PATCH in SemVer
3+
feat_en=A new feature. Correlates with MINOR in SemVer
4+
docs_en=Documentation only changes
5+
style_en=Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
6+
refactor_en=A code change that neither fixes a bug nor adds a feature
7+
perf_en=A code change that improves performance
8+
test_en=Adding missing or correcting existing tests
9+
build_en=Changes that affect the build system or external dependencies (example scopes: pip, docker, npm)
10+
ci_en=Changes to CI configuration files and scripts (example scopes: GitLabCI)
11+
scope_en=What is the scope of this change? (class or file name): (press [enter] to skip)
12+
subject_en=Write a short and imperative summary of the code changes: (lower case and no period)

commitizen/cz/conventional_commits/conventional_commits.py

+59-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
from commitizen import defaults
55
from commitizen.cz.base import BaseCommitizen
6+
from commitizen.cz.conventional_commits.translation_multilanguage import (
7+
translate_text_from_eng,
8+
)
69
from commitizen.cz.utils import multiple_line_breaker, required_validator
710
from commitizen.defaults import Questions
811

@@ -40,70 +43,98 @@ class ConventionalCommitsCz(BaseCommitizen):
4043
}
4144
changelog_pattern = defaults.bump_pattern
4245

43-
def questions(self) -> Questions:
46+
def questions(self, language: str) -> Questions:
4447
questions: Questions = [
4548
{
4649
"type": "list",
4750
"name": "prefix",
48-
"message": "Select the type of change you are committing",
51+
"message": translate_text_from_eng(
52+
"Select the type of change you are committing", language, "prefix"
53+
),
4954
"choices": [
5055
{
5156
"value": "fix",
52-
"name": "fix: A bug fix. Correlates with PATCH in SemVer",
57+
"name": "fix: "
58+
+ translate_text_from_eng(
59+
"A bug fix. Correlates with PATCH in SemVer",
60+
language,
61+
"fix",
62+
),
5363
"key": "x",
5464
},
5565
{
5666
"value": "feat",
57-
"name": "feat: A new feature. Correlates with MINOR in SemVer",
67+
"name": "feat: "
68+
+ translate_text_from_eng(
69+
"A new feature. Correlates with MINOR in SemVer",
70+
language,
71+
"feat",
72+
),
5873
"key": "f",
5974
},
6075
{
6176
"value": "docs",
62-
"name": "docs: Documentation only changes",
77+
"name": "docs: "
78+
+ translate_text_from_eng(
79+
"Documentation only changes", language, "docs"
80+
),
6381
"key": "d",
6482
},
6583
{
6684
"value": "style",
67-
"name": (
68-
"style: Changes that do not affect the "
69-
"meaning of the code (white-space, formatting,"
70-
" missing semi-colons, etc)"
85+
"name": "style: "
86+
+ translate_text_from_eng(
87+
"""Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)""",
88+
language,
89+
"style",
7190
),
7291
"key": "s",
7392
},
7493
{
7594
"value": "refactor",
76-
"name": (
77-
"refactor: A code change that neither fixes "
78-
"a bug nor adds a feature"
95+
"name": "refactor: "
96+
+ translate_text_from_eng(
97+
"""A code change that neither fixes a bug nor adds a feature""",
98+
language,
99+
"refactor",
79100
),
80101
"key": "r",
81102
},
82103
{
83104
"value": "perf",
84-
"name": "perf: A code change that improves performance",
105+
"name": "perf: "
106+
+ translate_text_from_eng(
107+
"A code change that improves performance", language, "perf"
108+
),
85109
"key": "p",
86110
},
87111
{
88112
"value": "test",
89-
"name": (
90-
"test: Adding missing or correcting " "existing tests"
113+
"name": "test: "
114+
+ translate_text_from_eng(
115+
"Adding missing or correcting existing tests",
116+
language,
117+
"test",
91118
),
92119
"key": "t",
93120
},
94121
{
95122
"value": "build",
96-
"name": (
97-
"build: Changes that affect the build system or "
98-
"external dependencies (example scopes: pip, docker, npm)"
123+
"name": "build: "
124+
+ translate_text_from_eng(
125+
"""Changes that affect the build system or external dependencies (example scopes: pip, docker, npm)""",
126+
language,
127+
"build",
99128
),
100129
"key": "b",
101130
},
102131
{
103132
"value": "ci",
104-
"name": (
105-
"ci: Changes to CI configuration files and "
106-
"scripts (example scopes: GitLabCI)"
133+
"name": "ci: "
134+
+ translate_text_from_eng(
135+
"""Changes to CI configuration files and scripts (example scopes: GitLabCI)""",
136+
language,
137+
"ci",
107138
),
108139
"key": "c",
109140
},
@@ -112,18 +143,20 @@ def questions(self) -> Questions:
112143
{
113144
"type": "input",
114145
"name": "scope",
115-
"message": (
116-
"What is the scope of this change? (class or file name): (press [enter] to skip)\n"
146+
"message": translate_text_from_eng(
147+
"What is the scope of this change? (class or file name): (press [enter] to skip)\n",
148+
language,
149+
"scope",
117150
),
118151
"filter": parse_scope,
119152
},
120153
{
121154
"type": "input",
122155
"name": "subject",
123156
"filter": parse_subject,
124-
"message": (
125-
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
126-
),
157+
"message": translate_text_from_eng(
158+
"Write a short and imperative summary of the code changes: (lower case and no period)\n",
159+
language, "subject"),
127160
},
128161
{
129162
"type": "input",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from translate import Translator
2+
3+
FILENAME = "commitizen/cz/conventional_commits/.cache_multilanguage.txt"
4+
MULTILANGUAGE = {}
5+
6+
7+
def load_multilanguage():
8+
global MULTILANGUAGE
9+
MULTILANGUAGE = {}
10+
with open(FILENAME) as file:
11+
for line in file:
12+
if not line.strip():
13+
continue
14+
key, value = line.strip().split("=", 1)
15+
MULTILANGUAGE[key] = value
16+
17+
def generate_key(original_key, to_lang):
18+
return f"{original_key}_{to_lang}"
19+
20+
def save_multilanguage(key, value):
21+
if "\n" in value:
22+
value = value.replace("\n", "")
23+
with open(FILENAME, "a") as file:
24+
file.write(f"{key}={value}\n")
25+
26+
def translate_text(text, from_lang, to_lang):
27+
translator = Translator(from_lang=from_lang, to_lang=to_lang)
28+
translation = translator.translate(text)
29+
return translation
30+
31+
def translate_text_from_eng(text, to_lang, key):
32+
global MULTILANGUAGE
33+
key_generated = generate_key(key, to_lang)
34+
if not MULTILANGUAGE:
35+
load_multilanguage()
36+
if key_generated in MULTILANGUAGE:
37+
return MULTILANGUAGE[key_generated]
38+
else:
39+
if to_lang == "en":
40+
MULTILANGUAGE[key_generated] = text
41+
save_multilanguage(key_generated, MULTILANGUAGE[key_generated])
42+
return text
43+
MULTILANGUAGE[key_generated] = translate_text(text, "en", to_lang)
44+
save_multilanguage(key_generated, MULTILANGUAGE[key_generated])
45+
return MULTILANGUAGE[key_generated]

0 commit comments

Comments
 (0)