Skip to content

Commit

Permalink
Add new format for duration translations
Browse files Browse the repository at this point in the history
  • Loading branch information
filips123 committed Dec 17, 2020
1 parent 367d851 commit 101389b
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 50 deletions.
66 changes: 42 additions & 24 deletions lingua_franca/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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])


Expand Down
1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/and.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/day.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/days.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/hour.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/hours.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/minute.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/minutes.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/or.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/second.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/en-us/seconds.word

This file was deleted.

24 changes: 24 additions & 0 deletions lingua_franca/res/text/en-us/translations.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/and.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/day.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/days.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/hour.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/hours.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/minute.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/minutes.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/or.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/second.word

This file was deleted.

1 change: 0 additions & 1 deletion lingua_franca/res/text/sl-si/seconds.word

This file was deleted.

32 changes: 32 additions & 0 deletions lingua_franca/res/text/sl-si/translations.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 3 additions & 6 deletions test/test_format_sl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 101389b

Please sign in to comment.