relativewithplurals.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2009 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 Code to make goog.date.relative plurals-aware.
  16. */
  17. goog.provide('goog.date.relativeWithPlurals');
  18. goog.require('goog.date.relative');
  19. goog.require('goog.date.relative.Unit');
  20. goog.require('goog.i18n.MessageFormat');
  21. /**
  22. * Gets a localized relative date string for a given delta and unit.
  23. * @param {number} delta Number of minutes/hours/days.
  24. * @param {boolean} future Whether the delta is in the future.
  25. * @param {goog.date.relative.Unit} unit The units the delta is in.
  26. * @return {string} The message.
  27. * @private
  28. */
  29. goog.date.relativeWithPlurals.formatTimeDelta_ = function(delta, future, unit) {
  30. if (!future && unit == goog.date.relative.Unit.MINUTES) {
  31. /**
  32. * @desc Relative date indicating how many minutes ago something happened.
  33. */
  34. var MSG_MINUTES_AGO_ICU = goog.getMsg(
  35. '{NUM, plural, ' +
  36. '=0 {# minutes ago}' +
  37. '=1 {# minute ago}' +
  38. 'other {# minutes ago}}');
  39. return new goog.i18n.MessageFormat(MSG_MINUTES_AGO_ICU).format({
  40. 'NUM': delta
  41. });
  42. } else if (future && unit == goog.date.relative.Unit.MINUTES) {
  43. /**
  44. * @desc Relative date indicating in how many minutes something happens.
  45. */
  46. var MSG_IN_MINUTES_ICU = goog.getMsg(
  47. '{NUM, plural, ' +
  48. '=0 {in # minutes}' +
  49. '=1 {in # minute}' +
  50. 'other {in # minutes}}');
  51. return new goog.i18n.MessageFormat(MSG_IN_MINUTES_ICU).format({
  52. 'NUM': delta
  53. });
  54. } else if (!future && unit == goog.date.relative.Unit.HOURS) {
  55. /**
  56. * @desc Relative date indicating how many hours ago something happened.
  57. */
  58. var MSG_HOURS_AGO_ICU = goog.getMsg(
  59. '{NUM, plural, ' +
  60. '=0 {# hours ago}' +
  61. '=1 {# hour ago}' +
  62. 'other {# hours ago}}');
  63. return new goog.i18n.MessageFormat(MSG_HOURS_AGO_ICU).format({
  64. 'NUM': delta
  65. });
  66. } else if (future && unit == goog.date.relative.Unit.HOURS) {
  67. /**
  68. * @desc Relative date indicating in how many hours something happens.
  69. */
  70. var MSG_IN_HOURS_ICU = goog.getMsg(
  71. '{NUM, plural, ' +
  72. '=0 {in # hours}' +
  73. '=1 {in # hour}' +
  74. 'other {in # hours}}');
  75. return new goog.i18n.MessageFormat(MSG_IN_HOURS_ICU).format({'NUM': delta});
  76. } else if (!future && unit == goog.date.relative.Unit.DAYS) {
  77. /**
  78. * @desc Relative date indicating how many days ago something happened.
  79. */
  80. var MSG_DAYS_AGO_ICU = goog.getMsg(
  81. '{NUM, plural, ' +
  82. '=0 {# days ago}' +
  83. '=1 {# day ago}' +
  84. 'other {# days ago}}');
  85. return new goog.i18n.MessageFormat(MSG_DAYS_AGO_ICU).format({'NUM': delta});
  86. } else if (future && unit == goog.date.relative.Unit.DAYS) {
  87. /**
  88. * @desc Relative date indicating in how many days something happens.
  89. */
  90. var MSG_IN_DAYS_ICU = goog.getMsg(
  91. '{NUM, plural, ' +
  92. '=0 {in # days}' +
  93. '=1 {in # day}' +
  94. 'other {in # days}}');
  95. return new goog.i18n.MessageFormat(MSG_IN_DAYS_ICU).format({'NUM': delta});
  96. } else {
  97. return '';
  98. }
  99. };
  100. goog.date.relative.setTimeDeltaFormatter(
  101. goog.date.relativeWithPlurals.formatTimeDelta_);