如何找到本质上取决于月份的文本

问题描述 投票:0回答:1

在法语中,你说“Le mois d'avril”,你说“Le mois de septembre”。根据月份的不同,您可以使用“d'”或“de”。类似于英语中“a”和“an”的区别。

有没有办法在 Javascript 中自动获得正确的措辞,或者我需要硬编码吗?

我认为 Intl 或 Moment 可能会有所帮助,但到目前为止我无法在这些库中找到解决方案。

javascript date translation
1个回答
1
投票

没有太多要测试的。艾薇儿、奥特和八月。

const getMonth = d => {
  const month = d.toLocaleString('fr-fr', {month:'long'});
  return `${"ao".includes(month[0]) ? `d'` : `de `}${month}`
}

console.log(getMonth(new Date('2023-04-05')))
console.log(getMonth(new Date('2023-05-05')))
console.log(getMonth(new Date('2023-08-05')))
console.log(getMonth(new Date('2023-10-05')))

© www.soinside.com 2019 - 2024. All rights reserved.