Skip to content

Commit

Permalink
Return default locale if passed unrecognized locale identifier (#753)
Browse files Browse the repository at this point in the history
* Return default locale if passed unrecognized locale identifier

* Better messaging for locale fallback.
  • Loading branch information
adrianmroz-allegro authored May 12, 2021
1 parent 99765bf commit 2ddcce3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/common/models/locale/locale.mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe("locale", () => {
expect(locale).to.deep.equal(en_us);
});

it("should return default locale if passed unrecognized base identifier", () => {
const locale = fromConfig({ base: "foobar" } as any);

expect(locale).to.deep.equal(en_us);
});

it("should use base locale and override desired fields", () => {
const locale = fromConfig({ base: "en-US", overrides: { weekStart: 42 } });

Expand Down
7 changes: 6 additions & 1 deletion src/common/models/locale/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { isObject } from "../../utils/general/general";
import { LOGGER } from "../../logger/logger";
import { isObject, isTruthy } from "../../utils/general/general";

const enUS: Locale = {
shortDays: ["S", "M", "T", "W", "T", "F", "S"],
Expand Down Expand Up @@ -48,6 +49,10 @@ export interface Locale {
export function fromConfig(locale?: LocaleJS): Locale {
if (!isObject(locale)) return DEFAULT_LOCALE;
const { base, overrides } = locale;
if (!isTruthy(LOCALES[base])) {
LOGGER.warn(`Unsupported locale identifier: ${base}. Fallback to en-US.`);
return DEFAULT_LOCALE;
}
return {
...LOCALES[base],
...overrides
Expand Down

0 comments on commit 2ddcce3

Please sign in to comment.