summaryrefslogtreecommitdiffstats
path: root/base/i18n
diff options
context:
space:
mode:
authoravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-22 20:40:22 +0000
committeravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-22 20:40:22 +0000
commit7a3b263af6356885628add7a8843a40dbee5b8ab (patch)
treeeff6140c44d29b6904b90366df65161364ea555f /base/i18n
parentefaba4815ff6786082583393ad06f8c22ef11fa0 (diff)
downloadchromium_src-7a3b263af6356885628add7a8843a40dbee5b8ab.zip
chromium_src-7a3b263af6356885628add7a8843a40dbee5b8ab.tar.gz
chromium_src-7a3b263af6356885628add7a8843a40dbee5b8ab.tar.bz2
Localize strings, speeds.
BUG=86527 TEST=run in non-English. For European languages, during a download the decimal separators should be commas (e.g. "0,0 MB"). (The speeds are in strings files and might take a little time to run through the translation machinery.) Review URL: http://codereview.chromium.org/7189076 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90092 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/i18n')
-rw-r--r--base/i18n/number_formatting.cc37
-rw-r--r--base/i18n/number_formatting.h18
-rw-r--r--base/i18n/number_formatting_unittest.cc92
3 files changed, 143 insertions, 4 deletions
diff --git a/base/i18n/number_formatting.cc b/base/i18n/number_formatting.cc
index a529a84..b2eeb16 100644
--- a/base/i18n/number_formatting.cc
+++ b/base/i18n/number_formatting.cc
@@ -20,6 +20,10 @@ namespace {
struct NumberFormatWrapper {
NumberFormatWrapper() {
+ Reset();
+ }
+
+ void Reset() {
// There's no ICU call to destroy a NumberFormat object other than
// operator delete, so use the default Delete, which calls operator delete.
// This can cause problems if a different allocator is used by this file
@@ -32,12 +36,14 @@ struct NumberFormatWrapper {
scoped_ptr<icu::NumberFormat> number_format;
};
-} // namespace
+LazyInstance<NumberFormatWrapper> g_number_format_int(LINKER_INITIALIZED);
+LazyInstance<NumberFormatWrapper> g_number_format_float(LINKER_INITIALIZED);
-static LazyInstance<NumberFormatWrapper> g_number_format(LINKER_INITIALIZED);
+} // namespace
string16 FormatNumber(int64 number) {
- icu::NumberFormat* number_format = g_number_format.Get().number_format.get();
+ icu::NumberFormat* number_format =
+ g_number_format_int.Get().number_format.get();
if (!number_format) {
// As a fallback, just return the raw number in a string.
@@ -49,4 +55,29 @@ string16 FormatNumber(int64 number) {
return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
}
+string16 FormatDouble(double number, int fractional_digits) {
+ icu::NumberFormat* number_format =
+ g_number_format_float.Get().number_format.get();
+
+ if (!number_format) {
+ // As a fallback, just return the raw number in a string.
+ return UTF8ToUTF16(StringPrintf("%f", number));
+ }
+ number_format->setMaximumFractionDigits(fractional_digits);
+ number_format->setMinimumFractionDigits(fractional_digits);
+ icu::UnicodeString ustr;
+ number_format->format(number, ustr);
+
+ return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
+}
+
+namespace testing {
+
+void ResetFormatters() {
+ g_number_format_int.Get().Reset();
+ g_number_format_float.Get().Reset();
+}
+
+} // namespace testing
+
} // namespace base
diff --git a/base/i18n/number_formatting.h b/base/i18n/number_formatting.h
index f5ec083..5df7f17 100644
--- a/base/i18n/number_formatting.h
+++ b/base/i18n/number_formatting.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -11,8 +11,24 @@
namespace base {
+// Return a number formatted with separators in the user's locale.
+// Ex: FormatNumber(1234567)
+// => "1,234,567" in English, "1.234.567" in German
string16 FormatNumber(int64 number);
+// Return a number formatted with separators in the user's locale.
+// Ex: FormatDouble(1234567.8, 1)
+// => "1,234,567.8" in English, "1.234.567,8" in German
+string16 FormatDouble(double number, int fractional_digits);
+
+namespace testing {
+
+// Causes cached formatters to be discarded and recreated. Only useful for
+// testing.
+void ResetFormatters();
+
+} // namespace testing
+
} // namespace base
#endif // BASE_I18N_NUMBER_FORMATTING_H_
diff --git a/base/i18n/number_formatting_unittest.cc b/base/i18n/number_formatting_unittest.cc
new file mode 100644
index 0000000..e6a54b5
--- /dev/null
+++ b/base/i18n/number_formatting_unittest.cc
@@ -0,0 +1,92 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <limits>
+
+#include "base/i18n/number_formatting.h"
+#include "base/utf_string_conversions.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "unicode/locid.h"
+
+namespace {
+
+void SetICUDefaultLocale(const std::string& locale_string) {
+ icu::Locale locale(locale_string.c_str());
+ UErrorCode error_code = U_ZERO_ERROR;
+ icu::Locale::setDefault(locale, error_code);
+ EXPECT_TRUE(U_SUCCESS(error_code));
+}
+
+} // namespace
+
+TEST(NumberFormattingTest, FormatNumber) {
+ static const struct {
+ int64 number;
+ const char* expected_english;
+ const char* expected_german;
+ } cases[] = {
+ {0, "0", "0"},
+ {1024, "1,024", "1.024"},
+ {std::numeric_limits<int64>::max(),
+ "9,223,372,036,854,775,807", "9.223.372.036.854.775.807"},
+ {std::numeric_limits<int64>::min(),
+ "-9,223,372,036,854,775,808", "-9.223.372.036.854.775.808"},
+ {-42, "-42", "-42"},
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ SetICUDefaultLocale("en");
+ base::testing::ResetFormatters();
+ EXPECT_EQ(cases[i].expected_english,
+ UTF16ToUTF8(base::FormatNumber(cases[i].number)));
+ SetICUDefaultLocale("de");
+ base::testing::ResetFormatters();
+ EXPECT_EQ(cases[i].expected_german,
+ UTF16ToUTF8(base::FormatNumber(cases[i].number)));
+ }
+}
+
+TEST(NumberFormattingTest, FormatDouble) {
+ static const struct {
+ double number;
+ int frac_digits;
+ const char* expected_english;
+ const char* expected_german;
+ } cases[] = {
+ {0.0, 0, "0", "0"},
+ {-0.0, 4, "-0.0000", "-0,0000"},
+ {1024.2, 0, "1,024", "1.024"},
+ {-1024.223, 2, "-1,024.22", "-1.024,22"},
+ {std::numeric_limits<double>::max(), 6,
+ "179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000,"
+ "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
+ "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
+ "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
+ "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
+ "000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,"
+ "000.000000",
+ "179.769.313.486.232.000.000.000.000.000.000.000.000.000.000.000.000."
+ "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
+ "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
+ "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
+ "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
+ "000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000."
+ "000,000000"},
+ {std::numeric_limits<double>::min(), 2, "0.00", "0,00"},
+ {-42.7, 3, "-42.700", "-42,700"},
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ SetICUDefaultLocale("en");
+ base::testing::ResetFormatters();
+ EXPECT_EQ(cases[i].expected_english,
+ UTF16ToUTF8(base::FormatDouble(cases[i].number,
+ cases[i].frac_digits)));
+ SetICUDefaultLocale("de");
+ base::testing::ResetFormatters();
+ EXPECT_EQ(cases[i].expected_german,
+ UTF16ToUTF8(base::FormatDouble(cases[i].number,
+ cases[i].frac_digits)));
+ }
+}