def test_qgis_app_locale(self): """Test for qgis app locale.""" from safe.definitions.constants import no_field self.assertEqual(no_field, u'No Field') # run qgis on bahasa _ = get_qgis_app('id', INASAFE_TEST) # test if string from inasafe module are translated from safe.definitions.constants import no_field self.assertNotEqual(no_field, u'No Field') expected_locale = 'id' self.assertEqual(locale(INASAFE_TEST), expected_locale) # check for bahasa translation expected_message = ( 'Tidak ada informasi gaya yang ditemukan pada lapisan %s') real_message = tr('No styleInfo was found for layer %s') message = 'expected %s but got %s' % (expected_message, real_message) self.assertEqual(expected_message, real_message, message) # run qgis on portuguese _ = get_qgis_app('pt', qsetting=INASAFE_TEST) expected_locale = 'pt' self.assertEqual(locale(INASAFE_TEST), expected_locale) # check for english translation expected_message = (u'Começando') real_message = tr('Getting started') message = 'expected %s but got %s' % (expected_message, real_message) self.assertEqual(expected_message, real_message, message) # run qgis on english _ = get_qgis_app(qsetting=INASAFE_TEST) expected_locale = 'en' self.assertEqual(locale(INASAFE_TEST), expected_locale) # check for english translation expected_message = ('No styleInfo was found for layer %s') real_message = tr('No styleInfo was found for layer %s') message = 'expected %s but got %s' % (expected_message, real_message) self.assertEqual(expected_message, real_message, message) # Set back to en os.environ['LANG'] = 'en'
def add_separators(x): """Format integer with separator between thousands. :param x: A number to be formatted in a locale friendly way. :type x: int :returns: A locale friendly formatted string e.g. 1,000,0000.00 representing the original x. If a ValueError exception occurs, x is simply returned. :rtype: basestring From http:// stackoverflow.com/questions/5513615/add-thousands-separators-to-a-number Instead use this: http://docs.python.org/library/string.html#formatspec """ try: s = '{0:,}'.format(x) # s = '{0:n}'.format(x) # n means locale aware (read up on this) # see issue #526 except ValueError: return x # Quick solution for the moment if locale() in ['id', 'fr']: # Replace commas with the correct thousand separator. s = s.replace(',', thousand_separator()) return s
def download(feature_type, output_base_path, extent, progress_dialog=None): """Download shapefiles from Kartoza server. .. versionadded:: 3.2 :param feature_type: What kind of features should be downloaded. Currently 'buildings', 'building-points' or 'roads' are supported. :type feature_type: str :param output_base_path: The base path of the shape file. :type output_base_path: str :param extent: A list in the form [xmin, ymin, xmax, ymax] where all coordinates provided are in Geographic / EPSG:4326. :type extent: list :param progress_dialog: A progress dialog. :type progress_dialog: QProgressDialog :raises: ImportDialogError, CanceledImportDialogError """ # preparing necessary data min_longitude = extent[0] min_latitude = extent[1] max_longitude = extent[2] max_latitude = extent[3] box = ( '{min_longitude},{min_latitude},{max_longitude},' '{max_latitude}').format( min_longitude=min_longitude, min_latitude=min_latitude, max_longitude=max_longitude, max_latitude=max_latitude ) url = ( '{url_osm_prefix}' '{feature_type}' '{url_osm_suffix}?' 'bbox={box}&' 'qgis_version={qgis}&' 'lang={lang}&' 'inasafe_version={inasafe_version}'.format( url_osm_prefix=URL_OSM_PREFIX, feature_type=feature_type, url_osm_suffix=URL_OSM_SUFFIX, box=box, qgis=qgis_version(), lang=locale(), inasafe_version=get_version())) path = tempfile.mktemp('.shp.zip') # download and extract it fetch_zip(url, path, feature_type, progress_dialog) extract_zip(path, output_base_path) if progress_dialog: progress_dialog.done(QDialog.Accepted)
def download(feature_type, output_base_path, extent, progress_dialog=None): """Download shapefiles from Kartoza server. .. versionadded:: 3.2 :param feature_type: What kind of features should be downloaded. Currently 'buildings', 'building-points' or 'roads' are supported. :type feature_type: str :param output_base_path: The base path of the shape file. :type output_base_path: str :param extent: A list in the form [xmin, ymin, xmax, ymax] where all coordinates provided are in Geographic / EPSG:4326. :type extent: list :param progress_dialog: A progress dialog. :type progress_dialog: QProgressDialog :raises: ImportDialogError, CanceledImportDialogError """ # preparing necessary data min_longitude = extent[0] min_latitude = extent[1] max_longitude = extent[2] max_latitude = extent[3] box = ( '{min_longitude},{min_latitude},{max_longitude},' '{max_latitude}').format( min_longitude=min_longitude, min_latitude=min_latitude, max_longitude=max_longitude, max_latitude=max_latitude ) url = ( '{url_osm_prefix}' '{feature_type}' '{url_osm_suffix}?' 'bbox={box}&' 'qgis_version={qgis}&' 'lang={lang}&' 'inasafe_version={inasafe_version}'.format( url_osm_prefix=URL_OSM_PREFIX, feature_type=feature_type, url_osm_suffix=URL_OSM_SUFFIX, box=box, qgis=qgis_version(), lang=locale(), inasafe_version=get_version())) path = tempfile.mktemp('.shp.zip') # download and extract it fetch_zip(url, path, feature_type, progress_dialog) extract_zip(path, output_base_path) if progress_dialog: progress_dialog.done(QDialog.Accepted)
def test_qgis_app_locale(self): """Test for qgis app locale.""" from safe.definitions.constants import no_field self.assertEqual(no_field, u'No Field') # run qgis on bahasa _ = get_qgis_app('id', INASAFE_TEST) # test if string from inasafe module are translated from safe.definitions.constants import no_field self.assertNotEqual(no_field, u'No Field') expected_locale = 'id' self.assertEqual(locale(INASAFE_TEST), expected_locale) # check for bahasa translation expected_message = ( 'Tidak ada informasi gaya yang ditemukan pada lapisan %s') real_message = tr( 'No styleInfo was found for layer %s') message = 'expected %s but got %s' % (expected_message, real_message) self.assertEqual(expected_message, real_message, message) # run qgis on english _ = get_qgis_app(qsetting=INASAFE_TEST) expected_locale = 'en' self.assertEqual(locale(INASAFE_TEST), expected_locale) # check for english translation expected_message = ( 'No styleInfo was found for layer %s') real_message = tr( 'No styleInfo was found for layer %s') message = 'expected %s but got %s' % (expected_message, real_message) self.assertEqual(expected_message, real_message, message) # Set back to en os.environ['LANG'] = 'en'
def decimal_separator(): """Return decimal separator according to the locale. :return: The decimal separator. :rtype: basestring """ lang = locale() if lang in ['id', 'fr']: return ',' else: return '.'
def thousand_separator(): """Return thousand separator according to the locale. :return: The thousand separator. :rtype: basestring """ lang = locale() if lang in ['id']: return '.' elif lang in ['fr']: return ' ' else: return ','
def test_format_int(self): """Test formatting integer.""" lang = locale() number = 10000000 formatted_int = add_separators(number) if lang == 'id': expected_str = '10.000.000' else: expected_str = '10,000,000' self.assertEqual(expected_str, formatted_int) number = 1234 formatted_int = add_separators(number) if lang == 'id': expected_str = '1.234' else: expected_str = '1,234' self.assertEqual(expected_str, formatted_int)
def test_format_int(self): """Test formatting integer""" lang = locale() number = 10000000 formatted_int = add_separators(number) if lang == 'id': expected_str = '10.000.000' else: expected_str = '10,000,000' self.assertEqual(expected_str, formatted_int) number = 1234 formatted_int = add_separators(number) if lang == 'id': expected_str = '1.234' else: expected_str = '1,234' self.assertEqual(expected_str, formatted_int)
def specific_analysis_question(hazard, exposure): """Return a translated hardcoded analysis question a given hazard/exposure. :param hazard: The hazard definition. :type hazard: safe.definition.hazard :param exposure: The exposure definition. :type hazard: safe.definition.exposure :return: The analysis question or None if it's not hardcoded. :rtype: basestring """ lang = locale() for item in ITEMS: if item['hazard'] == hazard and item['exposure'] == exposure: analysis_questions = item.get('analysis_question', None) if not analysis_questions: return None return analysis_questions.get(lang, None) return None
def specific_analysis_question(hazard, exposure): """Return a translated hardcoded analysis question a given hazard/exposure. :param hazard: The hazard definition. :type hazard: safe.definition.hazard :param exposure: The exposure definition. :type hazard: safe.definition.exposure :return: The analysis question or None if it's not hardcoded. :rtype: basestring """ lang = locale() for item in ITEMS: if item['hazard'] == hazard and item['exposure'] == exposure: analysis_questions = item.get('analysis_question', None) if not analysis_questions: return None return analysis_questions.get(lang, None) return None
def format_int(x): """Format integer with separator between thousands. Args: x: int - a number to be formatted in a locale friendly way. Returns: str - a locale friendly formatted string e.g. 1,000,0000.00 representing the original x. If a ValueError exception occurs, x is simply returned. Raises: None From http://stackoverflow.com/questions/5513615/ add-thousands-separators-to-a-number # FIXME (Ole) Currently not using locale coz broken Instead use this: http://docs.python.org/library/string.html#formatspec """ try: s = '{0:,}'.format(x) # s = '{0:n}'.format(x) # n means locale aware (read up on this) # see issue #526 except ValueError: return x # Quick solution for the moment if locale() == 'id': # Replace commas with dots s = s.replace(',', '.') return s