forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocalizationUtils.js
More file actions
1 lines (1 loc) · 2.04 KB
/
LocalizationUtils.js
File metadata and controls
1 lines (1 loc) · 2.04 KB
1
define(function(require,exports,module){const Strings=require("strings");function getLocalizedLabel(locale){var key="LOCALE_"+locale.toUpperCase().replace("-","_"),i18n=Strings[key];return void 0===i18n?locale:i18n}const DATE_TIME_STYLE={FULL:"full",LONG:"long",MEDIUM:"medium",SHORT:"short"};function getFormattedDateTime(date,lang,dateTimeFormat){return date||(date=new Date),dateTimeFormat||(dateTimeFormat={dateStyle:DATE_TIME_STYLE.MEDIUM,timeStyle:DATE_TIME_STYLE.SHORT}),Intl.DateTimeFormat([lang||brackets.getLocale()||"en","en"],dateTimeFormat).format(date)}function dateTimeFromNow(date,lang,fromDate){date=date||new Date,fromDate=fromDate||new Date;const diffInSeconds=Math.floor((date-fromDate)/1e3),rtf=new Intl.RelativeTimeFormat([lang||brackets.getLocale()||"en","en"],{numeric:"auto"});return Math.abs(diffInSeconds)<3?rtf.format(0,"second"):Math.abs(diffInSeconds)<60?rtf.format(diffInSeconds,"second"):Math.abs(diffInSeconds)<3600?rtf.format(Math.trunc(diffInSeconds/60),"minute"):Math.abs(diffInSeconds)<86400?rtf.format(Math.trunc(diffInSeconds/3600),"hour"):Math.abs(diffInSeconds)<2592e3?rtf.format(Math.trunc(diffInSeconds/86400),"day"):Math.abs(diffInSeconds)<31536e3?rtf.format(Math.trunc(diffInSeconds/2592e3),"month"):rtf.format(Math.trunc(diffInSeconds/31536e3),"year")}function dateTimeFromNowFriendly(date,lang,fromDate){const diffInMilliseconds=date-(fromDate=fromDate||new Date),diffInDays=Math.trunc(diffInMilliseconds/864e5);if(Math.abs(diffInDays)<=30)return dateTimeFromNow(date,lang);const currentYear=fromDate.getFullYear(),dateYear=date.getFullYear(),languageOption=[lang||brackets.getLocale()||"en","en"];return currentYear===dateYear?new Intl.DateTimeFormat(languageOption,{month:"short",day:"numeric"}).format(date):new Intl.DateTimeFormat(languageOption,{month:"short",day:"numeric",year:"numeric"}).format(date)}exports.getLocalizedLabel=getLocalizedLabel,exports.getFormattedDateTime=getFormattedDateTime,exports.dateTimeFromNow=dateTimeFromNow,exports.dateTimeFromNowFriendly=dateTimeFromNowFriendly,exports.DATE_TIME_STYLE=DATE_TIME_STYLE});