Skip to content

Commit

Permalink
Fix #11569: Core JS setGlobalLocaleValue (#11604)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Mar 11, 2024
1 parent 0c56ded commit 91f8810
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/14_0_0/core/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ if (window.PrimeFaces) {

```

### Overwrite Global Value

In certain situations, you might find it necessary to unify all locales to a single value. For instance, if you wish to establish `firstDayOfWeek=1` uniformly across all locales.
The function `setGlobalLocaleValue` is available for precisely this purpose, ensuring consistency by applying the same value across all locales. Example usage:

```js
PrimeFaces.setGlobalLocaleValue('firstDayOfWeek', 1);
```

### Usage

To add another locale to the API, first create the locale object first with settings and assign it as a
Expand Down
1 change: 1 addition & 0 deletions docs/14_0_0/gettingstarted/whatsnew.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Look into [migration guide](https://primefaces.github.io/primefaces/14_0_0/#/../
* `MOVE_SCRIPTS_TO_BOTTOM` adds new option `defer` to defer loading scripts.
* OS settings for `prefers-reduced-motion: reduce` is now respected and PF disables all animations.
* Minimum inline Ajax load animation duration via `PrimeFaces.ajax.minLoadAnimation`.
* Client side `PrimeFaces.setGlobalLocaleValue(xxx);` if you need to set the same value in all locales.

* Chart
* A new more flexible version of Chart.js can be used `<p:chart>` allowing raw JSON or [XDEV Chart.js Java Model](https://github.com/xdev-software/chartjs-java-model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,35 @@
var locale = this.getLocaleSettings();
return (locale&&locale[key]) ? locale[key] : PrimeFaces.locales['en_US'][key];
},

/**
* Loop over all locales and set the label to the new value in all locales.
* @param {string} localeKey The locale key
* @param {string} localeValue The locale value
*/
setGlobalLocaleValue: function(localeKey, localeValue) {
// Recursive function to iterate over nested objects
function iterateLocale(locale, lkey, lvalue) {
for (var key in locale) {
if (typeof locale[key] === 'object') {
// If the value is an object, call the function recursively
iterateLocale(locale[key], lkey, lvalue);
} else {
// Otherwise, set the new value if found
if (key === lkey) {
locale[key] = lvalue;
}
}
}
}

// iterate over all locales and try and set the key in each locale
for (var lang in PrimeFaces.locales) {
if (typeof PrimeFaces.locales[lang] === 'object') {
iterateLocale(PrimeFaces.locales[lang], localeKey, localeValue)
}
}
},

/**
* For 4.0 jQuery deprecated $.trim in favor of PrimeFaces.trim however that does not handle
Expand Down

0 comments on commit 91f8810

Please sign in to comment.