sencha touch app bulid for GBK

来源:互联网 发布:秦岭村庄消失知乎 编辑:程序博客网 时间:2024/05/20 06:07

由于使用的项目编码是GBK,所以JS代码都是GBK编码,在使用sencha  app build production 打包生成的app.js默认是使用UTF-8编码生成,所以导致中文不能正常显示。


查看sencha.jar代码后发现如下:

public class CharsetDetector
{
  private static final Logger _logger = SenchaLogManager.getLogger();


  public static final Charset DefaultCharset = Charset.forName("UTF-8");


  private static final Pattern _directivePattern = RegexUtil.getInstance().get("//@charset\\s+?([^\\s]+)");


  public static String decodeBytes(byte[] data, int offset, int length)
  {
    String defaultEncoded;
    try
    {
      defaultEncoded = new String(data, offset, length, DefaultCharset);
    } catch (Exception e) {
      _logger.warn("Error loading byte array as {} : {}", DefaultCharset.name(), e.getMessage());


      defaultEncoded = new String(data, offset, length, Charset.forName("ASCII"));
    }


    Charset charset = detectCharsetFromTag(defaultEncoded);
    if (charset != null) {
      return new String(data, offset, length, charset);
    }
    return defaultEncoded;
  }


  public static String decodeBytes(byte[] data) {
    return decodeBytes(data, 0, data.length);
  }


  public static Charset detectCharsetFromTag(String defaultEncoded) {
    Matcher m = _directivePattern.matcher(defaultEncoded);
    if (m.find()) {
      String charsetName = m.group(1);


      if (charsetName.endsWith("-->")) {
        charsetName = charsetName.replace("-->", "");
      }


      if (_logger.isDebugEnabled()) {
        _logger.debug("Detected charset directive for charset : {}", charsetName);
      }


      if (Charset.availableCharsets().containsKey(charsetName)) {
        if (_logger.isDebugEnabled()) {
          _logger.debug("Detected charset {}", charsetName);
        }
        return Charset.forName(charsetName);
      }
      _logger.warn("Specified charset was not in available charsets : {}", charsetName);
    }


    return null;
  }
}


注意红色代码---:如果在js代码注释如下:

//@charset GBK

Ext.define('IMovie.view.Main', {
    extend: 'Ext.tab.Panel',
    xtype: 'main',
    requires: [
        'Ext.TitleBar',
        'Ext.Video'
    ],
    config: {
        tabBarPosition: 'bottom',


        items: [
            {
                title: '电影‘,
                iconCls: 'home',


                styleHtmlContent: true,
                scrollable: true,


                items: {
                    docked: 'top',
                    xtype: 'titlebar',
                    title: 'Welcome to Sencha Touch 2'
                },


                html: [
                    "You've just generated a new Sencha Touch 2 project. What you're looking at right now is the ",
                    "contents of <a target='_blank' href=\"app/view/Main.js\">app/view/Main.js</a> - edit that file ",
                    "and refresh to change what's rendered here."
                ].join("")
            },
            {
                title: '音乐',
                iconCls: 'action',


                items: [
                    {
                        docked: 'top',
                        xtype: 'titlebar',
                        title: 'Getting Started'
                    },
                    {
                        xtype: 'video',
                        url: 'http://av.vimeo.com/64284/137/87347327.mp4?token=1330978144_f9b698fea38cd408d52a2393240c896c',
                        posterUrl: 'http://b.vimeocdn.com/ts/261/062/261062119_640.jpg'
                    }
                ]
            }
        ]
    }
});

就可以按你指定的字符编码生成js,中文就可以正常显示。

0 0