Skip to content

Commit

Permalink
Merge pull request #61 from ErikvdVen/master
Browse files Browse the repository at this point in the history
Add possibility to use custom .mo files, locally
  • Loading branch information
Salamek authored Nov 27, 2022
2 parents 2fbbff8 + 381bd7c commit 4077b5f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:

jobs:
build:
runs-on: ubuntu-latest
# see https://github.com/actions/setup-python/issues/162#issuecomment-1325307787
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ["2.7", "3.6", "3.7", "3.8", "3.9", "3.10", "3.11.0-alpha - 3.11"]
Expand All @@ -20,6 +21,6 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pep8 flake8 pep8-naming
pip install mock pep8 flake8 pep8-naming
- name: Test
run: python setup.py test
2 changes: 1 addition & 1 deletion cron_descriptor/ExpressionDescriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, expression, options=None, **kwargs):
raise WrongArgumentException("Unknown {} configuration argument".format(kwarg))

# Initializes localization
self.get_text = GetText(options.locale_code)
self.get_text = GetText(options.locale_code, options.locale_location)

def _(self, message):
return self.get_text.trans.gettext(message)
Expand Down
11 changes: 7 additions & 4 deletions cron_descriptor/GetText.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class GetText(object):
Handles language translations
"""

def __init__(self, locale_code):
def __init__(self, locale_code, locale_location=None):
"""
Initialize GetText
:param locale_code selected locale
"""
try:
self.trans = self.load_locale(locale_code)
self.trans = self.load_locale(locale_code, locale_location)
except IOError:
logger.debug('Failed to find locale {}'.format(locale_code))
logger.debug('Attempting to load en_US as fallback')
Expand All @@ -55,8 +55,11 @@ def __init__(self, locale_code):
# support for _("") or _("")
self.trans.add_fallback(FallBackNull())

def load_locale(self, locale_code):
filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale', '{}.mo'.format(locale_code))
def load_locale(self, locale_code, locale_location=None):
if locale_location is None:
filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale', '{}.mo'.format(locale_code))
else:
filename = os.path.join(locale_location, '{}.mo'.format(locale_code))
with open(filename, "rb") as f:
trans = gettext.GNUTranslations(f)
logger.debug('{} Loaded'.format(filename))
Expand Down
1 change: 1 addition & 0 deletions cron_descriptor/Options.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self):
self.verbose = False
self.day_of_week_start_index_zero = True
self.use_24hour_time_format = False
self.locale_location = None

code, encoding = locale.getlocale()
self.locale_code = code
Expand Down
19 changes: 19 additions & 0 deletions tests/TestLocale.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

import tests.TestCase as TestCase
from cron_descriptor import Options, ExpressionDescriptor
from mock import patch
import tempfile, shutil, os
import logging


class TestLocale(TestCase.TestCase):
Expand All @@ -33,3 +36,19 @@ def test_locale_de(self):
self.assertEqual(
"Jede Minute",
ExpressionDescriptor("* * * * *", options).get_description())

def test_locale_de_custom_location(self):
logger = logging.getLogger('cron_descriptor.GetText')
with patch.object(logger, "debug") as mock_logger:
# Copy existing .mo file to temp directory:
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, 'de_DE.mo')
shutil.copyfile(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../cron_descriptor/locale/', 'de_DE.mo'), temp_path)

options = Options()
options.locale_location = temp_dir
options.locale_code = 'de_DE'
options.use_24hour_time_format = True

self.assertEqual("Jede Minute", ExpressionDescriptor("* * * * *", options).get_description())
mock_logger.assert_called_once_with("{temp_path} Loaded".format(**{"temp_path":temp_path}))
3 changes: 2 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from tests.TestFormats import TestFormats
from tests.TestApi import TestApi
from tests.TestImport import TestImport
from tests.TestLocale import TestLocale

__all__ = ['TestCasing', 'TestExceptions', 'TestFormats', 'TestApi', 'TestImport']
__all__ = ['TestCasing', 'TestExceptions', 'TestFormats', 'TestApi', 'TestImport', 'TestLocale']
__author__ = "Adam Schubert <[email protected]>"
__date__ = "$2016-01-17 14:51:02$"

0 comments on commit 4077b5f

Please sign in to comment.