No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mlstring.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import warnings
  4. ## Handle string with translations
  5. # @todo define a default language that will be used in case the wanted language is not available for this string (gettext-like behavior)
  6. class MlString(object):
  7. default_lang = '___'
  8. ## Instanciate a new string with translation
  9. #
  10. # @param translations dict|str : With key = lang and value the translation or a json string
  11. # @param default_value str : The default translation value
  12. def __init__(self, translations=None, default_value = None):
  13. if isinstance(translations, str):
  14. try:
  15. translations = json.loads(translations)
  16. except ValueError:
  17. if default_value is None:
  18. default_value = str(translations)
  19. translations = None
  20. if translations is None:
  21. translations = dict()
  22. if not isinstance(translations, dict):
  23. raise ValueError('Bad value given for translations argument on MlString instanciation')
  24. else:
  25. self.translations = translations
  26. self.translations[self.default_lang] = '' if default_value is None else default_value
  27. if default_value is None:
  28. warnings.warn('No default value when isntanciating an MlString')
  29. ## Return a translation
  30. # @param lang str: The lang
  31. # @return An empty string if the wanted lang don't exist
  32. # @warning Returns an empty string if the wanted translation didn't exists
  33. # @todo if the asked language is not available, use the default one, defined as a class property
  34. def get(self, lang = None):
  35. if lang is None or lang not in self.translations:
  36. lang = self.default_lang
  37. return self.translations[lang]
  38. ## Set a translation for this MlString
  39. # @param lang str: The language
  40. # @param text str: The translation
  41. # @todo check if the default language as a translation
  42. def set(self, lang, text):
  43. if not text:
  44. if lang in self.translations:
  45. del(self.translations[lang])
  46. else:
  47. self.translations[lang] = text
  48. ## @return Default translation
  49. def get_default(self):
  50. return self.get(self.default_lang)
  51. ## @brief Set default value
  52. def set_default(self, text):
  53. self.set(self.default_lang, text)
  54. ## String representation
  55. # @return A json dump of the MlString::translations dict
  56. def __str__(self):
  57. return self.get_default()
  58. ## @brief Serialize the MlString in Json
  59. def json_dumps(self):
  60. if self.translations:
  61. return json.dumps(self.translations, sort_keys=True)
  62. else:
  63. return ""
  64. ## @brief Equivalent to json_dumps
  65. def dumps(self): return self.json_dumps()
  66. ## Test if two MlString instance are equivalent
  67. # @param other MlString|str : Another MlString instance or a string (json formated)
  68. # @return True or False
  69. def __eq__(self, other):
  70. if isinstance(other, str):
  71. other = MlString(other)
  72. if not isinstance(other, MlString):
  73. return False
  74. if not set(lng for lng in self.translations) == set( lng for lng in other.translations):
  75. return False
  76. for lng in self.translations:
  77. if self.get(lng) != other.get(lng):
  78. return False
  79. return True
  80. ## @brief Allow [] access to translations
  81. def __getitem__(self, langname): return self.get(langname)
  82. ## @brief Allow [] set
  83. def __setitem__(self, langname, txt): return self.set(langname, txt)
  84. ## @brief Implements dict.keys() method
  85. def keys(self): return self.translations.keys()
  86. ## @brief Implements dict.values() method
  87. def values(self): return self.translations.values()
  88. ## @brief Implements dict.items() method
  89. def items(self): return self.translations.items()
  90. @staticmethod
  91. ## Instanciate a MlString from something
  92. # @deprecated Equivalent to MlString(translations = arg, default_value = None)
  93. def load(arg):
  94. return MlString(arg)