Sencha 项目多语言和多主题的 build 配置

Build Profiles

多样化 build

当一个 Sencha 应用需要多样化,比如多个主题的时候,可以利用 Build Profiles 配置不同的构建参数。实现方式是在 app.json 中添加如下的 builds 键,如下:

"builds": {
    "classic": {
        "theme": "ext-theme-classic"
    },
    "crisp": {
        "theme": "ext-theme-crisp"
    },
    "neptune": {
        "theme": "ext-theme-neptune"
    },
    "neptune-touch": {
        "theme": "ext-theme-neptune-touch"
    }
}

上面的 builds 包含了4 种 构建配置,每种用了不同的主题。

利用类似 sencha app build classicsencha app build neptune 的命令即可 build 出不同主题的结果。

如果依次执行所有 builds(比如上面的 classic、crisp、neptune、neptune-touch),结果却只有 builds 中配置的最后一个。这是因为默认情况下,build 输出的文件名是 app.js, app.json, app.jsonp,所以后一次 build 会将前一次的结果覆盖掉。如下图:
默认 build 输出
(提示:app.json 是 manifest 清单文件)

不同的 build 输出到不同的目录下

理想的结果是每个 build profile 输出到不同的目录下。

我们可以配置 app.json,使最后 build 结果如下图:
build 分不同的输出目录

如下修改 app.json

"bootstrap": {
    "base": "${app.dir}",
    "manifest": "${build.id}.json", // 增加该配置
    "microloader": "bootstrap.js",
    "css": "bootstrap.css"
},
.....
"output": {
    "base": "${workspace.build.dir}/${build.environment}/${app.name}",
    "manifest": "${build.id}.json", // 增加该配置
    "framework": "${build.id}/framework.js", // 增加该配置(如果开启了split.mode会有framework.js)
    "appCache": {
        "enable": false
    },
    "js": {
        "path": "${build.id}/app.js", // 增加该配置
        "filter": "all" //all/minimum
    }
},

如上配置之后,不同的 build 会输出不同名字的 manifest(清单)文件,app.js 会输出到不同的目录下。

locales 多语言

app.json 中有一个 locales 配置键。如果要支持英文和简体中文,可以如下配置:

"locales": [
    "en",
    "zh_CN"
]

当然,需要先 requires 引入多语言的包 locale

"requires": [
    "locale"
]

这样,执行 sencha app build [profile]的时候,会依次将 m 个 build profile 和 n 个 locale 交叉输出 m*n 个结果。比如上面的例子中,会生成 classic-en, classic-zh_CN, crisp-en, crisp-zh_CN, neptune-en, neptune-zh_CN, neptune-touch-en, neptune-touch-zh_CN 共 8 个结果。

打开 index.html 时加载不同的 profile 和 locale

浏览器访问 build 输出的 index.html,默认是加载 app.json 这个清单文件。但是 配置了 buildslocales 之后的输出结果中已经没有了 app.json 文件了,所以我们要改变 index.html 加载清单文件的逻辑。

修改项目下的 index.html (不是 build 后的)

<!DOCTYPE HTML>
<html manifest="">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="UTF-8">
    <title>Demo</title>
    <!-- 增加下面这段代码 -->
    <script>
        var getSearchParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        window.Ext = window.Ext || {};
        Ext.beforeLoad = function (tags) {
            var locale = getSearchParam('locale') || 'zh_CN'; // 如果没有 locale 参数,则默认加载 zh_CN
            var profile = getSearchParam('profile') || 'classic'; // 如果没有 profile 参数,则默认加载 classic
            Ext.manifest = profile + "-" + locale;
        };
    </script>

    <!-- The line below must be kept intact for Sencha Cmd to build your application -->
    <script id="microloader" data-app="2cb3beca-1639-4cd6-b904-2ed6ef0a86bc" type="text/javascript" src="bootstrap.js"></script>
</head>
<body>
</body>
</html>

最后,通过 http://.../index.html?profile=classic&locale=en 这种地址就可以访问不同的 profile 和 locale 了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘_博士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值