UNPKG

@qooxdoo/framework

Version:

The JS Framework for Coders

505 lines (437 loc) 15.2 kB
/* ************************************************************************ qooxdoo - the new era of web development http://qooxdoo.org Copyright: 2004-2008 1&1 Internet AG, Germany, http://www.1und1.de License: MIT: https://opensource.org/licenses/MIT See the LICENSE file in the project's top-level directory for details. Authors: * Sebastian Werner (wpbasti) * Andreas Ecker (ecker) * Fabian Jakobs (fjakobs) ************************************************************************ */ /** * Static class that provides localized date information (like names of week * days, AM/PM markers, start of week, etc.). * * @cldr() */ qx.Class.define("qx.locale.Date", { statics : { /** * Reference to the locale manager. * * @internal */ __mgr : qx.locale.Manager.getInstance(), /** * Get AM marker for time definitions * * @param locale {String} optional locale to be used * @return {String} translated AM marker. */ getAmMarker : function(locale) { return this.__mgr.localize("cldr_am", [], locale); }, /** * Get PM marker for time definitions * * @param locale {String} optional locale to be used * @return {String} translated PM marker. */ getPmMarker : function(locale) { return this.__mgr.localize("cldr_pm", [], locale); }, /** * Return localized names of day names * * @param length {String} format of the day names. * Possible values: "abbreviated", "narrow", "wide" * @param locale {String} optional locale to be used * @param context {String} (default: "format") intended context. * Possible values: "format", "stand-alone" * @param withFallback {Boolean?} if true, the previous parameter's other value is tried * in order to find a localized name for the day * @return {String[]} array of localized day names starting with sunday. */ getDayNames : function(length, locale, context, withFallback) { var context = context ? context : "format"; if (qx.core.Environment.get("qx.debug")) { qx.core.Assert.assertInArray(length, ["abbreviated", "narrow", "wide"]); qx.core.Assert.assertInArray(context, ["format", "stand-alone"]); } var days = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ]; var names = []; for (var i=0; i<days.length; i++) { var key = "cldr_day_" + context + "_" + length + "_" + days[i]; names.push(withFallback ? this.__localizeWithFallback(context, context === 'format' ? 'stand-alone' : 'format', key, locale) : this.__mgr.localize(key, [], locale)); } return names; }, /** * Return localized name of a week day name * * @param length {String} format of the day name. * Possible values: "abbreviated", "narrow", "wide" * @param day {Integer} day number. 0=sunday, 1=monday, ... * @param locale {String} optional locale to be used * @param context {String} (default: "format") intended context. * Possible values: "format", "stand-alone" * @param withFallback {Boolean?} if true, the previous parameter's other value is tried * in order to find a localized name for the day * @return {String} localized day name */ getDayName : function(length, day, locale, context, withFallback) { var context = context ? context : "format"; if (qx.core.Environment.get("qx.debug")) { qx.core.Assert.assertInArray(length, ["abbreviated", "narrow", "wide"]); qx.core.Assert.assertInteger(day); qx.core.Assert.assertInRange(day, 0, 6); qx.core.Assert.assertInArray(context, ["format", "stand-alone"]); } var days = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ]; var key = "cldr_day_" + context + "_" + length + "_" + days[day]; return withFallback ? this.__localizeWithFallback(context, context === 'format' ? 'stand-alone' : 'format', key, locale) : this.__mgr.localize(key, [], locale); }, /** * Return localized names of month names * * @param length {String} format of the month names. * Possible values: "abbreviated", "narrow", "wide" * @param locale {String} optional locale to be used * @param context {String} (default: "format") intended context. * Possible values: "format", "stand-alone" * @param withFallback {Boolean?} if true, the previous parameter's other value is tried * in order to find a localized name for the month * @return {String[]} array of localized month names starting with january. */ getMonthNames : function(length, locale, context, withFallback) { var context = context ? context : "format"; if (qx.core.Environment.get("qx.debug")) { qx.core.Assert.assertInArray(length, ["abbreviated", "narrow", "wide"]); qx.core.Assert.assertInArray(context, ["format", "stand-alone"]); } var names = []; for (var i=0; i<12; i++) { var key = "cldr_month_" + context + "_" + length + "_" + (i + 1); names.push(withFallback ? this.__localizeWithFallback(context, context === 'format' ? 'stand-alone' : 'format', key, locale) : this.__mgr.localize(key, [], locale)); } return names; }, /** * Return localized name of a month * * @param length {String} format of the month names. * Possible values: "abbreviated", "narrow", "wide" * @param month {Integer} index of the month. 0=january, 1=february, ... * @param locale {String} optional locale to be used * @param context {String} (default: "format") intended context. * Possible values: "format", "stand-alone" * @param withFallback {Boolean?} if true, the previous parameter's other value is tried * in order to find a localized name for the month * @return {String} localized month name */ getMonthName : function(length, month, locale, context, withFallback) { var context = context ? context : "format"; if (qx.core.Environment.get("qx.debug")) { qx.core.Assert.assertInArray(length, ["abbreviated", "narrow", "wide"]); qx.core.Assert.assertInArray(context, ["format", "stand-alone"]); } var key = "cldr_month_" + context + "_" + length + "_" + (month + 1); return withFallback ? this.__localizeWithFallback(context, context === 'format' ? 'stand-alone' : 'format', key, locale) : this.__mgr.localize(key, [], locale); }, /** * Return localized date format string to be used with {@link qx.util.format.DateFormat}. * * @param size {String} format of the date format. * Possible values: "short", "medium", "long", "full" * @param locale {String?} optional locale to be used * @return {String} localized date format string */ getDateFormat : function(size, locale) { if (qx.core.Environment.get("qx.debug")) { qx.core.Assert.assertInArray(size, ["short", "medium", "long", "full"]); } var key = "cldr_date_format_" + size; return this.__mgr.localize(key, [], locale); }, /** * Try to localize a date/time format string. For format string possibilities see * <a href="http://cldr.unicode.org/translation/date-time">Date/Time Symbol reference</a> * at CLDR - Unicode Common Locale Data Repository. * * If no localization is available take the fallback format string. * * @param canonical {String} format string containing only field information, and in a canonical order. * Examples are "yyyyMMMM" for year + full month, or "MMMd" for abbreviated month + day. * @param fallback {String} fallback format string if no localized version is found * @param locale {String} optional locale to be used * @return {String} best matching format string */ getDateTimeFormat : function(canonical, fallback, locale) { var key = "cldr_date_time_format_" + canonical; var localizedFormat = this.__mgr.localize(key, [], locale); if (localizedFormat == key) { localizedFormat = fallback; } return localizedFormat; }, /** * Return localized time format string to be used with {@link qx.util.format.DateFormat}. * * @param size {String} format of the time pattern. * Possible values: "short", "medium", "long", "full" * @param locale {String} optional locale to be used * @return {String} localized time format string */ getTimeFormat : function(size, locale) { if (qx.core.Environment.get("qx.debug")) { qx.core.Assert.assertInArray(size, ["short", "medium", "long", "full"]); } var key = "cldr_time_format_" + size; var localizedFormat = this.__mgr.localize(key, [], locale); if (localizedFormat != key) { return localizedFormat; } switch(size) { case "short": case "medium": return qx.locale.Date.getDateTimeFormat("HHmm", "HH:mm"); case "long": return qx.locale.Date.getDateTimeFormat("HHmmss", "HH:mm:ss"); case "full": return qx.locale.Date.getDateTimeFormat("HHmmsszz", "HH:mm:ss zz"); default: throw new Error("This case should never happen."); } }, /** * Return the day the week starts with * * Reference: Common Locale Data Repository (cldr) supplementalData.xml * * @param locale {String} optional locale to be used * @return {Integer} index of the first day of the week. 0=sunday, 1=monday, ... */ getWeekStart : function(locale) { var weekStart = { // default is monday "MV" : 5, // friday "AE" : 6, // saturday "AF" : 6, "BH" : 6, "DJ" : 6, "DZ" : 6, "EG" : 6, "ER" : 6, "ET" : 6, "IQ" : 6, "IR" : 6, "JO" : 6, "KE" : 6, "KW" : 6, "LB" : 6, "LY" : 6, "MA" : 6, "OM" : 6, "QA" : 6, "SA" : 6, "SD" : 6, "SO" : 6, "TN" : 6, "YE" : 6, "AS" : 0, // sunday "AU" : 0, "AZ" : 0, "BW" : 0, "CA" : 0, "CN" : 0, "FO" : 0, "GE" : 0, "GL" : 0, "GU" : 0, "HK" : 0, "IE" : 0, "IL" : 0, "IS" : 0, "JM" : 0, "JP" : 0, "KG" : 0, "KR" : 0, "LA" : 0, "MH" : 0, "MN" : 0, "MO" : 0, "MP" : 0, "MT" : 0, "NZ" : 0, "PH" : 0, "PK" : 0, "SG" : 0, "TH" : 0, "TT" : 0, "TW" : 0, "UM" : 0, "US" : 0, "UZ" : 0, "VI" : 0, "ZA" : 0, "ZW" : 0, "MW" : 0, "NG" : 0, "TJ" : 0 }; var territory = qx.locale.Date._getTerritory(locale); // default is monday return weekStart[territory] != null ? weekStart[territory] : 1; }, /** * Return the day the weekend starts with * * Reference: Common Locale Data Repository (cldr) supplementalData.xml * * @param locale {String} optional locale to be used * @return {Integer} index of the first day of the weekend. 0=sunday, 1=monday, ... */ getWeekendStart : function(locale) { var weekendStart = { // default is saturday "EG" : 5, // friday "IL" : 5, "SY" : 5, "IN" : 0, // sunday "AE" : 4, // thursday "BH" : 4, "DZ" : 4, "IQ" : 4, "JO" : 4, "KW" : 4, "LB" : 4, "LY" : 4, "MA" : 4, "OM" : 4, "QA" : 4, "SA" : 4, "SD" : 4, "TN" : 4, "YE" : 4 }; var territory = qx.locale.Date._getTerritory(locale); // default is saturday return weekendStart[territory] != null ? weekendStart[territory] : 6; }, /** * Return the day the weekend ends with * * Reference: Common Locale Data Repository (cldr) supplementalData.xml * * @param locale {String} optional locale to be used * @return {Integer} index of the last day of the weekend. 0=sunday, 1=monday, ... */ getWeekendEnd : function(locale) { var weekendEnd = { // default is sunday "AE" : 5, // friday "BH" : 5, "DZ" : 5, "IQ" : 5, "JO" : 5, "KW" : 5, "LB" : 5, "LY" : 5, "MA" : 5, "OM" : 5, "QA" : 5, "SA" : 5, "SD" : 5, "TN" : 5, "YE" : 5, "AF" : 5, "IR" : 5, "EG" : 6, // saturday "IL" : 6, "SY" : 6 }; var territory = qx.locale.Date._getTerritory(locale); // default is sunday return weekendEnd[territory] != null ? weekendEnd[territory] : 0; }, /** * Returns whether a certain day of week belongs to the week end. * * @param day {Integer} index of the day. 0=sunday, 1=monday, ... * @param locale {String} optional locale to be used * @return {Boolean} whether the given day is a weekend day */ isWeekend : function(day, locale) { var weekendStart = qx.locale.Date.getWeekendStart(locale); var weekendEnd = qx.locale.Date.getWeekendEnd(locale); if (weekendEnd > weekendStart) { return ((day >= weekendStart) && (day <= weekendEnd)); } else { return ((day >= weekendStart) || (day <= weekendEnd)); } }, /** * Extract the territory part from a locale * * @param locale {String} the locale * @return {String} territory */ _getTerritory : function(locale) { if (locale) { var territory = locale.split("_")[1] || locale; } else { territory = this.__mgr.getTerritory() || this.__mgr.getLanguage(); } return territory.toUpperCase(); }, /** * Provide localization (CLDR) data with fallback between "format" and "stand-alone" contexts. * It is used in {@link #getDayName} and {@link #getMonthName} methods. * * @param context {String} intended context. * Possible values: "format", "stand-alone". * @param fallbackContext {String} the context used in case no localization is found for the key. * @param key {String} message id (may contain format strings) * @param locale {String} the locale * @return {String} localized name for the key * */ __localizeWithFallback : function(context, fallbackContext, key, locale) { var localizedString = this.__mgr.localize(key, [], locale); if(localizedString == key) { var newKey = key.replace('_' + context + '_', '_' + fallbackContext + '_'); return this.__mgr.localize(newKey, [], locale); } else { return localizedString; } } } });