Skip to content

Multi-Language Version #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def __call__(
"required": False,
"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/",
},
{
"name": ["-language"],
"type": str,
"default": "en",
"help": "language of the commit message (default: en). Available languages: enGLISH, frENCH. others are welcome",
},
],
"subcommands": {
"title": "commands",
Expand Down
12 changes: 12 additions & 0 deletions commitizen/cz/conventional_commits/.cache_multilanguage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix_en=Select the type of change you are committing
fix_en=A bug fix. Correlates with PATCH in SemVer
feat_en=A new feature. Correlates with MINOR in SemVer
docs_en=Documentation only changes
style_en=Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
refactor_en=A code change that neither fixes a bug nor adds a feature
perf_en=A code change that improves performance
test_en=Adding missing or correcting existing tests
build_en=Changes that affect the build system or external dependencies (example scopes: pip, docker, npm)
ci_en=Changes to CI configuration files and scripts (example scopes: GitLabCI)
scope_en=What is the scope of this change? (class or file name): (press [enter] to skip)
subject_en=Write a short and imperative summary of the code changes: (lower case and no period)
85 changes: 59 additions & 26 deletions commitizen/cz/conventional_commits/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from commitizen import defaults
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.conventional_commits.translation_multilanguage import (
translate_text_from_eng,
)
from commitizen.cz.utils import multiple_line_breaker, required_validator
from commitizen.defaults import Questions

Expand Down Expand Up @@ -40,70 +43,98 @@ class ConventionalCommitsCz(BaseCommitizen):
}
changelog_pattern = defaults.bump_pattern

def questions(self) -> Questions:
def questions(self, language: str) -> Questions:
questions: Questions = [
{
"type": "list",
"name": "prefix",
"message": "Select the type of change you are committing",
"message": translate_text_from_eng(
"Select the type of change you are committing", language, "prefix"
),
"choices": [
{
"value": "fix",
"name": "fix: A bug fix. Correlates with PATCH in SemVer",
"name": "fix: "
+ translate_text_from_eng(
"A bug fix. Correlates with PATCH in SemVer",
language,
"fix",
),
"key": "x",
},
{
"value": "feat",
"name": "feat: A new feature. Correlates with MINOR in SemVer",
"name": "feat: "
+ translate_text_from_eng(
"A new feature. Correlates with MINOR in SemVer",
language,
"feat",
),
"key": "f",
},
{
"value": "docs",
"name": "docs: Documentation only changes",
"name": "docs: "
+ translate_text_from_eng(
"Documentation only changes", language, "docs"
),
"key": "d",
},
{
"value": "style",
"name": (
"style: Changes that do not affect the "
"meaning of the code (white-space, formatting,"
" missing semi-colons, etc)"
"name": "style: "
+ translate_text_from_eng(
"""Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)""",
language,
"style",
),
"key": "s",
},
{
"value": "refactor",
"name": (
"refactor: A code change that neither fixes "
"a bug nor adds a feature"
"name": "refactor: "
+ translate_text_from_eng(
"""A code change that neither fixes a bug nor adds a feature""",
language,
"refactor",
),
"key": "r",
},
{
"value": "perf",
"name": "perf: A code change that improves performance",
"name": "perf: "
+ translate_text_from_eng(
"A code change that improves performance", language, "perf"
),
"key": "p",
},
{
"value": "test",
"name": (
"test: Adding missing or correcting " "existing tests"
"name": "test: "
+ translate_text_from_eng(
"Adding missing or correcting existing tests",
language,
"test",
),
"key": "t",
},
{
"value": "build",
"name": (
"build: Changes that affect the build system or "
"external dependencies (example scopes: pip, docker, npm)"
"name": "build: "
+ translate_text_from_eng(
"""Changes that affect the build system or external dependencies (example scopes: pip, docker, npm)""",
language,
"build",
),
"key": "b",
},
{
"value": "ci",
"name": (
"ci: Changes to CI configuration files and "
"scripts (example scopes: GitLabCI)"
"name": "ci: "
+ translate_text_from_eng(
"""Changes to CI configuration files and scripts (example scopes: GitLabCI)""",
language,
"ci",
),
"key": "c",
},
Expand All @@ -112,18 +143,20 @@ def questions(self) -> Questions:
{
"type": "input",
"name": "scope",
"message": (
"What is the scope of this change? (class or file name): (press [enter] to skip)\n"
"message": translate_text_from_eng(
"What is the scope of this change? (class or file name): (press [enter] to skip)\n",
language,
"scope",
),
"filter": parse_scope,
},
{
"type": "input",
"name": "subject",
"filter": parse_subject,
"message": (
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
),
"message": translate_text_from_eng(
"Write a short and imperative summary of the code changes: (lower case and no period)\n",
language, "subject"),
},
{
"type": "input",
Expand Down
45 changes: 45 additions & 0 deletions commitizen/cz/conventional_commits/translation_multilanguage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from translate import Translator

FILENAME = "commitizen/cz/conventional_commits/.cache_multilanguage.txt"
MULTILANGUAGE = {}


def load_multilanguage():
global MULTILANGUAGE
MULTILANGUAGE = {}
with open(FILENAME) as file:
for line in file:
if not line.strip():
continue
key, value = line.strip().split("=", 1)
MULTILANGUAGE[key] = value

def generate_key(original_key, to_lang):
return f"{original_key}_{to_lang}"

def save_multilanguage(key, value):
if "\n" in value:
value = value.replace("\n", "")
with open(FILENAME, "a") as file:
file.write(f"{key}={value}\n")

def translate_text(text, from_lang, to_lang):
translator = Translator(from_lang=from_lang, to_lang=to_lang)
translation = translator.translate(text)
return translation

def translate_text_from_eng(text, to_lang, key):
global MULTILANGUAGE
key_generated = generate_key(key, to_lang)
if not MULTILANGUAGE:
load_multilanguage()
if key_generated in MULTILANGUAGE:
return MULTILANGUAGE[key_generated]
else:
if to_lang == "en":
MULTILANGUAGE[key_generated] = text
save_multilanguage(key_generated, MULTILANGUAGE[key_generated])
return text
MULTILANGUAGE[key_generated] = translate_text(text, "en", to_lang)
save_multilanguage(key_generated, MULTILANGUAGE[key_generated])
return MULTILANGUAGE[key_generated]
Loading