[core][android][darwin] Move number format to i18n

This commit is contained in:
Alexander Shalamov
2019-09-23 17:22:38 +03:00
parent 9c8295c6a6
commit e5cdc4ecf3
21 changed files with 124 additions and 56 deletions

View File

@@ -0,0 +1,15 @@
#pragma once
#include <string>
namespace mbgl {
namespace platform {
std::string formatNumber(double number,
const std::string& localeId,
const std::string& currency,
uint8_t minFractionDigits,
uint8_t maxFractionDigits);
} // namespace platform
} // namespace mbgl

View File

@@ -16,9 +16,6 @@ std::string lowercase(const std::string &string);
// Gets the name of the current thread.
std::string getCurrentThreadName();
std::string formatNumber(double number, const std::string& localeId, const std::string& currency,
uint8_t minFractionDigits, uint8_t maxFractionDigits);
// Set the name of the current thread, truncated at 15.
void setCurrentThreadName(const std::string& name);

View File

@@ -85,6 +85,10 @@ target_sources(
${MBGL_ROOT}/platform/android/src/gson/json_primitive.cpp
${MBGL_ROOT}/platform/android/src/gson/json_primitive.hpp
${MBGL_ROOT}/platform/android/src/http_file_source.cpp
${MBGL_ROOT}/platform/android/src/i18n/collator.cpp
${MBGL_ROOT}/platform/android/src/i18n/collator_jni.hpp
${MBGL_ROOT}/platform/android/src/i18n/number_format.cpp
${MBGL_ROOT}/platform/android/src/i18n/number_format_jni.hpp
${MBGL_ROOT}/platform/android/src/image.cpp
${MBGL_ROOT}/platform/android/src/java/util.cpp
${MBGL_ROOT}/platform/android/src/java/util.hpp
@@ -187,10 +191,6 @@ target_sources(
${MBGL_ROOT}/platform/android/src/style/transition_options.hpp
${MBGL_ROOT}/platform/android/src/style/value.cpp
${MBGL_ROOT}/platform/android/src/style/value.hpp
${MBGL_ROOT}/platform/android/src/i18n/collator.cpp
${MBGL_ROOT}/platform/android/src/i18n/collator_jni.hpp
${MBGL_ROOT}/platform/android/src/text/format_number.cpp
${MBGL_ROOT}/platform/android/src/text/format_number_jni.hpp
${MBGL_ROOT}/platform/android/src/text/local_glyph_rasterizer.cpp
${MBGL_ROOT}/platform/android/src/text/local_glyph_rasterizer_jni.hpp
${MBGL_ROOT}/platform/android/src/thread.cpp

View File

@@ -15,6 +15,7 @@ target_sources(
${MBGL_ROOT}/platform/darwin/src/local_glyph_rasterizer.mm
${MBGL_ROOT}/platform/darwin/src/logging_nslog.mm
${MBGL_ROOT}/platform/darwin/src/nsthread.mm
${MBGL_ROOT}/platform/darwin/src/number_format.mm
${MBGL_ROOT}/platform/darwin/src/reachability.m
${MBGL_ROOT}/platform/darwin/src/run_loop.cpp
${MBGL_ROOT}/platform/darwin/src/string_nsstring.mm

View File

@@ -15,6 +15,8 @@ target_sources(
${MBGL_ROOT}/platform/default/src/mbgl/gfx/headless_backend.cpp
${MBGL_ROOT}/platform/default/src/mbgl/gfx/headless_frontend.cpp
${MBGL_ROOT}/platform/default/src/mbgl/gl/headless_backend.cpp
${MBGL_ROOT}/platform/default/src/mbgl/i18n/collator.cpp
${MBGL_ROOT}/platform/default/src/mbgl/i18n/number_format.cpp
${MBGL_ROOT}/platform/default/src/mbgl/layermanager/layer_manager.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/asset_file_source.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/default_file_source.cpp
@@ -29,11 +31,9 @@ target_sources(
${MBGL_ROOT}/platform/default/src/mbgl/storage/online_file_source.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/sqlite3.cpp
${MBGL_ROOT}/platform/default/src/mbgl/text/bidi.cpp
${MBGL_ROOT}/platform/default/src/mbgl/i18n/collator.cpp
${MBGL_ROOT}/platform/default/src/mbgl/text/local_glyph_rasterizer.cpp
${MBGL_ROOT}/platform/default/src/mbgl/util/async_task.cpp
${MBGL_ROOT}/platform/default/src/mbgl/util/compression.cpp
${MBGL_ROOT}/platform/default/src/mbgl/util/format_number.cpp
${MBGL_ROOT}/platform/default/src/mbgl/util/image.cpp
${MBGL_ROOT}/platform/default/src/mbgl/util/jpeg_reader.cpp
${MBGL_ROOT}/platform/default/src/mbgl/util/logging_stderr.cpp

View File

@@ -83,6 +83,7 @@ target_sources(
${MBGL_ROOT}/platform/darwin/src/local_glyph_rasterizer.mm
${MBGL_ROOT}/platform/darwin/src/logging_nslog.mm
${MBGL_ROOT}/platform/darwin/src/nsthread.mm
${MBGL_ROOT}/platform/darwin/src/number_format.mm
${MBGL_ROOT}/platform/darwin/src/reachability.m
${MBGL_ROOT}/platform/darwin/src/run_loop.cpp
${MBGL_ROOT}/platform/darwin/src/string_nsstring.mm

View File

@@ -45,7 +45,7 @@ target_sources(
${MBGL_ROOT}/platform/default/src/mbgl/util/compression.cpp
${MBGL_ROOT}/platform/qt/src/async_task.cpp
${MBGL_ROOT}/platform/qt/src/async_task_impl.hpp
${MBGL_ROOT}/platform/qt/src/format_number.cpp
${MBGL_ROOT}/platform/qt/src/number_format.cpp
${MBGL_ROOT}/platform/qt/src/gl_functions.cpp
${MBGL_ROOT}/platform/qt/src/headless_backend_qt.cpp
${MBGL_ROOT}/platform/qt/src/http_file_source.cpp

View File

@@ -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"
}
}

View File

@@ -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 {

View File

@@ -2,7 +2,7 @@
#include <jni/jni.hpp>
#include "../i18n/collator_jni.hpp"
#include "collator_jni.hpp"
/*
android::NumberFormat is the JNI wrapper

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -1,4 +1,4 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/i18n/number_format.hpp>
#include <unicode/numberformatter.h>

View File

@@ -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

View File

@@ -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",

View File

@@ -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)

View File

@@ -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",

View File

@@ -333,6 +333,8 @@
"mbgl/gfx/renderer_backend.hpp": "include/mbgl/gfx/renderer_backend.hpp",
"mbgl/gl/renderable_resource.hpp": "include/mbgl/gl/renderable_resource.hpp",
"mbgl/gl/renderer_backend.hpp": "include/mbgl/gl/renderer_backend.hpp",
"mbgl/i18n/collator.hpp": "include/mbgl/i18n/collator.hpp",
"mbgl/i18n/number_format.hpp": "include/mbgl/i18n/number_format.hpp",
"mbgl/layermanager/background_layer_factory.hpp": "include/mbgl/layermanager/background_layer_factory.hpp",
"mbgl/layermanager/circle_layer_factory.hpp": "include/mbgl/layermanager/circle_layer_factory.hpp",
"mbgl/layermanager/custom_layer_factory.hpp": "include/mbgl/layermanager/custom_layer_factory.hpp",

View File

@@ -1,6 +1,6 @@
#include <mbgl/style/expression/number_format.hpp>
#include <mbgl/i18n/number_format.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/style/expression/number_format.hpp>
namespace mbgl {
namespace style {