Flex i18n问题(在onCreationComplete中选择Locale)



好的。我一直在网上转来转去,试图找到一种不用头疼就能实现国际化的方法;我终于找到了办法。在构建参数中包括区域设置,并执行-locale locale/{locale}操作,使它们成为资源管理器的属性文件(即:src文件夹中的locale/en_GB/lang.properties(

问题:我似乎无法在启动时设置用户的区域设置。我收到一些关于"LocaleID索引超出范围"的错误(这很奇怪,因为LocaleID是基于字符串的..?(

此部分工作正常:

<fx:Script> 
    <![CDATA[
        // Shorthand resource management.
        private function getLang(key:String):String
        { return resourceManager.getString(key, 'lang'); }
    ]]>
</fx:Script>

此部分不工作:

protected function creationCompleteHandler(event:FlexEvent):void
{
    var locale:LocaleID = new LocaleID("en_GB");
    trace(locale.getLanguage()); // en
    trace(locale.getRegion()); // GB
    trace(locale.name); // en-GB
    if (!empty(saveData.data.lang)) // empty checks if str == null or trim(str).length == 0
        locale = new LocaleID(saveData.data.lang);
    this.setStyle("locale", locale);
}

实际错误不是在我设置区域设置时抛出的,而是在UI中的对象试图获取它们的值时抛出的。完整的错误消息如下:

RangeError: Property locale value [object LocaleID] is out of range
    at flashx.textLayout.property::Property$/defaultErrorHandler()[C:Vellumbranchesv22.0devoutputopenSourcetextLayoutsrcflashxtextLayoutpropertyProperty.as:31]
    at flashx.textLayout.property::Property/setHelper()[C:Vellumbranchesv22.0devoutputopenSourcetextLayoutsrcflashxtextLayoutpropertyProperty.as:230]
    at flashx.textLayout.formats::TextLayoutFormat/setStyleByProperty()[C:Vellumbranchesv22.0devoutputopenSourcetextLayoutsrcflashxtextLayoutformatsTextLayoutFormat.as:628]
    at flashx.textLayout.formats::TextLayoutFormat/set locale()[C:Vellumbranchesv22.0devoutputopenSourcetextLayoutsrcflashxtextLayoutformatsTextLayoutFormat.as:1271]
    at spark.core::CSSTextLayoutFormat()[E:dev4.5.1frameworksprojectssparksrcsparkcoreCSSTextLayoutFormat.as:75]
    at spark.components::RichEditableText/updateStylesIfChanged()[E:dev4.5.1frameworksprojectssparksrcsparkcomponentsRichEditableText.as:3619]
    at spark.components::RichEditableText/commitProperties()[E:dev4.5.1frameworksprojectssparksrcsparkcomponentsRichEditableText.as:2491]
    at mx.core::UIComponent/validateProperties()[E:dev4.5.1frameworksprojectsframeworksrcmxcoreUIComponent.as:8209]
    at mx.managers::LayoutManager/validateProperties()[E:dev4.5.1frameworksprojectsframeworksrcmxmanagersLayoutManager.as:597]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:dev4.5.1frameworksprojectsframeworksrcmxmanagersLayoutManager.as:813]
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:dev4.5.1frameworksprojectsframeworksrcmxmanagersLayoutManager.as:1180]

有趣的事实:1.我的名字不是维勒姆(?(2.我的sdk不在E:dev4.5.1中。。。它在C:Program FilesAdobeAdobe Flash Builder 4.5.1sdks4.5.1中。干得好错误消息!使用原始生成路径和所有。。。

这个问题的实际"修复"很恶心,但它起了作用:

protected function creationCompleteHandler(event:FlexEvent):void
{
    var locale:String = LocaleID.DEFAULT.name.replace("-", "_"); // en_GB
    if (!empty(saveData.data.lang)) // empty checks if str == null or trim(str).length == 0
        locale = saveData.data.lang;
    resourceManager.localeChain = [locale, "en_GB"];
}

为什么?因为Flex文档(如此美妙的东西(似乎又一次过时了。实际上,在Flex4.5中使用this.setStyle("locale", locale)是错误的。相反,您会更新resourceManager的languageChain,将您首选的语言环境作为第一个首选项。

根据丑陋的命名行,这是从LocaleID中获得"en_GB"的唯一方法。要么就是字符串串联,这也很难看。遗憾的是,resourceManager没有像它应该的那样使用短划线作为下划线。

语言环境样式应该有一个字符串值;但是您正在使用LocaleID对象设置它。我很确定你想这样做:

this.setStyle("locale", "en_GB");

以下是有关LocaleID对象的一些详细信息。

最新更新