Skip to content

Commit

Permalink
Provide i18n support specific for InaSAFE Realtime
Browse files Browse the repository at this point in the history
- Adapt transifex scripts
- Add translation files
- Add dockerized qt translation tools in Makefile
  • Loading branch information
lucernae committed Jul 20, 2017
1 parent 429fb7b commit 42596b9
Show file tree
Hide file tree
Showing 19 changed files with 2,455 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .tx/config
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ source_file = i18n/inasafe_en.ts
source_lang = en
type = QT

[inasafe-code.inasafe-realtime]
file_filter = realtime/i18n/inasafe_realtime_<lang>.ts
source_file = realtime/i18n/inasafe_realtime_en.ts
source_lang = en
type = QT

31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,25 @@ update-translation-strings:
@echo "Checking current translation."
@scripts/update-strings.sh $(LOCALES)

update-realtime-translation-strings:
#update application strings
@echo "Checking current translation."
@realtime/scripts/update-strings.sh $(LOCALES)

#Qt .qm file updates - run to create binary representation of translated strings for translation in safe_qgis
compile-translation-strings:
@#Compile qt messages binary
@scripts/create_pro_file.sh
@lrelease-qt4 inasafe.pro
@rm inasafe.pro

#Qt .qm file updates - run to create binary representation of translated strings for translation in safe_qgis
compile-realtime-translation-strings:
@#Compile qt messages binary
@realtime/scripts/create_pro_file.sh
@lrelease-qt4 inasafe.pro
@rm inasafe.pro

test-translations:
@echo
@echo "----------------------------------------------------------------"
Expand Down Expand Up @@ -349,6 +361,25 @@ docker-test: testdata clean
--verbose \
--cover-package=safe safe

docker-update-translation-strings:
@echo "Update translation using docker"
@docker run -t -i -v $(PWD):/home kartoza/qt-translation make update-translation-strings

docker-update-realtime-translation-strings:
@echo "Update translation using docker"
@docker run -t -i -v $(PWD):/home kartoza/qt-translation make update-realtime-translation-strings

docker-compile-translation-strings:
@echo "Update translation using docker"
@docker run -t -i -v $(PWD):/home kartoza/qt-translation make compile-translation-strings

docker-compile-realtime-translation-strings:
@echo "Update translation using docker"
@docker run -t -i -v $(PWD):/home kartoza/qt-translation make compile-realtime-translation-strings

docker-test-translation:
@echo "Update translation using docker"
@docker run -t -i -v $(PWD):/home kartoza/qt-translation make test-translations

##########################################################
#
Expand Down
48 changes: 45 additions & 3 deletions realtime/ash/ash_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import pytz
import shutil
from PyQt4.QtCore import QObject, QFileInfo, QUrl

from PyQt4.QtCore import QCoreApplication, QObject, QFileInfo, QUrl, QTranslator
from PyQt4.QtXml import QDomDocument
from qgis.core import (
QgsProject,
Expand All @@ -22,7 +23,10 @@
from headless.tasks.utilities import download_file
from realtime.exceptions import MapComposerError
from realtime.utilities import realtime_logger_name
from safe.common.exceptions import ZeroImpactException, KeywordNotFoundError
from safe.common.exceptions import (
ZeroImpactException,
KeywordNotFoundError,
TranslationLoadError)
from safe.common.utilities import format_int
from safe.impact_functions.core import population_rounding
from safe.impact_functions.impact_function_manager import \
Expand Down Expand Up @@ -107,6 +111,8 @@ def __init__(
if not self.locale:
self.locale = 'en'

self.setup_i18n()

if not working_dir:
raise Exception("Working directory can't be empty")
self.working_dir = working_dir
Expand Down Expand Up @@ -135,7 +141,6 @@ def __init__(
self.project_path = self.working_dir_path('project.qgs')
self.impact_exists = None
self.impact_zip_path = self.working_dir_path('impact.zip')
self.locale = 'en'

self.population_path = population_path
self.cities_path = cities_path
Expand Down Expand Up @@ -847,3 +852,40 @@ def generate_report(self):
map_renderer.setDestinationCrs(default_crs)
map_renderer.setProjectionsEnabled(False)
LOGGER.info('Report generation completed.')

def setup_i18n(self):
"""Setup internationalisation for the reports.
Args:
None
Returns:
None.
Raises:
TranslationLoadException
"""
locale_name = self.locale

root = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
os.pardir,
os.pardir))
translation_path = os.path.join(
root,
'realtime',
'i18n',
'inasafe_realtime_' + str(locale_name) + '.qm')
if os.path.exists(translation_path):
self.translator = QTranslator()
result = self.translator.load(translation_path)
LOGGER.debug('Switched locale to %s' % translation_path)
if not result:
message = 'Failed to load translation for %s' % locale_name
LOGGER.exception(message)
raise TranslationLoadError(message)
# noinspection PyTypeChecker, PyCallByClass, PyArgumentList
QCoreApplication.installTranslator(self.translator)
else:
if locale_name != 'en':
message = 'No translation exists for %s' % locale_name
LOGGER.exception(message)
3 changes: 2 additions & 1 deletion realtime/earthquake/shake_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1962,8 +1962,9 @@ def setup_i18n(self):
os.pardir))
translation_path = os.path.join(
root,
'realtime',
'i18n',
'inasafe_' + str(locale_name) + '.qm')
'inasafe_realtime_' + str(locale_name) + '.qm')
if os.path.exists(translation_path):
self.translator = QTranslator()
result = self.translator.load(translation_path)
Expand Down
3 changes: 2 additions & 1 deletion realtime/flood/flood_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,9 @@ def setup_i18n(self):
os.pardir))
translation_path = os.path.join(
root,
'realtime',
'i18n',
'inasafe_' + str(locale_name) + '.qm')
'inasafe_realtime_' + str(locale_name) + '.qm')
if os.path.exists(translation_path):
self.translator = QTranslator()
result = self.translator.load(translation_path)
Expand Down
Binary file added realtime/i18n/inasafe_realtime_af.qm
Binary file not shown.
Loading

0 comments on commit 42596b9

Please sign in to comment.