Skip to content

fix locale import for esm #664

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 8 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BasicDatePicker, RangeDatePicker } from './pickers';
import { Locale, SemanticDatepickerProps } from './types';
import Calendar from './components/calendar';
import Input from './components/input';
import localeOptions from "./locales";
Copy link
Owner

@arthurdenner arthurdenner Oct 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong but this would bundle all of the locales into the project, and we don't want to do that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct that it does includes all locales. This is quickest (but not ideal) solution to make this work without needing to refactor a lot of code to accommodate dynamic import. Please feel free to close the PR and go with one of the options @wollo suggested in the issue.


const style: React.CSSProperties = {
display: 'inline-block',
Expand Down Expand Up @@ -166,17 +167,15 @@ class SemanticDatepicker extends React.Component<

get locale() {
const { locale } = this.props;

let localeJson: Locale;

try {
localeJson = require(`./locales/${locale}.json`);
} catch (e) {

const localeKey = locale.replace("-", "_");

if (!localeOptions.hasOwnProperty(localeKey)) {
console.warn(`"${locale}" is not a valid locale`);
localeJson = require('./locales/en-US.json');
return localeOptions["en_US"];
}

return localeJson;
return localeOptions[localeKey];
}

get weekdays() {
Expand Down
45 changes: 45 additions & 0 deletions src/locales/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import bg_BG from "./bg-BG";
import ca_ES from "./ca-ES";
import cs_CZ from "./cs-CZ";
import de_DE from "./de-DE";
import en_US from "./en-US";
import es_ES from "./es-ES";
import et_EE from "./et-EE";
import fi_FI from "./fi-FI";
import fr_FR from "./fr-FR";
import he_IL from "./he-IL";
import it_IT from './it-IT';
import ja_JP from "./ja-JP";
import ko_KR from "./ko-KR";
import nb_NO from "./nb-NO";
import nn_NO from "./nn-NO";
import pl_PL from "./pl-PL";
import pt_BR from "./pt-BR";
import ru_RU from "./ru-RU";
import sv_SE from "./sv-SE";
import tr_TR from "./tr-TR";
import zh_CN from "./zh-CN";

export default {
bg_BG,
ca_ES,
cs_CZ,
de_DE,
en_US,
es_ES,
et_EE,
fi_FI,
fr_FR,
he_IL,
it_IT,
ja_JP,
ko_KR,
nb_NO,
nn_NO,
pl_PL,
pt_BR,
ru_RU,
sv_SE,
tr_TR,
zh_CN,
};