diff --git a/exports/api.d.ts b/exports/api.d.ts index cec0fa8876..a6c3d0c1ff 100644 --- a/exports/api.d.ts +++ b/exports/api.d.ts @@ -1,3 +1,7 @@ +import PrimeReact from './components/api/PrimeReact'; + +export default PrimeReact; +export * from './components/api/Locale'; export * from './components/menuitem/MenuItem'; export * from './components/selectitem/SelectItem'; -export * from './components/treenode/TreeNode'; \ No newline at end of file +export * from './components/treenode/TreeNode'; diff --git a/exports/api.js b/exports/api.js new file mode 100644 index 0000000000..bd0d863a76 --- /dev/null +++ b/exports/api.js @@ -0,0 +1,4 @@ +'use strict'; + +module.exports = require('./components/api/PrimeReact'); +module.exports = require('./components/api/Locale'); diff --git a/exports/utils.d.ts b/exports/utils.d.ts deleted file mode 100644 index 5de547fa57..0000000000 --- a/exports/utils.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import PrimeReact from './components/utils/PrimeReact'; -export default PrimeReact; diff --git a/exports/utils.js b/exports/utils.js deleted file mode 100644 index ec61a644d2..0000000000 --- a/exports/utils.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -module.exports = require('./components/utils/PrimeReact'); \ No newline at end of file diff --git a/src/App.js b/src/App.js index b8f9cc642e..10377596a5 100644 --- a/src/App.js +++ b/src/App.js @@ -21,7 +21,7 @@ import AppConfig from './AppConfig'; import AppContentContext from './AppContentContext'; import { Toast } from './components/toast/Toast'; -import PrimeReact from './components/utils/PrimeReact'; +import PrimeReact from './components/api/PrimeReact'; import { AppChangelogDialog } from './AppChangelogDialog'; export class App extends Component { diff --git a/src/components/api/Locale.js b/src/components/api/Locale.js new file mode 100644 index 0000000000..6ef832b5a8 --- /dev/null +++ b/src/components/api/Locale.js @@ -0,0 +1,65 @@ +import PrimeReact from './PrimeReact'; + +let locales = { + 'en': { + accept: 'Yes', + reject: 'No', + choose: 'Choose', + upload: 'Upload', + cancel: 'Cancel', + dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + dayNamesMin: ['Su','Mo','Tu','We','Th','Fr','Sa'], + monthNames: ['January','February','March','April','May','June','July','August','September','October','November','December'], + monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + today: 'Today', + clear: 'Clear', + weekHeader: 'Wk', + firstDayOfWeek: 0, + dateFormat: 'mm/dd/yy', + weak: 'Weak', + medium: 'Medium', + strong: 'Strong', + passwordPrompt: 'Enter a password' + } +}; + +function locale(locale) { + if (locale) { + PrimeReact.locale = locale; + } + + return { + locale: PrimeReact.locale, + options: locales[PrimeReact.locale] + } +} + +function addLocale(locale, options) { + locales[locale] = { ...locales['en'], ...options }; +} + +function updateLocaleOption(key, value, locale) { + localeOptions(locale)[key] = value; +} + +function updateLocaleOptions(options, locale) { + const _locale = locale || PrimeReact.locale; + locales[_locale] = { ...locales[_locale], ...options }; +} + +function localeOption(key, locale) { + try { + return localeOptions(locale)[key]; + } + catch(error) { + throw new Error(`The ${key} option is not found in the current locale('${locale || PrimeReact.locale}').`); + } +} + +function localeOptions(locale) { + const _locale = locale || PrimeReact.locale; + return locales[_locale]; +} + +export { locale, addLocale, updateLocaleOption, updateLocaleOptions, localeOption, localeOptions }; diff --git a/src/components/utils/PrimeReact.js b/src/components/api/PrimeReact.js similarity index 70% rename from src/components/utils/PrimeReact.js rename to src/components/api/PrimeReact.js index f8d4783b63..13656dd0f1 100644 --- a/src/components/utils/PrimeReact.js +++ b/src/components/api/PrimeReact.js @@ -1,4 +1,6 @@ export default class PrimeReact { static ripple = false; + + static locale = 'en'; } diff --git a/src/components/calendar/Calendar.js b/src/components/calendar/Calendar.js index 77b0129ae0..29af5c98de 100644 --- a/src/components/calendar/Calendar.js +++ b/src/components/calendar/Calendar.js @@ -11,6 +11,7 @@ import { CSSTransition } from 'react-transition-group'; import { Ripple } from '../ripple/Ripple'; import UniqueComponentId from '../utils/UniqueComponentId'; import ConnectedOverlayScrollHandler from '../utils/ConnectedOverlayScrollHandler'; +import { localeOption, localeOptions } from '../api/Locale'; export class Calendar extends Component { @@ -50,17 +51,7 @@ export class Calendar extends Component { shortYearCutoff: '+10', hideOnDateTimeSelect: false, showWeek: false, - locale: { - firstDayOfWeek: 0, - dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], - dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], - dayNamesMin: ["Su","Mo","Tu","We","Th","Fr","Sa"], - monthNames: [ "January","February","March","April","May","June","July","August","September","October","November","December" ], - monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ], - today: 'Today', - clear: 'Clear', - weekHeader: 'Wk' - }, + locale: null, dateFormat: 'mm/dd/yy', panelStyle: null, panelClassName: null, @@ -131,7 +122,7 @@ export class Calendar extends Component { shortYearCutoff: PropTypes.string, hideOnDateTimeSelect: PropTypes.bool, showWeek: PropTypes.bool, - locale: PropTypes.object, + locale: PropTypes.string, dateFormat: PropTypes.string, panelStyle: PropTypes.object, panelClassName: PropTypes.string, @@ -1668,14 +1659,15 @@ export class Calendar extends Component { } getSundayIndex() { - return this.props.locale.firstDayOfWeek > 0 ? 7 - this.props.locale.firstDayOfWeek : 0; + const firstDayOfWeek = localeOption('firstDayOfWeek', this.props.locale); + return firstDayOfWeek > 0 ? 7 - firstDayOfWeek : 0; } createWeekDays() { let weekDays = []; - let dayIndex = this.props.locale.firstDayOfWeek; + let { firstDayOfWeek: dayIndex, dayNamesMin } = localeOptions(this.props.locale); for(let i = 0; i < 7; i++) { - weekDays.push(this.props.locale.dayNamesMin[dayIndex]); + weekDays.push(dayNamesMin[dayIndex]); dayIndex = (dayIndex === 6) ? 0 : ++dayIndex; } @@ -2052,6 +2044,7 @@ export class Calendar extends Component { }; let output = ''; let literal = false; + const { dayNamesShort, dayNames, monthNamesShort, monthNames } = localeOptions(this.props.locale); if (date) { for (iFormat = 0; iFormat < format.length; iFormat++) { @@ -2067,7 +2060,7 @@ export class Calendar extends Component { output += formatNumber('d', date.getDate(), 2); break; case 'D': - output += formatName('D', date.getDay(), this.props.locale.dayNamesShort, this.props.locale.dayNames); + output += formatName('D', date.getDay(), dayNamesShort, dayNames); break; case 'o': output += formatNumber('o', @@ -2079,7 +2072,7 @@ export class Calendar extends Component { output += formatNumber('m', date.getMonth() + 1, 2); break; case 'M': - output += formatName('M',date.getMonth(), this.props.locale.monthNamesShort, this.props.locale.monthNames); + output += formatName('M',date.getMonth(), monthNamesShort, monthNames); break; case 'y': output += lookAhead('y') ? date.getFullYear() : (date.getFullYear() % 100 < 10 ? '0' : '') + (date.getFullYear() % 100); @@ -2317,6 +2310,8 @@ export class Calendar extends Component { day = 1; } + const { dayNamesShort, dayNames, monthNamesShort, monthNames } = localeOptions(this.props.locale); + for (iFormat = 0; iFormat < format.length; iFormat++) { if(literal) { if(format.charAt(iFormat) === "'" && !lookAhead("'")) { @@ -2330,7 +2325,7 @@ export class Calendar extends Component { day = getNumber("d"); break; case "D": - getName("D", this.props.locale.dayNamesShort, this.props.locale.dayNames); + getName("D", dayNamesShort, dayNames); break; case "o": doy = getNumber("o"); @@ -2339,7 +2334,7 @@ export class Calendar extends Component { month = getNumber("m"); break; case "M": - month = getName("M", this.props.locale.monthNamesShort, this.props.locale.monthNames); + month = getName("M", monthNamesShort, monthNames); break; case "y": year = getNumber("y"); @@ -2433,14 +2428,16 @@ export class Calendar extends Component { } renderTitleMonthElement(month) { + const monthNames = localeOption('monthNames', this.props.locale); + if (this.props.monthNavigator && this.props.view !== 'month') { - let viewDate = this.getViewDate(); - let viewMonth = viewDate.getMonth(); + const viewDate = this.getViewDate(); + const viewMonth = viewDate.getMonth(); return ( this.fileInput = el} type="file" onChange={this.onFileSelect} multiple={this.props.multiple} accept={this.props.accept} disabled={this.chooseDisabled()} /> - {this.props.chooseLabel} + {this.chooseButtonLabel()} ); @@ -441,8 +454,8 @@ export class FileUpload extends Component { const emptyContent = this.renderEmptyContent(); if (!this.props.auto) { - uploadButton =