Add a toBeginningOfSentenceCase method for Intl.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125996604
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b63bb8..a8ab1e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
## 0.13.1
* Update CLDR data to version 29.
+ * Add a toBeginningOfSentenceCase() method which converts the first character
+ of a string to uppercase. It may become more clever about that for locales
+ with different conventions over time.
## 0.13.0
* Add support for compact number formats ("1.2K") and for significant digits in
diff --git a/lib/intl.dart b/lib/intl.dart
index 4c3db81..62db076 100644
--- a/lib/intl.dart
+++ b/lib/intl.dart
@@ -443,3 +443,34 @@
toString() => "Intl($locale)";
}
+
+/// Convert a string to beginning of sentence case, in a way appropriate to the
+/// locale.
+///
+/// Currently this just converts the first letter to uppercase, which works for
+/// many locales, and we have the option to extend this to handle more cases
+/// without changing the API for clients. It also hard-codes the case of
+/// dotted i in Turkish and Azeri.
+String toBeginningOfSentenceCase(String input, [String locale]) {
+ if (input == null || input.isEmpty) return input;
+ return "${_upperCaseLetter(input[0], locale)}${input.substring(1)}";
+}
+
+/// Convert the input single-letter string to upper case. A trivial
+/// hard-coded implementation that only handles simple upper case
+/// and the dotted i in Turkish/Azeri.
+///
+/// Private to the implementation of [toBeginningOfSentenceCase].
+// TODO(alanknight): Consider hard-coding other important cases.
+// See http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt
+// TODO(alanknight): Alternatively, consider toLocaleUpperCase in browsers.
+// See also https://github.com/dart-lang/sdk/issues/6706
+String _upperCaseLetter(String input, String locale) {
+ // Hard-code the important edge case of i->İ
+ if (locale != null) {
+ if (input == "i" && locale.startsWith("tr") || locale.startsWith("az")) {
+ return "\u0130";
+ }
+ }
+ return input.toUpperCase();
+}
diff --git a/test/intl_test.dart b/test/intl_test.dart
index 268d765..5e2946e 100644
--- a/test/intl_test.dart
+++ b/test/intl_test.dart
@@ -78,4 +78,17 @@
checkAsDateDefault('en-ZZ', 'en');
checkAsDateDefault('es-999', 'es');
});
+
+ test("toBeginningOfSentenceCase", () {
+ expect(toBeginningOfSentenceCase(null), null);
+ expect(toBeginningOfSentenceCase(""), "");
+ expect(toBeginningOfSentenceCase("A"), "A");
+ expect(toBeginningOfSentenceCase("a"), "A");
+ expect(toBeginningOfSentenceCase("abc"), "Abc");
+ expect(toBeginningOfSentenceCase("[a]"), "[a]");
+ expect(toBeginningOfSentenceCase("ABc"), "ABc");
+ expect(toBeginningOfSentenceCase("ı"), "I");
+ expect(toBeginningOfSentenceCase("i"), "I");
+ expect(toBeginningOfSentenceCase("i", "tr"), "\u0130");
+ });
}