diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-09-23 17:22:38 +0300 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-09-26 10:55:47 +0300 |
commit | e5cdc4ecf362dadf2da148a22cf97aaf3c9c4ecf (patch) | |
tree | ebae02fa6a6da0f4c81cb72df87c6d7359c17acd /platform | |
parent | 9c8295c6a64b6f5b2c9b67fd01633a156481a3c8 (diff) | |
download | qtlocation-mapboxgl-e5cdc4ecf362dadf2da148a22cf97aaf3c9c4ecf.tar.gz |
[core][android][darwin] Move number format to i18n
Diffstat (limited to 'platform')
-rw-r--r-- | platform/android/core-files.json | 7 | ||||
-rw-r--r-- | platform/android/src/i18n/number_format.cpp (renamed from platform/android/src/text/format_number.cpp) | 5 | ||||
-rw-r--r-- | platform/android/src/i18n/number_format_jni.hpp (renamed from platform/android/src/text/format_number_jni.hpp) | 2 | ||||
-rw-r--r-- | platform/android/src/jni_native.cpp | 2 | ||||
-rw-r--r-- | platform/darwin/src/number_format.mm | 36 | ||||
-rw-r--r-- | platform/darwin/src/string_nsstring.mm | 31 | ||||
-rw-r--r-- | platform/default/src/mbgl/i18n/format_number.cpp (renamed from platform/default/src/mbgl/util/format_number.cpp) | 2 | ||||
-rw-r--r-- | platform/default/src/mbgl/i18n/number_format.cpp | 41 | ||||
-rw-r--r-- | platform/ios/core-files.json | 1 | ||||
-rw-r--r-- | platform/linux/config.cmake | 12 | ||||
-rw-r--r-- | platform/macos/core-files.json | 1 | ||||
-rw-r--r-- | platform/qt/src/number_format.cpp (renamed from platform/qt/src/format_number.cpp) | 0 |
12 files changed, 96 insertions, 44 deletions
diff --git a/platform/android/core-files.json b/platform/android/core-files.json index 4057b21000..e442883e47 100644 --- a/platform/android/core-files.json +++ b/platform/android/core-files.json @@ -34,6 +34,8 @@ "platform/android/src/gson/json_element.cpp", "platform/android/src/gson/json_object.cpp", "platform/android/src/gson/json_primitive.cpp", + "platform/android/src/i18n/collator.cpp", + "platform/android/src/i18n/number_format.cpp", "platform/android/src/image.cpp", "platform/android/src/java/util.cpp", "platform/android/src/java_types.cpp", @@ -80,9 +82,7 @@ "platform/android/src/style/sources/vector_source.cpp", "platform/android/src/style/transition_options.cpp", "platform/android/src/style/value.cpp", - "platform/android/src/i18n/collator.cpp", "platform/android/src/text/local_glyph_rasterizer.cpp", - "platform/android/src/text/format_number.cpp", "platform/android/src/gl_functions.cpp", "platform/android/src/thread.cpp", "platform/android/src/timer.cpp", @@ -143,6 +143,8 @@ "java_types.hpp": "platform/android/src/java_types.hpp", "jni.hpp": "platform/android/src/jni.hpp", "jni_native.hpp": "platform/android/src/jni_native.hpp", + "i18n/collator_jni.hpp": "platform/android/src/i18n/collator_jni.hpp", + "i18n/number_format_jni.hpp": "platform/android/src/i18n/number_format_jni.hpp", "logger.hpp": "platform/android/src/logger.hpp", "map/camera_position.hpp": "platform/android/src/map/camera_position.hpp", "map/image.hpp": "platform/android/src/map/image.hpp", @@ -185,7 +187,6 @@ "style/sources/vector_source.hpp": "platform/android/src/style/sources/vector_source.hpp", "style/transition_options.hpp": "platform/android/src/style/transition_options.hpp", "style/value.hpp": "platform/android/src/style/value.hpp", - "text/collator_jni.hpp": "platform/android/src/i18n/collator_jni.hpp", "text/local_glyph_rasterizer_jni.hpp": "platform/android/src/text/local_glyph_rasterizer_jni.hpp" } } diff --git a/platform/android/src/text/format_number.cpp b/platform/android/src/i18n/number_format.cpp index 3a41175ecc..c8e0b70562 100644 --- a/platform/android/src/text/format_number.cpp +++ b/platform/android/src/i18n/number_format.cpp @@ -1,11 +1,10 @@ -#include <mbgl/style/expression/collator.hpp> +#include <mbgl/i18n/number_format.hpp> #include <mbgl/text/language_tag.hpp> -#include <mbgl/util/platform.hpp> #include <jni/jni.hpp> #include "../attach_env.hpp" -#include "format_number_jni.hpp" +#include "number_format_jni.hpp" namespace mbgl { namespace android { diff --git a/platform/android/src/text/format_number_jni.hpp b/platform/android/src/i18n/number_format_jni.hpp index 1b63012c6e..1720038925 100644 --- a/platform/android/src/text/format_number_jni.hpp +++ b/platform/android/src/i18n/number_format_jni.hpp @@ -2,7 +2,7 @@ #include <jni/jni.hpp> -#include "../i18n/collator_jni.hpp" +#include "collator_jni.hpp" /* android::NumberFormat is the JNI wrapper diff --git a/platform/android/src/jni_native.cpp b/platform/android/src/jni_native.cpp index 966dc6a007..9fe14f8f1f 100644 --- a/platform/android/src/jni_native.cpp +++ b/platform/android/src/jni_native.cpp @@ -51,9 +51,9 @@ #include "snapshotter/map_snapshot.hpp" #endif #include "i18n/collator_jni.hpp" +#include "i18n/number_format_jni.hpp" #include "logger.hpp" #include "text/local_glyph_rasterizer_jni.hpp" -#include "text/format_number_jni.hpp" namespace mbgl { namespace android { diff --git a/platform/darwin/src/number_format.mm b/platform/darwin/src/number_format.mm new file mode 100644 index 0000000000..86f16eb6dd --- /dev/null +++ b/platform/darwin/src/number_format.mm @@ -0,0 +1,36 @@ +#import <Foundation/Foundation.h> + +#include <mbgl/i18n/number_format.hpp> + +namespace mbgl { +namespace platform { + +std::string formatNumber(double number, const std::string& localeId, const std::string& currency, + uint8_t minFractionDigits, uint8_t maxFractionDigits) { + + static NSNumberFormatter *numberFormatter; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + numberFormatter = [[NSNumberFormatter alloc] init]; + }); + + numberFormatter.locale = !localeId.empty() ? [NSLocale localeWithLocaleIdentifier:@(localeId.c_str())] : nil; + numberFormatter.currencyCode = !currency.empty() ? @(currency.c_str()) : nil; + if (currency.empty()) { + numberFormatter.minimumFractionDigits = minFractionDigits; + numberFormatter.maximumFractionDigits = maxFractionDigits; + numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; + } else { + // Reset fraction digits to NSNumberFormatter's default values, so that the + // system will choose formatting based on number formatter locale. + numberFormatter.minimumFractionDigits = 0; + numberFormatter.maximumFractionDigits = 0; + numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; + } + NSString *formatted = [numberFormatter stringFromNumber:@(number)]; + std::string result = std::string([formatted UTF8String]); + return result; +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/darwin/src/string_nsstring.mm b/platform/darwin/src/string_nsstring.mm index 382a2acf33..d7c81236ef 100644 --- a/platform/darwin/src/string_nsstring.mm +++ b/platform/darwin/src/string_nsstring.mm @@ -27,32 +27,5 @@ std::string lowercase(const std::string &string) { return result; } -std::string formatNumber(double number, const std::string& localeId, const std::string& currency, - uint8_t minFractionDigits, uint8_t maxFractionDigits) { - - static NSNumberFormatter *numberFormatter; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - numberFormatter = [[NSNumberFormatter alloc] init]; - }); - - numberFormatter.locale = !localeId.empty() ? [NSLocale localeWithLocaleIdentifier:@(localeId.c_str())] : nil; - numberFormatter.currencyCode = !currency.empty() ? @(currency.c_str()) : nil; - if (currency.empty()) { - numberFormatter.minimumFractionDigits = minFractionDigits; - numberFormatter.maximumFractionDigits = maxFractionDigits; - numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; - } else { - // Reset fraction digits to NSNumberFormatter's default values, so that the - // system will choose formatting based on number formatter locale. - numberFormatter.minimumFractionDigits = 0; - numberFormatter.maximumFractionDigits = 0; - numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle; - } - NSString *formatted = [numberFormatter stringFromNumber:@(number)]; - std::string result = std::string([formatted UTF8String]); - return result; -} - -} -} +} // namespace platform +} // namespace mbgl diff --git a/platform/default/src/mbgl/util/format_number.cpp b/platform/default/src/mbgl/i18n/format_number.cpp index d1b51e11a1..7b6f24221d 100644 --- a/platform/default/src/mbgl/util/format_number.cpp +++ b/platform/default/src/mbgl/i18n/format_number.cpp @@ -1,4 +1,4 @@ -#include <mbgl/util/platform.hpp> +#include <mbgl/i18n/number_format.hpp> #include <unicode/numberformatter.h> diff --git a/platform/default/src/mbgl/i18n/number_format.cpp b/platform/default/src/mbgl/i18n/number_format.cpp new file mode 100644 index 0000000000..7f2bc5a5ef --- /dev/null +++ b/platform/default/src/mbgl/i18n/number_format.cpp @@ -0,0 +1,41 @@ +#include <mbgl/i18n/number_format.hpp> + +#include <unicode/numberformatter.h> + +namespace mbgl { +namespace platform { + +std::string formatNumber(double number, + const std::string& localeId, + const std::string& currency, + uint8_t minFractionDigits, + uint8_t maxFractionDigits) { + UErrorCode status = U_ZERO_ERROR; + icu::UnicodeString ustr; + std::string formatted; + + icu::Locale locale = icu::Locale(localeId.c_str()); + // Print the value as currency + if (!currency.empty()) { + icu::UnicodeString ucurrency = icu::UnicodeString::fromUTF8(currency); + ustr = icu::number::NumberFormatter::with() + .unit(icu::CurrencyUnit(ucurrency.getBuffer(), status)) + .locale(locale) + .formatDouble(number, status) + .toString(); + } else { + ustr = icu::number::NumberFormatter::with() +#if U_ICU_VERSION_MAJOR_NUM >= 62 + .precision(icu::number::Precision::minMaxFraction(minFractionDigits, maxFractionDigits)) +#else + .rounding(icu::number::Rounder::minMaxFraction(minFractionDigits, maxFractionDigits)) +#endif + .locale(locale) + .formatDouble(number, status) + .toString(); + } + return ustr.toUTF8String(formatted); +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/ios/core-files.json b/platform/ios/core-files.json index c4930b1667..7c916b027b 100644 --- a/platform/ios/core-files.json +++ b/platform/ios/core-files.json @@ -7,6 +7,7 @@ "platform/darwin/src/image.mm", "platform/darwin/src/local_glyph_rasterizer.mm", "platform/darwin/src/logging_nslog.mm", + "platform/darwin/src/number_format.mm", "platform/darwin/src/nsthread.mm", "platform/darwin/src/reachability.m", "platform/darwin/src/string_nsstring.mm", diff --git a/platform/linux/config.cmake b/platform/linux/config.cmake index 01c46c9fcb..26de2430ce 100644 --- a/platform/linux/config.cmake +++ b/platform/linux/config.cmake @@ -45,17 +45,17 @@ macro(mbgl_platform_core) PRIVATE platform/linux/src/gl_functions.cpp # Misc + PRIVATE platform/default/src/mbgl/i18n/collator.cpp + PRIVATE platform/default/src/mbgl/i18n/number_format.cpp + PRIVATE platform/default/src/mbgl/text/bidi.cpp + PRIVATE platform/default/src/mbgl/text/local_glyph_rasterizer.cpp + PRIVATE platform/default/src/mbgl/layermanager/layer_manager.cpp PRIVATE platform/default/src/mbgl/util/compression.cpp PRIVATE platform/default/src/mbgl/util/logging_stderr.cpp PRIVATE platform/default/src/mbgl/util/string_stdlib.cpp PRIVATE platform/default/src/mbgl/util/thread.cpp - PRIVATE platform/default/src/mbgl/text/bidi.cpp - PRIVATE platform/default/src/mbgl/i18n/collator.cpp - PRIVATE platform/default/src/mbgl/layermanager/layer_manager.cpp - PRIVATE platform/default/src/mbgl/text/local_glyph_rasterizer.cpp PRIVATE platform/default/src/mbgl/util/thread_local.cpp PRIVATE platform/default/src/mbgl/util/utf.cpp - PRIVATE platform/default/src/mbgl/util/format_number.cpp # Image handling PRIVATE platform/default/src/mbgl/util/image.cpp @@ -86,7 +86,7 @@ macro(mbgl_platform_core) target_add_mason_package(mbgl-core PRIVATE icu) # Ignore warning caused by ICU header unistr.h in some CI environments - set_source_files_properties(platform/default/src/mbgl/util/format_number.cpp PROPERTIES COMPILE_FLAGS -Wno-error=shadow) + set_source_files_properties(platform/default/src/mbgl/i18n/number_format.cpp PROPERTIES COMPILE_FLAGS -Wno-error=shadow) # Link all ICU libraries (by default only libicuuc is linked) find_library(LIBICUI18N NAMES icui18n HINTS ${MASON_PACKAGE_icu_INCLUDE_DIRS}/../lib) diff --git a/platform/macos/core-files.json b/platform/macos/core-files.json index 21b0e698f0..b0536c4863 100644 --- a/platform/macos/core-files.json +++ b/platform/macos/core-files.json @@ -7,6 +7,7 @@ "platform/darwin/src/local_glyph_rasterizer.mm", "platform/darwin/src/logging_nslog.mm", "platform/darwin/src/nsthread.mm", + "platform/darwin/src/number_format.mm", "platform/darwin/src/reachability.m", "platform/darwin/src/string_nsstring.mm", "platform/default/src/mbgl/gfx/headless_backend.cpp", diff --git a/platform/qt/src/format_number.cpp b/platform/qt/src/number_format.cpp index b6fe3558e6..b6fe3558e6 100644 --- a/platform/qt/src/format_number.cpp +++ b/platform/qt/src/number_format.cpp |