Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return default locale if passed unrecognized locale identifier #753

Merged
merged 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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