stringBundle.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. #!/usr/bin/env python
  14. # -*- coding: utf-8 -*-
  15. import re
  16. import os
  17. import sys
  18. import locale
  19. from libs.ustr import ustr
  20. try:
  21. from PyQt5.QtCore import *
  22. except ImportError:
  23. if sys.version_info.major >= 3:
  24. import sip
  25. sip.setapi('QVariant', 2)
  26. from PyQt4.QtCore import *
  27. class StringBundle:
  28. __create_key = object()
  29. def __init__(self, create_key, localeStr):
  30. assert(create_key == StringBundle.__create_key), "StringBundle must be created using StringBundle.getBundle"
  31. self.idToMessage = {}
  32. paths = self.__createLookupFallbackList(localeStr)
  33. for path in paths:
  34. self.__loadBundle(path)
  35. @classmethod
  36. def getBundle(cls, localeStr=None):
  37. if localeStr is None:
  38. try:
  39. localeStr = locale.getlocale()[0] if locale.getlocale() and len(
  40. locale.getlocale()) > 0 else os.getenv('LANG')
  41. except:
  42. print('Invalid locale')
  43. localeStr = 'en'
  44. return StringBundle(cls.__create_key, localeStr)
  45. def getString(self, stringId):
  46. assert(stringId in self.idToMessage), "Missing string id : " + stringId
  47. return self.idToMessage[stringId]
  48. def __createLookupFallbackList(self, localeStr):
  49. resultPaths = []
  50. basePath = ":/strings"
  51. resultPaths.append(basePath)
  52. if localeStr is not None:
  53. # Don't follow standard BCP47. Simple fallback
  54. tags = re.split('[^a-zA-Z]', localeStr)
  55. for tag in tags:
  56. lastPath = resultPaths[-1]
  57. resultPaths.append(lastPath + '-' + tag)
  58. return resultPaths
  59. def __loadBundle(self, path):
  60. PROP_SEPERATOR = '='
  61. f = QFile(path)
  62. if f.exists():
  63. if f.open(QIODevice.ReadOnly | QFile.Text):
  64. text = QTextStream(f)
  65. text.setCodec("UTF-8")
  66. while not text.atEnd():
  67. line = ustr(text.readLine())
  68. key_value = line.split(PROP_SEPERATOR)
  69. key = key_value[0].strip()
  70. value = PROP_SEPERATOR.join(key_value[1:]).strip().strip('"')
  71. self.idToMessage[key] = value
  72. f.close()