Skip to content

Commit

Permalink
Continued docstring for i18n module
Browse files Browse the repository at this point in the history
  • Loading branch information
sveetch committed May 13, 2018
1 parent d67d954 commit 6dadd16
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 58 deletions.
9 changes: 9 additions & 0 deletions docs/api/i18n.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. automodule:: optimus.i18n
:members:

.. automodule:: optimus.i18n.lang
:members:

.. automodule:: optimus.i18n.manager
:members:

1 change: 1 addition & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Modules
assets.rst
watchers.rst
start_project.rst
i18n.rst
5 changes: 5 additions & 0 deletions optimus/i18n/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
"""
Internationalization and localization
=====================================
"""
76 changes: 54 additions & 22 deletions optimus/i18n/lang.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
# -*- coding: utf-8 -*-
"""
Language object used to encapsulate some details on a language
Language base object
********************
Usage : ::
class LangFr(LangBase):
code = 'fr'
label = 'France'
Or : ::
lang = LangBase(lang="zh_CN", label="Chinese")
"""
from optimus.exceptions import InvalidLanguageIdentifier
from optimus.utils import UnicodeMixin


class LangBase(UnicodeMixin):
"""
Language base object to encapsulate the language label, code and other details
Language base object to encapsulate the language label, code and other
details.
Alternative and External code are not really used internally in optimus,
there are only for some template usage.
The instance will also supply a "language_name" and "region_name" class
attributes, which are the result of splitting the code on two parts.
"region_name" is ``None`` by default, as the region name is optional in
language identifier.
See http://www.i18nguy.com/unicode/language-identifiers.html for more
details on language identifiers.
Alternative and External code are not really used internally in optimus, there
are only for some template usage.
Usage : ::
The instance will also supply a "language_name" and "region_name" class attributes,
which are the result of splitting the code on two parts. "region_name" is ``None``
by default, as the region name is optional in language identifier.
class LangFr(LangBase):
code = 'fr'
label = 'France'
See http://www.i18nguy.com/unicode/language-identifiers.html for more details on
language identifiers.
Or : ::
lang = LangBase(code="zh_CN", label="Chinese")
Attributes:
label (string): Default language label if not given in kwargs.
code (string): Default language identifier if not given in kwargs.
alt_code (string): Alternative code, will be equal to "code" if not
set.
external_code (string): External code for some external apps, will be
equal to ``alt_code`` if not set.
Keyword Arguments:
code (string): Language identifier.
label (string): Language label like "Français" for ``fr``.
"""
label = None # Label to display
code = None # Internal code
alt_code = None # Alternative code, will be equal to "code" if not setted
external_code = None # External code for some external apps, will be equal to "alt_code" if not setted
label = None
code = None
alt_code = None
external_code = None

def __init__(self, code=None, label=None):
self.code = code or self.code
Expand All @@ -60,12 +76,28 @@ def __unicode__(self):
return self.label

def __repr__(self):
"""
Object representation
Returns:
string: Representation with name and code
"""
return "<{name} code:{code}>".format(
name=self.__class__.__name__,
code=self.code
)

def split_code(self, code):
"""
Split language identifier to language name and region name (if any).
Arguments:
code (string): Language identifier.
Returns:
tuple: A pair of language name and possibly region name, if code
does not contain any region it will be ``None``.
"""
items = code.split('_')
language_name = items[0]
region_name = None
Expand Down
Loading

0 comments on commit 6dadd16

Please sign in to comment.