英文:
how to get locale dynamically from dayjs https://www.npmjs.com/package/dayjs?
问题
当我使用像dayjs(date).locale('te').format('YYYY MMM DD')
这样的代码时,我得到的月份值是英文的。为了使用区域设置,我必须导入区域设置。
import * as locale from 'dayjs/locale/te';
问题是,我看不到一种动态导入区域设置的方法。我没有访问Node.js的require()
函数。我有一个基于React的应用程序。如何解决这个问题?
英文:
When I use like dayjs(date).locale('te').format('YYYY MMM DD')
, then I get Month value in english. For working with locale, I have to import locale.
import * as locale from 'dayjs/locale/te';
The problem is that I don't see a way to dynamically import a locale. I don't have access to nodejs require() function. I have react based application. How to mitigate this issue?
答案1
得分: 7
A workaround for dynamic Imports without errors:
const locales = {
de: () => import('dayjs/locale/de'),
en: () => import('dayjs/locale/en'),
}
function loadLocale (language) {
locales[language]().then(() => dayjs.locale(language))
}
英文:
A workaround for dynamic Imports without errors:
const locales = {
de: () => import('dayjs/locale/de'),
en: () => import('dayjs/locale/en'),
}
function loadLocale (language) {
locales[language]().then(() => dayjs.locale(language))
}
答案2
得分: 6
你可以使用import
表达式来动态导入任何你想要的内容。
例如:
import dayjs from "dayjs";
import localeData from "dayjs/plugin/localeData";
dayjs.extend(localeData);
const LOCALE = "de";
import(`dayjs/locale/${LOCALE}`)
.then(() => {
dayjs.locale(LOCALE);
console.log(dayjs.weekdays());
});
你可以在这个code sandbox链接中看到一个可运行的示例。
要了解更多关于动态导入的信息,建议阅读https://javascript.info/modules-dynamic-imports
英文:
You can use import as import expression and dynamically import whatever you want.
for example:
import dayjs from "dayjs";
import localeData from "dayjs/plugin/localeData";
dayjs.extend(localeData);
const LOCALE = "de";
import(`dayjs/locale/${LOCALE}`)
.then(() => {
dayjs.locale(LOCALE);
console.log(dayjs.weekdays());
});
You can see a working example in this codesandbox.
for more information on dynamic imports I suggest you to read https://javascript.info/modules-dynamic-imports
答案3
得分: 2
你需要首先按照以下方式导入所需的区域文件:
import 'dayjs/locale/te'
import 'dayjs/locale/en'
然后你可以按照以下方式动态切换区域设置:
dayjs().locale('en').format()
dayjs('2018-4-28', { locale: 'te' })
英文:
You need to first import needed locale files as below
import 'dayjs/locale/te'
import 'dayjs/locale/en'
Then you can switch between locals dynamically as below operations
dayjs().locale('en').format()
dayjs('2018-4-28', { locale: 'te' })
答案4
得分: 0
你可以这样做,如果你不介意添加一个 Promise,就可以动态导入你需要的语言环境。代码会像这样:
const convertToLocale = (date, locale) => {
return import(`dayjs/locale/${locale}`)
.then(() => dayjs(date).locale(locale));
}
这将加载所有语言环境文件,就像你提到的导入语句一样,而无需手动添加导入语句。
英文:
One thing you could do if you don't mind adding a promise there is to dynamically import the locale you need. The code would look like this:
const convertToLocale = (date, locale) => {
return import(`dayjs/locale/${locale}`)
.then(() => dayjs(date).locale(locale));
}
This will load all locale files as the import you mentioned would without the need to add the import statements by hand.
答案5
得分: 0
尝试这段代码。
const language = {
en: import('dayjs/locale/en'),
te: import('dayjs/locale/te')
}
language['te'].then(lng => { console.log((dayjs(new Date()).locale(lng.name).format('YYYY MMMM DD')))})
英文:
Try this code.
const language = {
en: import('dayjs/locale/en'),
te: import('dayjs/locale/te')
}
language['te'].then(lng => { console.log((dayjs(new Date()).locale(lng.name).format('YYYY MMMM DD')))})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论