Skip to content

Commit

Permalink
feat: add exclude_paths option to translations filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydw committed Mar 5, 2022
1 parent 29e1bc0 commit ce0ae69
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
8 changes: 8 additions & 0 deletions grow/commands/shared.py
Expand Up @@ -24,6 +24,14 @@ def _decorator(func):
return _decorator


def exclude_path_option(func):
"""Option for excluding a path for extraction."""
return click.option(
'--exclude-path',
type=str, multiple=True,
help='Which paths to exclude from the generated messages file.')(func)


def force_untranslated_option(config):
"""Option for forcing untranslated string deployment."""
shared_default = CFG.get('force-untranslated', False)
Expand Down
5 changes: 3 additions & 2 deletions grow/commands/subcommands/translations_filter.py
Expand Up @@ -31,7 +31,8 @@
' The path must be relative to the pod\'s root. This option'
' is only applicable when using --localized.'))
@shared.path_option
def translations_filter(pod_path, locale, o, include_obsolete, localized, path,
@shared.exclude_path_option
def translations_filter(pod_path, locale, o, include_obsolete, localized, path, exclude_path,
include_header, out_dir, f):
"""Filters untranslated messages from catalogs into new catalogs."""
root = os.path.abspath(os.path.join(os.getcwd(), pod_path))
Expand All @@ -47,6 +48,6 @@ def translations_filter(pod_path, locale, o, include_obsolete, localized, path,
'specified directory.'.format(out_dir))
catalogs.filter(out_path=o, out_dir=out_dir,
include_obsolete=include_obsolete,
localized=localized, paths=path,
localized=localized, paths=path, exclude_paths=exclude_path,
include_header=include_header, locales=locale)
return pod
6 changes: 4 additions & 2 deletions grow/translations/catalog_holder.py
Expand Up @@ -568,17 +568,19 @@ def get_gettext_translations(self, locale):

def filter(self, out_path=None, out_dir=None,
include_obsolete=True, localized=False,
paths=None, include_header=None, locales=None):
paths=None, exclude_paths=None, include_header=None, locales=None):
if localized and out_dir is None:
raise UsageError('Must specify --out_dir when using --localized in '
'order to generate localized catalogs.')
if not localized and out_path is None:
raise UsageError('Must specify -o when not using --localized.')
if not locales:
raise UsageError('Must specify locales to filter.')
filtered_catalogs = []
messages_to_locales = {}
for locale in locales:
locale_catalog = self.get(locale)
missing_messages = locale_catalog.list_untranslated(paths=paths)
missing_messages = locale_catalog.list_untranslated(paths=paths, exclude_paths=exclude_paths)
num_missing = len(missing_messages)
num_total = len(locale_catalog)
for message in missing_messages:
Expand Down
9 changes: 9 additions & 0 deletions grow/translations/catalog_holder_test.py
Expand Up @@ -219,6 +219,7 @@ def test_filter(self):
de_catalog = catalogs[0]
self.assertEqual(3, len(de_catalog))


paths = [
'/content/pages/yaml_test.html',
]
Expand All @@ -230,6 +231,14 @@ def test_filter(self):
de_catalog = catalogs[0]
self.assertEqual(1, len(de_catalog))

catalogs = self.pod.catalogs.filter(
out_path='./untranslated.po',
locales=locales,
exclude_paths=paths,
localized=False)
de_catalog = catalogs[0]
self.assertEqual(2, len(de_catalog))

def test_filter_localized(self):
locales = ['de', 'fr']
catalogs = self.pod.catalogs.filter(
Expand Down
4 changes: 3 additions & 1 deletion grow/translations/catalogs.py
Expand Up @@ -227,12 +227,14 @@ def _message_in_paths(cls, message, paths):
return True
return False

def list_untranslated(self, paths=None):
def list_untranslated(self, paths=None, exclude_paths=None):
"""Returns untranslated messages, including fuzzy translations."""
untranslated = []
for message in self:
if paths and not Catalog._message_in_paths(message, paths):
continue
if exclude_paths and Catalog._message_in_paths(message, exclude_paths):
continue
# Ensure fuzzy messages have a message.id otherwise we'd include
# the header as part of the results, which we don't want.
if not message.string or (message.fuzzy and message.id):
Expand Down

0 comments on commit ce0ae69

Please sign in to comment.