2021-05-10 13:26:30 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Safe Eyes is a utility to remind you to take break frequently
|
|
|
|
# to protect your eyes from eye strain.
|
|
|
|
|
|
|
|
# Copyright (C) 2021 Gobinath
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2024-08-04 18:40:04 +02:00
|
|
|
from collections import defaultdict
|
2021-05-10 13:26:30 +02:00
|
|
|
import os
|
|
|
|
import polib
|
2024-08-04 18:40:04 +02:00
|
|
|
import re
|
2023-08-03 17:16:06 +02:00
|
|
|
import sys
|
2021-05-10 13:26:30 +02:00
|
|
|
|
2024-08-04 19:54:25 +02:00
|
|
|
def validate_placeholders(message: str) -> bool:
|
|
|
|
pos = 0
|
|
|
|
|
|
|
|
success = True
|
|
|
|
|
|
|
|
count_placeholders = 0
|
|
|
|
count_unnamed = 0
|
|
|
|
|
|
|
|
while True:
|
|
|
|
index = message.find("%", pos)
|
|
|
|
if index == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
pos = index + 1
|
|
|
|
|
|
|
|
nextchar = message[pos : pos + 1]
|
|
|
|
|
|
|
|
name = None
|
|
|
|
|
|
|
|
if nextchar == "(":
|
|
|
|
index = message.find(")", pos)
|
|
|
|
if index == -1:
|
|
|
|
success = False
|
|
|
|
print(f"Unclosed parenthetical in '{message}'")
|
|
|
|
break
|
|
|
|
name = message[pos + 1 : index]
|
|
|
|
|
|
|
|
pos = index + 1
|
|
|
|
|
|
|
|
nextchar = message[pos : pos + 1]
|
|
|
|
if nextchar not in ["%", "s", "d", "i", "f", "F"]:
|
|
|
|
success = False
|
|
|
|
print(f"Invalid format modifier in '{message}'")
|
|
|
|
break
|
|
|
|
|
|
|
|
if nextchar != "%":
|
|
|
|
count_placeholders += 1
|
|
|
|
if name is None:
|
|
|
|
count_unnamed += 1
|
|
|
|
|
|
|
|
pos += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
if count_unnamed > 1:
|
|
|
|
success = False
|
|
|
|
print(f"Multiple unnamed placeholders in '{message}'")
|
|
|
|
|
|
|
|
if count_unnamed > 0 and count_placeholders > count_unnamed:
|
|
|
|
success = False
|
|
|
|
print(f"Mixing named and unnamed placeholders in '{message}'")
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
2024-08-04 18:40:04 +02:00
|
|
|
def has_equal_placeholders(left: str, right: str) -> bool:
|
2024-08-04 19:54:25 +02:00
|
|
|
def _get_placeholders(message: str) -> tuple:
|
|
|
|
percents = re.finditer(r"%(?P<name>\(\w+\))?(?P<format>[a-z])", message)
|
|
|
|
|
|
|
|
unnamed = defaultdict(int)
|
|
|
|
named = set()
|
|
|
|
for percent in percents:
|
|
|
|
if percent.group("name"):
|
|
|
|
named.add(f"%({percent.group('name')}){percent.group('format')}")
|
|
|
|
else:
|
|
|
|
match = f"%{percent.group('format')}"
|
|
|
|
unnamed[match] += 1
|
|
|
|
return (unnamed, named)
|
2024-08-04 18:40:04 +02:00
|
|
|
|
2024-08-04 19:54:25 +02:00
|
|
|
(left_unnamed, left_named) = _get_placeholders(left)
|
|
|
|
(right_unnamed, right_named) = _get_placeholders(right)
|
2024-08-04 18:40:04 +02:00
|
|
|
|
|
|
|
# count unnamed cases (eg. %s, %d)
|
2024-08-04 19:54:25 +02:00
|
|
|
for match, count in left_unnamed.items():
|
|
|
|
if right_unnamed.get(match, 0) != count:
|
2024-08-04 18:40:04 +02:00
|
|
|
return False
|
|
|
|
|
2024-08-04 19:54:25 +02:00
|
|
|
# named cases are optional - but ensure that translation does not add new ones
|
|
|
|
if not right_named.issubset(left_named):
|
|
|
|
return False
|
2024-08-04 18:40:04 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2023-08-03 17:16:06 +02:00
|
|
|
def validate_po(locale: str, path: str) -> bool:
|
2024-08-04 18:40:04 +02:00
|
|
|
success = True
|
|
|
|
po = polib.pofile(path)
|
|
|
|
for entry in po:
|
2024-08-04 19:54:25 +02:00
|
|
|
if entry.msgstr:
|
|
|
|
if not validate_placeholders(entry.msgstr):
|
|
|
|
success = False
|
|
|
|
if not has_equal_placeholders(entry.msgid, entry.msgstr):
|
2024-08-04 18:40:04 +02:00
|
|
|
print("Number of variables mismatched in " + locale)
|
2024-08-04 19:54:25 +02:00
|
|
|
print(entry.msgid + " -> " + entry.msgstr)
|
2024-08-04 18:40:04 +02:00
|
|
|
print()
|
|
|
|
success = False
|
2024-08-04 19:54:25 +02:00
|
|
|
for plural in entry.msgstr_plural.values():
|
|
|
|
if plural:
|
|
|
|
if not validate_placeholders(plural):
|
|
|
|
success = False
|
|
|
|
if not has_equal_placeholders(entry.msgid, plural):
|
|
|
|
print("Number of variables mismatched in " + locale)
|
|
|
|
print(entry.msgid + " -> " + plural)
|
|
|
|
print()
|
|
|
|
success = False
|
2024-08-04 18:40:04 +02:00
|
|
|
return success
|
2021-05-10 13:26:30 +02:00
|
|
|
|
2023-08-03 17:16:06 +02:00
|
|
|
success = True
|
2021-05-10 13:26:30 +02:00
|
|
|
locales = os.listdir('safeeyes/config/locale')
|
2023-08-03 17:16:06 +02:00
|
|
|
for locale in sorted(locales):
|
2021-05-10 13:26:30 +02:00
|
|
|
path = os.path.join('safeeyes/config/locale', locale, "LC_MESSAGES/safeeyes.po")
|
|
|
|
if os.path.isfile(path):
|
2023-08-03 17:16:06 +02:00
|
|
|
print('Validating translation %s...' % path)
|
|
|
|
success = validate_po(locale, path) and success
|
2024-08-04 18:40:04 +02:00
|
|
|
|
2023-08-03 17:16:06 +02:00
|
|
|
sys.exit(0 if success else 1)
|