listformat.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview List format and gender decision library with locale support.
  16. *
  17. * ListFormat takes an array or a var_arg of objects and generates a user
  18. * friendly list in a locale-sensitive way (i.e. "red, green, and blue").
  19. *
  20. * GenderInfo can be used to determine the gender of a list of items,
  21. * depending on the gender of all items in the list.
  22. *
  23. * In English, lists of items don't really have gender, and in fact few things
  24. * have gender. But the idea is this:
  25. * - for a list of "male items" (think "John, Steve") you use "they"
  26. * - for "Marry, Ann" (all female) you might have a "feminine" form of "they"
  27. * - and yet another form for mixed lists ("John, Marry") or undetermined
  28. * (when you don't know the gender of the items, or when they are neuter)
  29. *
  30. * For example in Greek "they" will be translated as "αυτοί" for masculin,
  31. * "αυτές" for feminin, and "αυτά" for neutral/undetermined.
  32. * (it is in fact more complicated than that, as weak/strong forms and case
  33. * also matter, see http://en.wiktionary.org/wiki/Appendix:Greek_pronouns)
  34. *
  35. */
  36. goog.provide('goog.labs.i18n.GenderInfo');
  37. goog.provide('goog.labs.i18n.GenderInfo.Gender');
  38. goog.provide('goog.labs.i18n.ListFormat');
  39. goog.require('goog.asserts');
  40. goog.require('goog.labs.i18n.ListFormatSymbols');
  41. /**
  42. * ListFormat provides a method to format a list/array of objects to a string,
  43. * in a user friendly way and in a locale sensitive manner.
  44. * If the objects are not strings, toString is called to convert them.
  45. * The constructor initializes the object based on the locale data from
  46. * the current goog.labs.i18n.ListFormatSymbols.
  47. *
  48. * Similar to the ICU4J class com.ibm.icu.text.ListFormatter:
  49. * http://icu-project.org/apiref/icu4j/com/ibm/icu/text/ListFormatter.html
  50. * @constructor
  51. * @final
  52. */
  53. goog.labs.i18n.ListFormat = function() {
  54. /**
  55. * String for lists of exactly two items, containing {0} for the first,
  56. * and {1} for the second.
  57. * For instance '{0} and {1}' will give 'black and white'.
  58. * @private {string}
  59. *
  60. * Example: for "black and white" the pattern is "{0} and {1}"
  61. * While for a longer list we have "cyan, magenta, yellow, and black"
  62. * Think "{0} start {1} middle {2} middle {3} end {4}"
  63. * The last pattern is "{0}, and {1}." Note the comma before "and".
  64. * So the "Two" pattern can be different than Start/Middle/End ones.
  65. */
  66. this.listTwoPattern_ = goog.labs.i18n.ListFormatSymbols.LIST_TWO;
  67. /**
  68. * String for the start of a list items, containing {0} for the first,
  69. * and {1} for the rest.
  70. * @private {string}
  71. */
  72. this.listStartPattern_ = goog.labs.i18n.ListFormatSymbols.LIST_START;
  73. /**
  74. * String for the start of a list items, containing {0} for the first part
  75. * of the list, and {1} for the rest of the list.
  76. * @private {string}
  77. */
  78. this.listMiddlePattern_ = goog.labs.i18n.ListFormatSymbols.LIST_MIDDLE;
  79. /**
  80. * String for the end of a list items, containing {0} for the first part
  81. * of the list, and {1} for the last item.
  82. *
  83. * This is how start/middle/end come together:
  84. * start = '{0}, {1}' middle = '{0}, {1}', end = '{0}, and {1}'
  85. * will result in the typical English list: 'one, two, three, and four'
  86. * There are languages where the patterns are more complex than
  87. * '{1} someText {1}' and the start pattern is different than the middle one.
  88. *
  89. * @private {string}
  90. */
  91. this.listEndPattern_ = goog.labs.i18n.ListFormatSymbols.LIST_END;
  92. };
  93. /**
  94. * Replaces the {0} and {1} placeholders in a pattern with the first and
  95. * the second parameter respectively, and returns the result.
  96. * It is a helper function for goog.labs.i18n.ListFormat.format.
  97. *
  98. * @param {string} pattern used for formatting.
  99. * @param {string} first object to add to list.
  100. * @param {string} second object to add to list.
  101. * @return {string} The formatted list string.
  102. * @private
  103. */
  104. goog.labs.i18n.ListFormat.prototype.patternBasedJoinTwoStrings_ = function(
  105. pattern, first, second) {
  106. return pattern.replace('{0}', first).replace('{1}', second);
  107. };
  108. /**
  109. * Formats an array of strings into a string.
  110. * It is a user facing, locale-aware list (i.e. 'red, green, and blue').
  111. *
  112. * @param {!Array<string|number>} items Items to format.
  113. * @return {string} The items formatted into a string, as a list.
  114. */
  115. goog.labs.i18n.ListFormat.prototype.format = function(items) {
  116. var count = items.length;
  117. switch (count) {
  118. case 0:
  119. return '';
  120. case 1:
  121. return String(items[0]);
  122. case 2:
  123. return this.patternBasedJoinTwoStrings_(
  124. this.listTwoPattern_, String(items[0]), String(items[1]));
  125. }
  126. var result = this.patternBasedJoinTwoStrings_(
  127. this.listStartPattern_, String(items[0]), String(items[1]));
  128. for (var i = 2; i < count - 1; ++i) {
  129. result = this.patternBasedJoinTwoStrings_(
  130. this.listMiddlePattern_, result, String(items[i]));
  131. }
  132. return this.patternBasedJoinTwoStrings_(
  133. this.listEndPattern_, result, String(items[count - 1]));
  134. };
  135. /**
  136. * GenderInfo provides a method to determine the gender of a list/array
  137. * of objects when one knows the gender of each item of the list.
  138. * It does this in a locale sensitive manner.
  139. * The constructor initializes the object based on the locale data from
  140. * the current goog.labs.i18n.ListFormatSymbols.
  141. *
  142. * Similar to the ICU4J class com.icu.util.GenderInfo:
  143. * http://icu-project.org/apiref/icu4j/com/ibm/icu/util/GenderInfo.html
  144. * @constructor
  145. * @final
  146. */
  147. goog.labs.i18n.GenderInfo = function() {
  148. /**
  149. * Stores the language-aware mode of determining the gender of a list.
  150. * @private {goog.labs.i18n.GenderInfo.ListGenderStyle_}
  151. */
  152. this.listGenderStyle_ = goog.labs.i18n.ListFormatSymbols.GENDER_STYLE;
  153. };
  154. /**
  155. * Enumeration for the possible ways to generate list genders.
  156. * Indicates the category for the locale.
  157. * This only affects gender for lists more than one. For lists of 1 item,
  158. * the gender of the list always equals the gender of that sole item.
  159. * This is for internal use, matching ICU.
  160. * @enum {number}
  161. * @private
  162. */
  163. goog.labs.i18n.GenderInfo.ListGenderStyle_ = {
  164. NEUTRAL: 0,
  165. MIXED_NEUTRAL: 1,
  166. MALE_TAINTS: 2
  167. };
  168. /**
  169. * Enumeration for the possible gender values.
  170. * Gender: OTHER means either the information is unavailable,
  171. * or the person has declined to state MALE or FEMALE.
  172. * @enum {number}
  173. */
  174. goog.labs.i18n.GenderInfo.Gender = {
  175. MALE: 0,
  176. FEMALE: 1,
  177. OTHER: 2
  178. };
  179. /**
  180. * Determines the overal gender of a list based on the gender of all the list
  181. * items, in a locale-aware way.
  182. * @param {!Array<!goog.labs.i18n.GenderInfo.Gender>} genders An array of
  183. * genders, will give the gender of the list.
  184. * @return {goog.labs.i18n.GenderInfo.Gender} Get the gender of the list.
  185. */
  186. goog.labs.i18n.GenderInfo.prototype.getListGender = function(genders) {
  187. var Gender = goog.labs.i18n.GenderInfo.Gender;
  188. var count = genders.length;
  189. if (count == 0) {
  190. return Gender.OTHER; // degenerate case
  191. }
  192. if (count == 1) {
  193. return genders[0]; // degenerate case
  194. }
  195. switch (this.listGenderStyle_) {
  196. case goog.labs.i18n.GenderInfo.ListGenderStyle_.NEUTRAL:
  197. return Gender.OTHER;
  198. case goog.labs.i18n.GenderInfo.ListGenderStyle_.MIXED_NEUTRAL:
  199. var hasFemale = false;
  200. var hasMale = false;
  201. for (var i = 0; i < count; ++i) {
  202. switch (genders[i]) {
  203. case Gender.FEMALE:
  204. if (hasMale) {
  205. return Gender.OTHER;
  206. }
  207. hasFemale = true;
  208. break;
  209. case Gender.MALE:
  210. if (hasFemale) {
  211. return Gender.OTHER;
  212. }
  213. hasMale = true;
  214. break;
  215. case Gender.OTHER:
  216. return Gender.OTHER;
  217. default: // Should never happen, but just in case
  218. goog.asserts.assert(
  219. false, 'Invalid genders[' + i + '] = ' + genders[i]);
  220. return Gender.OTHER;
  221. }
  222. }
  223. return hasMale ? Gender.MALE : Gender.FEMALE;
  224. case goog.labs.i18n.GenderInfo.ListGenderStyle_.MALE_TAINTS:
  225. for (var i = 0; i < count; ++i) {
  226. if (genders[i] != Gender.FEMALE) {
  227. return Gender.MALE;
  228. }
  229. }
  230. return Gender.FEMALE;
  231. default:
  232. return Gender.OTHER;
  233. }
  234. };