diff --git a/lingua_franca/format.py b/lingua_franca/format.py index 4e93a300..51c3e99a 100755 --- a/lingua_franca/format.py +++ b/lingua_franca/format.py @@ -18,19 +18,16 @@ import os import re from collections import namedtuple -from warnings import warn from os.path import join - +from warnings import warn from lingua_franca.bracket_expansion import SentenceTreeParser from lingua_franca.internal import localized_function, \ populate_localized_function_dict, get_active_langs, \ - get_full_lang_code, get_default_lang, get_default_loc, \ - is_supported_full_lang, _raise_unsupported_language, \ - UnsupportedLanguageError, NoneLangWarning, InvalidLangWarning, \ + get_full_lang_code, get_default_loc, \ + is_supported_full_lang, UnsupportedLanguageError, NoneLangWarning, InvalidLangWarning, \ FunctionNotLocalizedError - _REGISTERED_FUNCTIONS = ("nice_number", "nice_time", "pronounce_number", @@ -42,9 +39,42 @@ populate_localized_function_dict("format", langs=get_active_langs()) -def _translate_word(name, lang=''): +def _translate_word(name, amount=1, lang=''): """ Helper to get word translations + Args: + name (str): Word name. Returned as the default value if not translated + amount (int): Amount of that word. Used for pluralization + lang (str): Language code, e.g. "en-us" + + Returns: + str: translated version of resource name + """ + from lingua_franca.internal import resolve_resource_file + if not lang: + if lang is None: + warn(NoneLangWarning) + lang = get_default_loc() + + lang_code = lang if is_supported_full_lang(lang) else get_full_lang_code(lang) + filename = resolve_resource_file(join("text", lang_code, "translations.json")) + + if filename: + try: + with open(filename, 'r', encoding='utf8') as file: + translations = json.load(file) + return translations[name][get_plural_category(amount, lang=lang)] + except Exception: + pass + return _translate_word_legacy(name + ('s' if amount > 1 else ''), lang) # fallback to legacy translation + + +def _translate_word_legacy(name, lang=''): + """ Legacy helper to get word translations. + + Do not use this function directly. Remove it once + all languages are migrated to the new format. + Args: name (str): Word name. Returned as the default value if not translated lang (str): Language code, e.g. "en-us" @@ -418,35 +448,23 @@ def nice_duration(duration, lang='', speech=True): out = "" if days > 0: out += pronounce_number(days, lang) + " " - if days == 1: - out += _translate_word("day", lang) - else: - out += _translate_word("days", lang) + out += _translate_word("day", amount=days, lang=lang) out += " " if hours > 0: if out: out += " " out += pronounce_number(hours, lang) + " " - if hours == 1: - out += _translate_word("hour", lang) - else: - out += _translate_word("hours", lang) + out += _translate_word("hour", amount=hours, lang=lang) if minutes > 0: if out: out += " " out += pronounce_number(minutes, lang) + " " - if minutes == 1: - out += _translate_word("minute", lang) - else: - out += _translate_word("minutes", lang) + out += _translate_word("minute", amount=minutes, lang=lang) if seconds > 0: if out: out += " " out += pronounce_number(seconds, lang) + " " - if seconds == 1: - out += _translate_word("second", lang) - else: - out += _translate_word("seconds", lang) + out += _translate_word("second", amount=seconds, lang=lang) else: # M:SS, MM:SS, H:MM:SS, Dd H:MM:SS format out = "" @@ -489,7 +507,7 @@ def join_list(items, connector, sep=None, lang=''): else: sep += " " return (sep.join(str(item) for item in items[:-1]) + - " " + _translate_word(connector, lang) + + " " + _translate_word(connector, lang=lang) + " " + items[-1]) diff --git a/lingua_franca/res/text/en-us/and.word b/lingua_franca/res/text/en-us/and.word deleted file mode 100644 index c51107c2..00000000 --- a/lingua_franca/res/text/en-us/and.word +++ /dev/null @@ -1 +0,0 @@ -and \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/day.word b/lingua_franca/res/text/en-us/day.word deleted file mode 100644 index 0c303a41..00000000 --- a/lingua_franca/res/text/en-us/day.word +++ /dev/null @@ -1 +0,0 @@ -day \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/days.word b/lingua_franca/res/text/en-us/days.word deleted file mode 100644 index 5eb8de30..00000000 --- a/lingua_franca/res/text/en-us/days.word +++ /dev/null @@ -1 +0,0 @@ -days \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/hour.word b/lingua_franca/res/text/en-us/hour.word deleted file mode 100644 index a13960e9..00000000 --- a/lingua_franca/res/text/en-us/hour.word +++ /dev/null @@ -1 +0,0 @@ -hour \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/hours.word b/lingua_franca/res/text/en-us/hours.word deleted file mode 100644 index 62c6decf..00000000 --- a/lingua_franca/res/text/en-us/hours.word +++ /dev/null @@ -1 +0,0 @@ -hours \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/minute.word b/lingua_franca/res/text/en-us/minute.word deleted file mode 100644 index 50bc2f27..00000000 --- a/lingua_franca/res/text/en-us/minute.word +++ /dev/null @@ -1 +0,0 @@ -minute \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/minutes.word b/lingua_franca/res/text/en-us/minutes.word deleted file mode 100644 index cde6523a..00000000 --- a/lingua_franca/res/text/en-us/minutes.word +++ /dev/null @@ -1 +0,0 @@ -minutes \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/or.word b/lingua_franca/res/text/en-us/or.word deleted file mode 100644 index c4fced5f..00000000 --- a/lingua_franca/res/text/en-us/or.word +++ /dev/null @@ -1 +0,0 @@ -or \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/second.word b/lingua_franca/res/text/en-us/second.word deleted file mode 100644 index 2147e418..00000000 --- a/lingua_franca/res/text/en-us/second.word +++ /dev/null @@ -1 +0,0 @@ -second \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/seconds.word b/lingua_franca/res/text/en-us/seconds.word deleted file mode 100644 index 729866f9..00000000 --- a/lingua_franca/res/text/en-us/seconds.word +++ /dev/null @@ -1 +0,0 @@ -seconds \ No newline at end of file diff --git a/lingua_franca/res/text/en-us/translations.json b/lingua_franca/res/text/en-us/translations.json new file mode 100644 index 00000000..f6a6bb33 --- /dev/null +++ b/lingua_franca/res/text/en-us/translations.json @@ -0,0 +1,24 @@ +{ + "day": { + "one": "day", + "other": "days" + }, + "hour": { + "one": "hour", + "other": "hours" + }, + "minute": { + "one": "minute", + "other": "minutes" + }, + "second": { + "one": "second", + "other": "seconds" + }, + "and": { + "one": "and" + }, + "or": { + "one": "or" + } +} diff --git a/lingua_franca/res/text/sl-si/and.word b/lingua_franca/res/text/sl-si/and.word deleted file mode 100644 index f087d891..00000000 --- a/lingua_franca/res/text/sl-si/and.word +++ /dev/null @@ -1 +0,0 @@ -in \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/day.word b/lingua_franca/res/text/sl-si/day.word deleted file mode 100644 index dc816d94..00000000 --- a/lingua_franca/res/text/sl-si/day.word +++ /dev/null @@ -1 +0,0 @@ -dan \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/days.word b/lingua_franca/res/text/sl-si/days.word deleted file mode 100644 index 7b0e614a..00000000 --- a/lingua_franca/res/text/sl-si/days.word +++ /dev/null @@ -1 +0,0 @@ -dni \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/hour.word b/lingua_franca/res/text/sl-si/hour.word deleted file mode 100644 index fa6c4e16..00000000 --- a/lingua_franca/res/text/sl-si/hour.word +++ /dev/null @@ -1 +0,0 @@ -ura \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/hours.word b/lingua_franca/res/text/sl-si/hours.word deleted file mode 100644 index 873003f7..00000000 --- a/lingua_franca/res/text/sl-si/hours.word +++ /dev/null @@ -1 +0,0 @@ -ur \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/minute.word b/lingua_franca/res/text/sl-si/minute.word deleted file mode 100644 index 02810dfe..00000000 --- a/lingua_franca/res/text/sl-si/minute.word +++ /dev/null @@ -1 +0,0 @@ -minuta \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/minutes.word b/lingua_franca/res/text/sl-si/minutes.word deleted file mode 100644 index 4b98366b..00000000 --- a/lingua_franca/res/text/sl-si/minutes.word +++ /dev/null @@ -1 +0,0 @@ -minut \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/or.word b/lingua_franca/res/text/sl-si/or.word deleted file mode 100644 index 784f9000..00000000 --- a/lingua_franca/res/text/sl-si/or.word +++ /dev/null @@ -1 +0,0 @@ -ali \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/second.word b/lingua_franca/res/text/sl-si/second.word deleted file mode 100644 index ef210e25..00000000 --- a/lingua_franca/res/text/sl-si/second.word +++ /dev/null @@ -1 +0,0 @@ -sekunda \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/seconds.word b/lingua_franca/res/text/sl-si/seconds.word deleted file mode 100644 index 300f8e50..00000000 --- a/lingua_franca/res/text/sl-si/seconds.word +++ /dev/null @@ -1 +0,0 @@ -sekund \ No newline at end of file diff --git a/lingua_franca/res/text/sl-si/translations.json b/lingua_franca/res/text/sl-si/translations.json new file mode 100644 index 00000000..a8d09bb5 --- /dev/null +++ b/lingua_franca/res/text/sl-si/translations.json @@ -0,0 +1,32 @@ +{ + "day": { + "one": "dan", + "two": "dneva", + "few": "dnevi", + "other": "dni" + }, + "hour": { + "one": "ura", + "two": "uri", + "few": "ure", + "other": "ur" + }, + "minute": { + "one": "minuta", + "two": "minuti", + "few": "minute", + "other": "minut" + }, + "second": { + "one": "sekunda", + "two": "sekundi", + "few": "sekunde", + "other": "sekund" + }, + "and": { + "one": "in" + }, + "or": { + "one": "ali" + } +} diff --git a/test/test_format_sl.py b/test/test_format_sl.py index f3182785..e044d2d8 100644 --- a/test/test_format_sl.py +++ b/test/test_format_sl.py @@ -530,13 +530,10 @@ def test_nice_year(self): # print(nice_year(dt, lang=lang)) def test_nice_duration(self): - # TODO implement better plural support for nice_duration - # Correct results are in comments - self.assertEqual(nice_duration(1), "ena sekunda") - self.assertEqual(nice_duration(2), "dve sekund") # dve sekundi - self.assertEqual(nice_duration(3), "tri sekund") # tri sekunde - self.assertEqual(nice_duration(4), "štiri sekund") # štiri sekunde + self.assertEqual(nice_duration(2), "dve sekundi") + self.assertEqual(nice_duration(3), "tri sekunde") + self.assertEqual(nice_duration(4), "štiri sekunde") self.assertEqual(nice_duration(5), "pet sekund") self.assertEqual(nice_duration(6), "šest sekund")