Skip to content

Commit

Permalink
add support to manually set string references
Browse files Browse the repository at this point in the history
  • Loading branch information
willforde committed Sep 17, 2018
1 parent e1b1161 commit 7d91bef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
23 changes: 20 additions & 3 deletions script.module.codequick/lib/codequick/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import xbmc

# Package imports
from codequick.utils import ensure_unicode, ensure_native_str
from codequick.utils import ensure_unicode, ensure_native_str, unicode_type, string_map
from codequick.support import dispatcher, script_data, addon_data, logger_id

__all__ = ["Script", "Settings"]
Expand Down Expand Up @@ -245,16 +245,33 @@ def localize(string_id):
"""
Retruns a translated UI string from addon localization files.
:param int string_id: The numeric ID the localized string
.. note::
:data:`utils.string_map<codequick.utils.string_map>`
needs to be populated before you can pass in a string as the reference.
:param string_id: The numeric ID or gettext string ID of the localized string
:type string_id: str or int
:returns: Localized unicode string.
:rtype: str
:raises Keyword: if a gettext string ID was given but the string is not found in English :file:`strings.po`.
:example:
>>> Script.localize(30001)
"Toutes les vidéos"
>>> Script.localize("All Videos")
"Toutes les vidéos"
"""
if 30000 <= string_id <= 30999:
if isinstance(string_id, (str, unicode_type)):
try:
numeric_id = string_map[string_id]
except KeyError:
raise KeyError("no localization found for string id '%s'" % string_id)
else:
return addon_data.getLocalizedString(numeric_id)

elif 30000 <= string_id <= 30999:
return addon_data.getLocalizedString(string_id)
elif 32000 <= string_id <= 32999:
return script_data.getLocalizedString(string_id)
Expand Down
18 changes: 18 additions & 0 deletions script.module.codequick/lib/codequick/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
# Unicode Type object, unicode on python2 or str on python3
unicode_type = type(u"")

string_map = {}
"""
Dict of localized string references used in conjunction with
:class:`Script.localize<codequick.script.Script.localize>`.
Allowing you to use the string as the localized string reference.
.. note:: It is best if you set the string references at the top of your add-on python file.
:example:
>>> Script.localize(30001)
"Toutes les vidéos"
>>>
>>> # Add reference id for "All Videos" so you can use the string name instead.
>>> utils.string_map["All Videos": 30001]
>>> Script.localize("All Videos")
"Toutes les vidéos"
"""


def keyboard(heading, default="", hidden=False):
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ def test_localize(self):
def test_localize_allvideos(self):
self.assertEqual("All Videos", self.script.localize(32003))

def test_localize_allvideos_gettext(self):
org_map = script.string_map.copy()
try:
script.string_map["All Videos"] = 32003
self.assertEqual(self.script.localize("All Videos"), "All Videos")
finally:
script.string_map.clear()
script.string_map.update(org_map)

def test_localize_allvideos_gettext_non_exist(self):
with self.assertRaises(KeyError):
data = self.script.localize("All Videos")
print("###########")
print(data)

def test_localize_nodata(self):
self.assertEqual(self.script.localize(33077), "No data found!")

Expand Down

0 comments on commit 7d91bef

Please sign in to comment.