Exemple #1
0
 def wrapper(*args, **kwargs):
     try:
         alphabet = ROMANIZATION_ALPHABETS[locale]
     except KeyError:
         raise UnsupportedLocale(
             'Locale {0} is not supported yet.'.format(locale), )
     result = func(*args, **kwargs)
     txt = ''.join([alphabet[i] for i in result if i in alphabet])
     return txt
Exemple #2
0
 def wrapper(*args, **kwargs):
     try:
         # String can contain ascii symbols, digits and
         # punctuation symbols.
         alphabet = {s: s for s in letters + digits + punctuation}
         alphabet.update(data.ROMANIZATION_DICT[locale])
         # Add common cyrillic letters
         alphabet.update(data.COMMON_LETTERS)
     except KeyError:
         raise UnsupportedLocale(locale)
     result = func(*args, **kwargs)
     txt = ''.join([alphabet[i] for i in result if i in alphabet])
     return txt
Exemple #3
0
def locale_info(locale):
    """Return name (in english) or local name of the locale

    :param locale: Locale abbreviation.
    :type locale: str
    :returns: Locale name.
    """
    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale('Locale %s is not supported' % locale)

    return SUPPORTED_LOCALES[locale]['name']
Exemple #4
0
def locale_info(locale: str) -> str:
    """Check information about locale.

    :param locale: Locale abbreviation.
    :return: Locale name.
    :raises UnsupportedLocale: if locale is not supported.
    """
    locale = locale.lower()
    supported = config.SUPPORTED_LOCALES

    if locale not in supported:
        raise UnsupportedLocale(locale)

    return supported[locale]['name']
Exemple #5
0
def locale_info(locale: str) -> str:
    """Return name (in english) or local name of the locale

    :param str locale: Locale abbreviation.
    :return: Locale name.
    :raises UnsupportedLocale: if locale is not supported.
    """
    locale = locale.lower()
    supported = config.SUPPORTED_LOCALES

    if locale not in supported:
        raise UnsupportedLocale('Locale {} is not supported'.format(locale))

    return supported[locale]['name']
Exemple #6
0
 def wrapper(*args, **kwargs):
     try:
         alphabet = ROMANIZATION_DICT[locale]
         # Add common cyrillic common letters
         alphabet.update(COMMON_LETTERS)
         # String can contain ascii symbols, digits and
         # punctuation symbols.
         alphabet.update({s: s for s in letters + digits + punctuation})
     except KeyError:
         raise UnsupportedLocale(
             'Locale {0} is not supported yet.'.format(locale), )
     result = func(*args, **kwargs)
     txt = ''.join([alphabet[i] for i in result if i in alphabet])
     return txt
Exemple #7
0
def setup_locale(locale: Optional[str] = None) -> str:
    """Set up locale after pre-check.

    :param str locale: Locale
    :return: Locale in lowercase.
    :raises UnsupportedLocale: if locales is not supported.
    """
    if not locale:
        return config.DEFAULT_LOCALE

    locale = locale.lower()
    if locale not in config.SUPPORTED_LOCALES:
        raise UnsupportedLocale(locale)

    return locale
Exemple #8
0
    def _setup_locale(self, locale: str = locales.DEFAULT_LOCALE) -> None:
        """Set up locale after pre-check.

        :param str locale: Locale
        :raises UnsupportedLocale: When locale not supported.
        :return: Nothing.
        """
        if not locale:
            locale = locales.DEFAULT_LOCALE

        locale = locale.lower()
        if locale not in locales.SUPPORTED_LOCALES:
            raise UnsupportedLocale(locale)

        self.locale = locale
Exemple #9
0
        def wrapper(*args, **kwargs):
            try:
                # Cyrillic string can contain ascii
                # symbols, digits and punctuation.
                alphabet = {s: s for s in ascii_letters + digits + punctuation}
                alphabet.update({
                    **data.ROMANIZATION_DICT[locale],
                    **data.COMMON_LETTERS,
                })
            except KeyError:
                raise UnsupportedLocale(locale)

            result = func(*args, **kwargs)
            txt = ''.join([alphabet[i] for i in result if i in alphabet])
            return txt
Exemple #10
0
def pull(file: str, locale: str = 'en') -> JSON:
    """Pull the content from the JSON and memorize one.

    Opens JSON file ``file`` in the folder ``data/locale``
    and get content from the file and memorize ones using lru_cache.

    :param file: The name of file.
    :param locale: Locale.
    :return: The content of the file.
    :rtype: dict
    :raises UnsupportedLocale: if locale is not supported.

    :Example:

        >>> from mimesis.utils import pull
        >>> en = pull(file='datetime.json', locale='en')
        >>> isinstance(en, dict)
        True
        >>> en['day']['abbr'][0]
        'Mon.'
    """
    def get_data(locale_name: str) -> JSON:
        """Pull JSON data from file.

        :param locale_name: Locale name.
        :return: Content of JSON file as dict.
        """
        file_path = path.join(DATA_DIR, locale_name, file)
        # Needs explicit encoding for Windows
        with open(file_path, 'r', encoding='utf8') as f:
            return json.load(f)

    locale = locale.lower()

    if locale not in config.SUPPORTED_LOCALES:
        raise UnsupportedLocale(locale)

    master_locale = locale.split('-')[0]
    data = get_data(master_locale)

    # Handle sub-locales
    if '-' in locale:
        data = update_dict(data, get_data(locale))

    return data
Exemple #11
0
def pull(file, locale='en'):
    """Open json file file and get content from file and memorize result using
     lru_cache.

    .. note:: pull - is internal function, please do not use this function
    outside the module 'mimesis'.

    :param file: The name of file.
    :param locale: Locale.
    :returns: The content of the file.

    :Example:

        >>> from mimesis.utils import pull
        >>> en = pull(file='datetime.json', locale='en')
        >>> isinstance(en, dict)
        True
        >>> en['day']['abbr'][0]
        'Mon.'
    """

    def get_data(locale_name):
        """Pull JSON data from file.

        :param locale_name: Name of locale to pull.
        :return: Dict of data from file
        """
        file_path = path.join(PATH + '/' + locale_name, file)
        # Needs explicit encoding for Windows
        with open(file_path, 'r', encoding='utf8') as f:
            return json.load(f)

    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale('Locale %s is not supported' % locale)

    master_locale = locale.split('-')[0]
    data = get_data(master_locale)

    # Handle sub-locales
    if '-' in locale:
        data = update_dict(data, get_data(locale))

    return data
Exemple #12
0
def locale_info(locale: str) -> str:
    """Return name (in english) or local name of the locale

    :param locale: Locale abbreviation.
    :type locale: str
    :returns: Locale name.
    :rtype: str
    :Example:

    >>> from mimesis.utils import locale_info
    >>> locale_info('sv')
    'Swedish'
    """
    locale = locale.lower()

    if locale not in SUPPORTED_LOCALES:
        raise UnsupportedLocale('Locale %s is not supported' % locale)

    return SUPPORTED_LOCALES[locale]['name']