Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language files (i18n/*.js) in dist are not compatible with AMD #4494

Closed
evil-shrike opened this issue Jul 21, 2016 · 3 comments
Closed

Language files (i18n/*.js) in dist are not compatible with AMD #4494

evil-shrike opened this issue Jul 21, 2016 · 3 comments

Comments

@evil-shrike
Copy link

select2.js can be loaded as AMD-script, it returns Select2 class which can be instantiated.
But language files doesn't support AMD, they expect select2.amd is already injected into jQuery.fn:

(function(){
(function() {
    if (jQuery && jQuery.fn && jQuery.fn.select2 && jQuery.fn.select2.amd)
        var e = jQuery.fn.select2.amd;
    return e.define("select2/i18n/ru", [], function() {  /* skipped */   }),
    {
        define: e.define,
        require: e.require
    }
})();

BTW the code is not correct - why is this if needed if it fails on the next line (where e is undefined)?

Anyway, let's consider we have an AMD module which loads select2 and a lang file:

define(["jquery", "vendor/select2/select2", "vendor/select2/i18n/ru"], 
  function ($, Select2) {

  var sel = new Select2($(..), { language: "ru"} );
}

It's not guaranteed that select2.js loads before i18n/ru.js because the lang file has no dependency. So we have to define shim in RequireJS's config (for each language!):

    "shim": {
        "vendor/select2/i18n/ru": [ "vendor/select2/select2" ],
        }

which is tedious.

Language files do not need select2 itself actually. They just need an AMD loader (jQuery.fn.select2.amd), so why not just use UMD pattern?

And why not use i18n plugin from RequireJS?

@mirari
Copy link

mirari commented Aug 20, 2016

Same problem , and I have to modify the source file to this:

define(["jquery","select2"], function (jQuery) {
    if (jQuery && jQuery.fn && jQuery.fn.select2 && jQuery.fn.select2.amd)var e = jQuery.fn.select2.amd;
    return e.define("select2/i18n/zh-CN", [], function () {
        return { ... }
    }), {define: e.define, require: e.require}
});

@victorleal
Copy link

victorleal commented Apr 11, 2017

@mirari this solution works, but is not right to do that in the source file; for example the node_modules directory in my project is ignored, so other developers will have to do this alternative fix always.

A workaround is to use your solution in the main app file. I've done it and it is working.

@kevin-brown I think this is an important problem. Do we have any news here?

@stale
Copy link

stale bot commented Mar 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Mar 13, 2019
@stale stale bot closed this as completed Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants