← 中文资源

简体中文版本

Intl.Locale 读取周历习惯与文本书写方向

通过 Intl.Locale 的 getWeekInfo、getTextInfo 与 getCalendars 直接获取各区域的每周起始日、周末范围及 rtl/ltr 书写方向。

阅读英文原文

运行结果

以下输出由构建管线在沙箱中真实运行该示例捕获。

en-US              dir: ltr | firstDay: 7 | weekend: 6,7 | cal: gregory
de-DE              dir: ltr | firstDay: 1 | weekend: 6,7 | cal: gregory
ar-EG              dir: rtl | firstDay: 6 | weekend: 5,6 | cal: gregory
ja-JP-u-ca-japanese dir: ltr | firstDay: 7 | weekend: 6,7 | cal: japanese

实现说明

迭代发生在 for (const tag of tags) 内部——循环之外的语句只执行一遍。 各个操作都建立在顶层绑定之上:tags。

使用要点

  • 零拉取示例:每个标识符都在语言发行版内部解析。

常见问题

这段代码会引入外部包吗?

不涉及外部包,每个调用都落在内建 API 上。 文件用顶层绑定保存自己的状态:tags。

核心语句

const locale = new Intl.Locale(tag);

代码示例

const tags = ['en-US', 'de-DE', 'ar-EG', 'ja-JP-u-ca-japanese'];

for (const tag of tags) {
  const locale = new Intl.Locale(tag);
  const week = locale.getWeekInfo();
  const text = locale.getTextInfo();
  const calendars = locale.getCalendars();
  console.log(
    tag.padEnd(18),
    'dir:', text.direction,
    '| firstDay:', week.firstDay,
    '| weekend:', week.weekend.join(','),
    '| cal:', calendars[0],
  );
}