Zend_Date / Zend_Locale:更改特定语言环境的日期格式?

发布于 2024-10-10 03:17:07 字数 424 浏览 12 评论 0原文

是否可以指示 Zend_Locale 它应该使用特定的语言环境,除了对日期格式进行微小的更改之外?我不想将 Zend_Date::toString() 与特定格式一起使用,因为这将在所有语言环境中使用此格式。

有问题的案例:我有根据用户的区域设置格式化的日期。我的荷兰同胞 (nl_NL) 用户要求使用 dd-mm-yyyy 格式的日期,而不是 Zend_Locale 强烈声称是我们的短日期格式的 dd-mm-yy。如果我更改输出日期的代码以显式使用自定义格式,则这适用于所有客户而不仅仅是奶酪头。我可以检查用户的区域设置,但如果需要创建更多例外,每次回显日期时我都需要添加这些检查和例外,这让我感到畏缩。

我无法直接更改 Zend_Locale XML 数据(也不想),因为 ZF 库被多个站点使用。

我确信这是那些“非常简单”的问题之一......一旦你知道如何做。有什么想法吗?

Is it possible to instruct Zend_Locale that it should use a certain locale, except for a minor alteration to the date format? I don't want to use the Zend_Date::toString() with the specific formatting, because that would use this format on all locales.

Case in question: I have dates formatted according to a user's locale setting. My fellow Dutch (nl_NL) users are asking for dd-mm-yyyy formatted dates, instead of dd-mm-yy which Zend_Locale vehemently claims to be our short date format. If I change the code where the date is outputted to explicitly use a custom format, that applies to all customers instead of just the cheeseheads. I could check the user's locale, but if more exceptions need to be created, every time a date is echo'd I'd need to add these checks and exceptions, the prospect of which makes me cringe.

I can't alter the Zend_Locale XML data directly (and don't want to), as the ZF library is used by various sites.

I'm sure this is one of those "really simple" issues... once you know how. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从此见与不见 2024-10-17 03:17:08

[尝试一个新的答案,因为这种方法与以前的答案有本质上的不同。]

我假设您将在视图中调用 Zend_Date::toString()

因此,也许您可​​以在配置文件中存储格式覆盖列表。在 Bootstrap 期间,检查加载的区域设置是否需要覆盖,然后将格式存储在视图中。然后,每当您输出日期时,请使用该格式。

像这样的东西:

application/configs/application.ini 中:

dateFormat.nl_NL = "d-m-Y"

application/Bootstrap.php 中:

protected function _initDateFormat()
{
    // Bootstrap and grab the view
    $this->bootstrap('view');
    $view = $this->getResource('view');

    // grab the date format overrides from options or null for locale-default
    $options = $this->getOptions();
    $this->bootstrap('locale');
    $locale = $this->getResource('locale');
    $dateFormat = isset($options['dateFormat'][$locale]) 
        ? $options['dateFormat'][$locale] 
        : null;

    // stash the dateFormat into the view
    $view->dateFormat = $dateFormat;
}

最后在视图脚本中,其中 $date 是一个 Zend_Date 对象:

<p>The date is <?= $date->toString($this->dateFormat) ?>.</p>

如果 $view->dateFormat 为 null,则将使用当前语言环境的格式。否则,您的覆盖将适用。

[Trying a new answer since this approach is substantively different than the previous answer.]

I assume that you would be calling Zend_Date::toString() in a view.

So, maybe you could store a list of format overrides in a config file. During Bootstrap, check if the loaded locale requires an override and then store the format in the view. Then whenever you output a date, use that format.

Something like this:

In application/configs/application.ini:

dateFormat.nl_NL = "d-m-Y"

In application/Bootstrap.php:

protected function _initDateFormat()
{
    // Bootstrap and grab the view
    $this->bootstrap('view');
    $view = $this->getResource('view');

    // grab the date format overrides from options or null for locale-default
    $options = $this->getOptions();
    $this->bootstrap('locale');
    $locale = $this->getResource('locale');
    $dateFormat = isset($options['dateFormat'][$locale]) 
        ? $options['dateFormat'][$locale] 
        : null;

    // stash the dateFormat into the view
    $view->dateFormat = $dateFormat;
}

Finally in a view-script, where $date is a Zend_Date object:

<p>The date is <?= $date->toString($this->dateFormat) ?>.</p>

If $view->dateFormat is null, then the format for the current locale will be used. Otherwise, your override will apply.

<逆流佳人身旁 2024-10-17 03:17:08

必须有某种方法可以:

  1. 扩展 Zend_Locale
  2. 创建一个与您想要自定义其行为的现有语言环境相对应的新语言环境。
  3. 为包含自定义内容的新区域设置创建新的 XML 文件。

然后,在 Bootstrap 中创建新的区域设置对象。如果当前区域设置与您要自定义的区域设置相匹配,则手动切换到新区域设置。

关键可能是在哪里放置新的 XML 文件以及如何定义映射。当然,您可能不想在 Zend 文件夹中放置任何新内容,那么我们如何告诉 Zend_Locale 和他的朋友使用新位置呢?该映射信息是否“硬编码”在各个 Zend_Locale_XXX 类中?

无论如何,只是谈谈一些想法。希望它能给您一些方向或启发比我更有知识的其他人 - 确实,一个相当低的门槛! - 提出一些更明确和权威的东西。

干杯!

There must be some way to:

  1. Extend Zend_Locale
  2. Create a new locale corresponding to the existing locale whose behavior you want to customize.
  3. Create a new XML file for the new locale containing the customizations.

Then, in Bootstrap, create the new locale object. If the current locale matches the locale you wish to customize, then manually switch to the new locale.

The key is probably where to place the new XML files and how to define the mapping. Of course, you probably don't want to place anything new in the Zend folders, so how do we tell Zend_Locale and his friends to use the new location? Is that mapping information "hard-coded" in the various Zend_Locale_XXX classes?

Anyway, just talking out some ideas. Hope it gives you some direction or inspires someone else more knowledgeable than me - a rather low bar, indeed! - to come up with something more definitive and authoritative.

Cheers!

甜柠檬 2024-10-17 03:17:08

我也开始考虑 David Weinraub 的第二个解决方案。

以下是我对任何区域设置切换到 4 位数字年份的风格:

Bootstrap 类(application/Bootstrap.php 文件)中

protected function _initDate() {
    /** Test and correct Zend_Date::DATE_SHORT format **/
    $localeDataDateFormats = Zend_Locale_Data::getList(Zend_Locale::findLocale(), 'date'); // Date formats for auto Locale (@see Zend_Locale::findLocale())
    define(
        'DATE_SHORT_LONGYEAR',  // Define our new format, should now use it instead of Zend_Date::DATE_SHORT
        preg_replace('@((' . Zend_Date::YEAR . ')+|(' . Zend_Date::YEAR_8601 . ')+)@', '$2$3', $localeDataDateFormats['short']) // Makes years displayed with every digits available
    );
    /** /Test and correct Zend_Date::DATE_SHORT format **/
}

使用它(使用新常量而不是 <代码>Zend_Date::DATE_SHORT):

$date = new Zend_Date();
echo $date->toString(DATE_SHORT_LONGYEAR);

I was also commencing to consider David Weinraub's second solution.

Here is my flavour for switching to 4-digits years for any Locale:

In Bootstrap class (application/Bootstrap.php file)

protected function _initDate() {
    /** Test and correct Zend_Date::DATE_SHORT format **/
    $localeDataDateFormats = Zend_Locale_Data::getList(Zend_Locale::findLocale(), 'date'); // Date formats for auto Locale (@see Zend_Locale::findLocale())
    define(
        'DATE_SHORT_LONGYEAR',  // Define our new format, should now use it instead of Zend_Date::DATE_SHORT
        preg_replace('@((' . Zend_Date::YEAR . ')+|(' . Zend_Date::YEAR_8601 . ')+)@', '$2$3', $localeDataDateFormats['short']) // Makes years displayed with every digits available
    );
    /** /Test and correct Zend_Date::DATE_SHORT format **/
}

Use it (use the new constant instead of Zend_Date::DATE_SHORT):

$date = new Zend_Date();
echo $date->toString(DATE_SHORT_LONGYEAR);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文