Skip to content

Commit

Permalink
Add Language.code and Language.name convenience propertys.
Browse files Browse the repository at this point in the history
Also make matching languages by name space insensitive and cleanup
related code.

Related to #4390.
  • Loading branch information
pekkaklarck committed Aug 12, 2022
1 parent 9613ddb commit 35b4881
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
32 changes: 22 additions & 10 deletions src/robot/conf/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import inspect
import os.path

from robot.utils import getdoc, is_string, Importer
from robot.utils import getdoc, is_string, Importer, normalize


class Languages:
Expand Down Expand Up @@ -48,7 +48,7 @@ def _get_languages(self, languages):
available = self._get_available_languages()
returned = []
for lang in languages:
normalized = lang.lower().replace('-', '')
normalized = normalize(lang, ignore='-')
if normalized in available:
returned.append(available[normalized])
else:
Expand All @@ -67,9 +67,9 @@ def _resolve_languages(self, languages):
def _get_available_languages(self):
available = {}
for lang in Language.__subclasses__():
available[lang.__name__.lower()] = lang
available[normalize(lang.__name__)] = lang
if lang.__doc__:
available[lang.__doc__.lower()] = lang
available[normalize(lang.__doc__)] = lang
return available

def _import_languages(self, lang):
Expand Down Expand Up @@ -126,18 +126,30 @@ class Language:
def from_name(cls, name):
"""Return langauge class based on given `name`.
Name is matched both against the class name (language short name)
and possible docstring (full language name). Matching is case-insensitive
and hyphen (`-`) is ignored to support, for example, `PT-BR`.
Name can either be a language name (e.g. 'Finnish' or 'Brazilian Portuguese')
or a language code (e.g. 'fi' or 'pt-BR'). Matching is case and space
insensitive and the hyphen is ignored when matching language codes.
Raises `ValueError` if no matching langauge is found.
"""
normalized = name.lower().replace('-', '')
normalized = normalize(name, ignore='-')
for subcls in cls.__subclasses__():
if normalized in (subcls.__name__.lower(), getdoc(subcls).lower()):
if normalized in (normalize(subcls.__name__),
normalize(getdoc(subcls))):
return subcls()
raise ValueError(f"No language with name '{name}' found.")

@property
def code(self):
name = type(self).__name__
if len(name) < 3:
return name.lower()
return f'{name[:2].lower()}-{name[2:].upper()}'

@property
def name(self):
return self.__doc__ or ''

@property
def settings(self):
return {
Expand Down Expand Up @@ -378,7 +390,7 @@ class De(Language):


class PtBr(Language):
"""Portuguese, Brazilian"""
"""Brazilian Portuguese"""
setting_headers = {'Configuração', 'Configurações'}
variable_headers = {'Variável', 'Variáveis'}
test_case_headers = {'Caso de Teste', 'Casos de Teste'}
Expand Down
29 changes: 23 additions & 6 deletions utest/api/test_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@

from robot.api import Language
from robot.conf.languages import Fi, PtBr
from robot.utils.asserts import assert_raises_with_msg
from robot.utils.asserts import assert_equal, assert_raises_with_msg


class TestLanguage(unittest.TestCase):

def test_one_part_code(self):
assert_equal(Fi().code, 'fi')

def test_two_part_code(self):
assert_equal(PtBr().code, 'pt-BR')

def test_name(self):
assert_equal(Fi().name, 'Finnish')
assert_equal(PtBr().name, 'Brazilian Portuguese')


class TestFromName(unittest.TestCase):

def test_class_name(self):
def test_code(self):
assert isinstance(Language.from_name('fi'), Fi)
assert isinstance(Language.from_name('FI'), Fi)

def test_docstring(self):
def test_two_part_code(self):
assert isinstance(Language.from_name('pt-BR'), PtBr)
assert isinstance(Language.from_name('PTBR'), PtBr)

def test_name(self):
assert isinstance(Language.from_name('finnish'), Fi)
assert isinstance(Language.from_name('Finnish'), Fi)

def test_hyphen_is_ignored(self):
assert isinstance(Language.from_name('pt-br'), PtBr)
assert isinstance(Language.from_name('PT-BR'), PtBr)
def test_multi_part_name(self):
assert isinstance(Language.from_name('Brazilian Portuguese'), PtBr)
assert isinstance(Language.from_name('brazilianportuguese'), PtBr)

def test_no_match(self):
assert_raises_with_msg(ValueError, "No language with name 'no match' found.",
Expand Down

0 comments on commit 35b4881

Please sign in to comment.