简体   繁体   English

如何使用RequireJS r.js构建更改i18n的路径

[英]How to change path for i18n with RequireJS r.js build

I'm using RequireJS for my javascript project, and r.js to build one single javascript file for production. 我将RequireJS用于我的JavaScript项目,并使用r.js构建一个用于生产的单个javascript文件。 This single file (main.js) is then uploaded to a CDN. 然后将这个单个文件(main.js)上传到CDN。 This all works very fine, but now I'm trying to add i18n support. 这一切都很好,但是现在我试图添加i18n支持。

The problem is that the location of the i18n file is relative to the main javascript file. 问题是i18n文件的位置是相对于主要javascript文件的。 So within a module, I would have: 因此,在一个模块中,我将拥有:

define(['i18n!nls/text'], function(Translation) { });

This all works very fine when I'm developing, but for production the problem is that the translation file is not relative to the main.js file as this is placed in a CDN. 在我进行开发时,这一切都很好,但是对于生产而言,问题在于转换文件与main.js文件无关,因为它位于CDN中。 I don't want to store the translation file in the CDN, so how do I change the reference to that file in the build process? 我不想将翻译文件存储在CDN中,那么如何在构建过程中更改对该文件的引用?

I found a solution to my problem. 我找到了解决问题的方法。 In the RequireJS config I've added: 在RequireJS配置中,我添加了:

requirejs.config({
    paths: {
        nls: "/js/nls"
    }
});

Because the path starts with a slash, RequireJS knows it's not relative. 因为路径以斜杠开头,所以RequireJS知道它不是相对的。 Now the problem I got was that the build would fail, as RequireJS would be looking for default language files in /js/nls. 现在我遇到的问题是构建会失败,因为RequireJS将在/ js / nls中寻找默认语言文件。 Therefore, I added a symlink from the root of my webserver to the nls directory on the machine. 因此,我添加了一个从我的Web服务器的根目录到计算机上nls目录的符号链接。

Had the same issue. 有同样的问题。

Use baseUrl config 使用baseUrl配置

require.config({
    baseUrl: '/path_for_not_included_modules/'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM