PrimeFaces中<p:schedule>组件自定义阿拉伯语本地化不生效咨询
问题原因
<p:schedule>与<p:calendar>的本地化逻辑不共通:
- <p:calendar>是PrimeFaces原生组件,直接读取
PrimeFaces.locales全局配置 - <p:schedule>底层封装了第三方FullCalendar库,本地化需要单独配置FullCalendar的语言规则,不会自动继承
PrimeFaces.locales的配置
解决方案
第一步:调整代码逻辑
将自定义本地化代码放在<p:schedule>组件之后,绑定到页面加载完成事件触发,避免组件初始化时配置未生效。
第二步:补充FullCalendar专属配置
在原有PrimeFaces通用locale配置基础上,新增FullCalendar的阿拉伯语规则,通过schedule的widgetVar主动绑定配置。
修改后的完整JS代码如下:
// 原有PrimeFaces通用locale配置(保留,适配<p:calendar>) PrimeFaces.locales['ar'] = { closeText: 'إغلق', prevText: 'إلى الخلف', nextText: 'إلى الأمام', currentText: 'بداية', monthNames: ['جانفي', 'فيفري', 'مارس', 'أفريل', 'ماي', 'جوان', 'جويلية', 'أوت', 'سبتمبر', 'أكتوبر', 'نوفمبر', 'ديسمبر' ], monthNamesShort: ['جانفي', 'فيفري', 'مارس', 'أفريل', 'ماي', 'جوان', 'جويلية', 'أوت', 'سبتمبر', 'أكتوبر', 'نوفمبر', 'ديسمبر' ], dayNames: ['يوم الأحد', 'يوم الإثنين', 'يوم الثلاثاء', 'يوم الأَرْبعاء', 'يوم الخَمِيس', 'يوم الجُمْعَة', 'يوم السَّبْت'], dayNamesShort: ['الأحد', 'الإثنين', 'الثلاثاء', 'الأَرْبعاء', 'الخَمِيس', 'الجُمْعَة', 'السَّبْت'], dayNamesMin: ['الأحد', 'الإثنين', 'الثلاثاء', 'الأَرْبعاء', 'الخَمِيس', 'الجُمْعَة', 'السَّبْت'], weekHeader: 'الأسبوع', firstDay: 1, isRTL: true, // 阿拉伯语为从右到左语言,修改为true可适配排版逻辑 showMonthAfterYear: false, yearSuffix:'' , timeOnlyTitle: 'الوقت فقط' , timeText: 'الوقت' , hourText: 'ساعة', minuteText: 'دقيقة', secondText: 'ثانية', ampm: false, month: 'الشهر', week: 'الأسبوع', day: 'اليوم', allDayText: 'سا ئراليوم' }; // 新增:<p:schedule>专属FullCalendar本地化配置 $(document).ready(function() { var schedule = PF('myschedule'); if (schedule) { // 注册阿拉伯语规则到FullCalendar $.fullCalendar.locales['ar'] = { monthNames: PrimeFaces.locales['ar'].monthNames, monthNamesShort: PrimeFaces.locales['ar'].monthNamesShort, dayNames: PrimeFaces.locales['ar'].dayNames, dayNamesShort: PrimeFaces.locales['ar'].dayNamesShort, dayNamesMin: PrimeFaces.locales['ar'].dayNamesMin, weekNumberTitle: PrimeFaces.locales['ar'].weekHeader, firstDay: PrimeFaces.locales['ar'].firstDay, isRTL: PrimeFaces.locales['ar'].isRTL, allDayText: PrimeFaces.locales['ar'].allDayText, buttonText: { today: 'اليوم', month: PrimeFaces.locales['ar'].month, week: PrimeFaces.locales['ar'].week, day: PrimeFaces.locales['ar'].day } }; // 绑定配置并重新渲染组件 schedule.cfg.locale = 'ar'; schedule.update(); } });
版本兼容说明
如果使用PrimeFaces 10及以上版本,底层FullCalendar升级为v5版本,配置调用API略有调整,将上述代码中schedule更新部分替换为以下逻辑即可:
// PrimeFaces 10+ 适配代码 schedule.getCalendar().setOption('locale', 'ar'); schedule.getCalendar().render();
内容的提问来源于stack exchange,提问作者Haythem Nasri
相关产品推荐
相关产品推荐

