Skip to content

Commit

Permalink
Formats: Fixed customizing language code style
Browse files Browse the repository at this point in the history
It was used for validation only, but not for creating of the files.

Also add type hints to related code.

Fixes #4100
  • Loading branch information
nijel committed Jun 30, 2020
1 parent 14b6fa7 commit a36ba0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 11 additions & 9 deletions weblate/formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,32 +359,32 @@ def is_valid_base_for_new(cls, base, monolingual):
raise NotImplementedError()

@classmethod
def get_language_code(cls, code, language_format=None):
def get_language_code(cls, code: str, language_format: Optional[str] = None):
"""Do any possible formatting needed for language code."""
if not language_format:
language_format = cls.language_format
return getattr(cls, "get_language_{}".format(language_format))(code)

@staticmethod
def get_language_posix(code):
def get_language_posix(code: str):
return code

@staticmethod
def get_language_bcp(code):
def get_language_bcp(code: str):
return code.replace("_", "-")

@staticmethod
def get_language_posix_long(code):
def get_language_posix_long(code: str):
if code in EXPAND_LANGS:
return EXPAND_LANGS[code]
return code

@classmethod
def get_language_bcp_long(cls, code):
def get_language_bcp_long(cls, code: str):
return cls.get_language_posix_long(code).replace("_", "-")

@staticmethod
def get_language_android(code):
def get_language_android(code: str):
# Android doesn't use Hans/Hant, but rather TW/CN variants
if code == "zh_Hans":
return "zh-rCN"
Expand All @@ -396,7 +396,7 @@ def get_language_android(code):
return sanitized.replace("_", "-r")

@classmethod
def get_language_java(cls, code):
def get_language_java(cls, code: str):
# Java doesn't use Hans/Hant, but rather TW/CN variants
if code == "zh_Hans":
return "zh-CN"
Expand All @@ -409,12 +409,14 @@ def get_language_java(cls, code):
return cls.get_language_bcp(code)

@classmethod
def get_language_filename(cls, mask, code):
def get_language_filename(
cls, mask: str, code: str, language_format: Optional[str] = None
):
"""Return full filename of a language file.
Calculated forfor given path, filemask and language code.
"""
return mask.replace("*", cls.get_language_code(code))
return mask.replace("*", cls.get_language_code(code, language_format))

@classmethod
def add_language(cls, filename, language, base):
Expand Down
4 changes: 3 additions & 1 deletion weblate/trans/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,9 @@ def add_new_language(self, language, request, send_signal=True):

base_filename = self.get_new_base_filename()

filename = file_format.get_language_filename(self.filemask, code)
filename = file_format.get_language_filename(
self.filemask, code, self.language_code_style
)
fullname = os.path.join(self.full_path, filename)

# Ignore request if file exists (possibly race condition as
Expand Down

0 comments on commit a36ba0c

Please sign in to comment.