localize.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { buildLocalizeFn } from "../../_lib/buildLocalizeFn.js";
  2. const eraValues = {
  3. narrow: ["B", "A"],
  4. abbreviated: ["BC", "AD"],
  5. wide: ["Before Christ", "Anno Domini"],
  6. };
  7. const quarterValues = {
  8. narrow: ["1", "2", "3", "4"],
  9. abbreviated: ["Q1", "Q2", "Q3", "Q4"],
  10. wide: ["1st quarter", "2nd quarter", "3rd quarter", "4th quarter"],
  11. };
  12. // Note: in English, the names of days of the week and months are capitalized.
  13. // If you are making a new locale based on this one, check if the same is true for the language you're working on.
  14. // Generally, formatted dates should look like they are in the middle of a sentence,
  15. // e.g. in Spanish language the weekdays and months should be in the lowercase.
  16. const monthValues = {
  17. narrow: ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
  18. abbreviated: [
  19. "Jan",
  20. "Feb",
  21. "Mar",
  22. "Apr",
  23. "May",
  24. "Jun",
  25. "Jul",
  26. "Aug",
  27. "Sep",
  28. "Oct",
  29. "Nov",
  30. "Dec",
  31. ],
  32. wide: [
  33. "January",
  34. "February",
  35. "March",
  36. "April",
  37. "May",
  38. "June",
  39. "July",
  40. "August",
  41. "September",
  42. "October",
  43. "November",
  44. "December",
  45. ],
  46. };
  47. const dayValues = {
  48. narrow: ["S", "M", "T", "W", "T", "F", "S"],
  49. short: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
  50. abbreviated: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  51. wide: [
  52. "Sunday",
  53. "Monday",
  54. "Tuesday",
  55. "Wednesday",
  56. "Thursday",
  57. "Friday",
  58. "Saturday",
  59. ],
  60. };
  61. const dayPeriodValues = {
  62. narrow: {
  63. am: "a",
  64. pm: "p",
  65. midnight: "mi",
  66. noon: "n",
  67. morning: "morning",
  68. afternoon: "afternoon",
  69. evening: "evening",
  70. night: "night",
  71. },
  72. abbreviated: {
  73. am: "AM",
  74. pm: "PM",
  75. midnight: "midnight",
  76. noon: "noon",
  77. morning: "morning",
  78. afternoon: "afternoon",
  79. evening: "evening",
  80. night: "night",
  81. },
  82. wide: {
  83. am: "a.m.",
  84. pm: "p.m.",
  85. midnight: "midnight",
  86. noon: "noon",
  87. morning: "morning",
  88. afternoon: "afternoon",
  89. evening: "evening",
  90. night: "night",
  91. },
  92. };
  93. const formattingDayPeriodValues = {
  94. narrow: {
  95. am: "a",
  96. pm: "p",
  97. midnight: "mi",
  98. noon: "n",
  99. morning: "in the morning",
  100. afternoon: "in the afternoon",
  101. evening: "in the evening",
  102. night: "at night",
  103. },
  104. abbreviated: {
  105. am: "AM",
  106. pm: "PM",
  107. midnight: "midnight",
  108. noon: "noon",
  109. morning: "in the morning",
  110. afternoon: "in the afternoon",
  111. evening: "in the evening",
  112. night: "at night",
  113. },
  114. wide: {
  115. am: "a.m.",
  116. pm: "p.m.",
  117. midnight: "midnight",
  118. noon: "noon",
  119. morning: "in the morning",
  120. afternoon: "in the afternoon",
  121. evening: "in the evening",
  122. night: "at night",
  123. },
  124. };
  125. const ordinalNumber = (dirtyNumber, _options) => {
  126. const number = Number(dirtyNumber);
  127. // If ordinal numbers depend on context, for example,
  128. // if they are different for different grammatical genders,
  129. // use `options.unit`.
  130. //
  131. // `unit` can be 'year', 'quarter', 'month', 'week', 'date', 'dayOfYear',
  132. // 'day', 'hour', 'minute', 'second'.
  133. const rem100 = number % 100;
  134. if (rem100 > 20 || rem100 < 10) {
  135. switch (rem100 % 10) {
  136. case 1:
  137. return number + "st";
  138. case 2:
  139. return number + "nd";
  140. case 3:
  141. return number + "rd";
  142. }
  143. }
  144. return number + "th";
  145. };
  146. export const localize = {
  147. ordinalNumber,
  148. era: buildLocalizeFn({
  149. values: eraValues,
  150. defaultWidth: "wide",
  151. }),
  152. quarter: buildLocalizeFn({
  153. values: quarterValues,
  154. defaultWidth: "wide",
  155. argumentCallback: (quarter) => quarter - 1,
  156. }),
  157. month: buildLocalizeFn({
  158. values: monthValues,
  159. defaultWidth: "wide",
  160. }),
  161. day: buildLocalizeFn({
  162. values: dayValues,
  163. defaultWidth: "wide",
  164. }),
  165. dayPeriod: buildLocalizeFn({
  166. values: dayPeriodValues,
  167. defaultWidth: "wide",
  168. formattingValues: formattingDayPeriodValues,
  169. defaultFormattingWidth: "wide",
  170. }),
  171. };