1. #1
    Sencha User
    Join Date
    Dec 2009
    Location
    Tokyo
    Posts
    46
    Vote Rating
    0
    TigrouMeow is on a distinguished road

      0  

    Question Unanswered: MVC with dynamic loading and... i18n ?

    Unanswered: MVC with dynamic loading and... i18n ?


    I would like to translate my ExtJS application in different languages. My issue is that I'm using ExtJS MVC framework, and most of my JS files are downloaded dynamically by the framework itself.
    The ideal solution (that I thought of) would be to have an extra option in the Ext.Loader (or in my Ext.app.Application) that would define the language to use, and depending on this to automatically download such file as "a.MyClass.fr.js" after loading my "a.MyClass.js" (which would contain an Ext.apply, overriding my string resources). That's probably not available in the ExtJS framework at the moment.
    The alternative solution I can see, is to perform a trick on the server-side. First, a cookie would be created on the client, to set to the language. On the server-side, I could catch all the requests to JS files, then if a cookie is set (='fr' for example), I'd combine the requested JS file (MyClass.js) with its i18n's friend (MyClass.fr.js) dynamically on the server and return the result. That would work, but it's really tricky because it implies other things (caching...).
    Maybe the best way is to implement the first behavior I described in the ExtJS framework myself...
    What do you think? I'm looking for a really clean and neat way of doing it! Thanks

  2. #2
    Sencha User
    Join Date
    Aug 2009
    Posts
    11
    Vote Rating
    0
    vdan is on a distinguished road

      0  

    Default


    If you want to dynamically download a file, and assuming the loader is enabled, you could do:

    PHP Code:
    Ext.require('path/to/file'); 
    "Recommendation": When going into production, you would normally not download all the files of your app individually (see the getting started guide). But that is a side topic...

  3. #3
    Sencha User
    Join Date
    Dec 2009
    Location
    Tokyo
    Posts
    46
    Vote Rating
    0
    TigrouMeow is on a distinguished road

      0  

    Default


    It's actually a test environment, I can + enjoy to use dynamic loading, and it's pretty convenient within the MVC framework as well. So that would be another issue / question

    I could use Ext.require, but... with what? Where? I don't know when my file ("MyClass.js") is going to be loaded, and I need to apply my localization right after it (apply).

    It would be nice to have an "afterload" listener on the Ext.Loader: I could then have a handler on it, which would actually call Ext.require with my localized filename ("MyClass.fr.js").

    The hierarchy of folders within the MVC framework is quite strict and nice (controller / model / store / view...), but it would be nice to have a "i18n" folder that would be managed as well as the rest.

  4. #4
    Sencha User
    Join Date
    Aug 2009
    Posts
    11
    Vote Rating
    0
    vdan is on a distinguished road

      0  

    Default


    You may try to have a "require" directly in MyClass.js, so you are sure MyClass-lang.js is loaded when MyClass is loaded.

  5. #5
    Sencha User
    Join Date
    Dec 2009
    Location
    Tokyo
    Posts
    46
    Vote Rating
    0
    TigrouMeow is on a distinguished road

      0  

    Default


    Mmm, I would have to add those "require" on all my classes then, manually. I could maybe create a patch (Ext.apply) on the Ext.Component (right before to create my Application), so that the constructor will check if there are any "require" to perform. Wouldn't be too hard and too dirty, isn't it?

  6. #6
    Sencha User
    Join Date
    Aug 2009
    Posts
    11
    Vote Rating
    0
    vdan is on a distinguished road

      0  

    Default


    Then maybe have a look at this plugin.

  7. #7
    Sencha User
    Join Date
    Dec 2009
    Location
    Tokyo
    Posts
    46
    Vote Rating
    0
    TigrouMeow is on a distinguished road

      0  

    Default


    It's nice but not really helping neither. I still want to be able to have one resource file for each class I have. I think I'll try to make it by myself now then post it here.

  8. #8
    Sencha User mberrie's Avatar
    Join Date
    Feb 2011
    Location
    Bangkok, Thailand
    Posts
    506
    Answers
    26
    Vote Rating
    14
    mberrie will become famous soon enough mberrie will become famous soon enough

      0  

    Default


    EDIT: damn forum software!

    TigrouMeow,

    What about creating a class preprocessor that adds a dependency on the resource file to the class definition.

    Something like this:

    Code:
            Ext.Class.registerPreprocessor('resourceloader', function(cls, data) {
                var dependencies = data.requires = data.requires || [];
                dependencies.push(data.$className + 'Resources');
            }, true).setDefaultPreprocessorPosition('resourceloader', 'after', 'className');
    This needs more work, you most likely don't want to load a resource file for EVERY class in the application including Ext classes.
    Besides, if a resource file is missing, the class loading process will break (since it is a *required* dependency).

  9. #9
    Sencha User
    Join Date
    Dec 2009
    Location
    Tokyo
    Posts
    46
    Vote Rating
    0
    TigrouMeow is on a distinguished road

      0  

    Default


    Sorry I didn't come back earlier with a reply, mberrie. That's definitely what I was looking for, thanks very much for that ! And it lead me to read all the articles about ExtJS 4 Class system all over again I'm still working on my i18n dynamic loading, it's a bit tricky, but I may come with a solution very soon. I'll post it asap, then later I'll need your help to make it better and really helpful for more developers.

  10. #10
    Sencha User
    Join Date
    Dec 2009
    Location
    Tokyo
    Posts
    46
    Vote Rating
    0
    TigrouMeow is on a distinguished road

      0  

    Smile


    Okay, here is my first attempt to do this. It works in my environment. This code must be loaded at the beginning, then there is some documentation at the beginning (it should be enough). I am going to enhance it now, and to test it a bit more, but please try it on your side too I'll upload it on github or somewhere else later.

    Code:
    /*
    
    // LOCALE LOADER
    // Author: Jordy Theiller
    // Update: 2011/08/31
    
    LocaleLoader can be used to dynamically load the resources files.
    To enabled it, this must be done right after or before Ext.Loader.setConfig:
    
    
    Ext.Loader.setLocale({
        enabled: true,
        language: 'fr',
        types: [ 'controller', 'view' ]
    });
    
    
    This configuration will enable the dynamic localization, using the 'fr' locale,
    for the controllers and views (or more specifically, the namespace that contains
    your application name + type).
    
    
    The localization files have to be in this folder: '/locale/{language}'.
    For instance, with the previous configuration, it would be found in '/locale/fr'.
    
    
    Localization files must contain classes which will redefine string resources (which areused in 
    the normal classes). For instance, if we have a 'MyApp.controller.Coordinator', the localization
    file would be '/locale/fr/controller/Coordinator.js' and would contain something like:
    
    
    Ext.define('MyApp.locale.fr.controller.Coordinator', {
        xDeleteCurrentTabConfirmation: "Souhaitez-vous vraiment supprimer cet onglet ?"
    });
    
    
    Please note that the resource attributes MUST start with the letter 'x'.
    
    
    Of course, the 'MyApp.controller.Coordinator' class also contains the 'xDeleteCurrentTabConfirmation'
    string that should be, ideally, in English (as a default language). If the strings aren't found in the
    locale file, the defaults strings will be used.
    
    
    Ext.define('MyApp.controller.Coordinator', {
        xDeleteCurrentTabConfirmation: "Do you really want to remove this tab?"
    });
    
    
    ** FURTHER CONFIGURATION **
    The 'localized' property can be used on classes to disable the automatic loading of its
    locale file. By default, localized will be considered 'true'.
    */
    
    
    Ext.apply(Ext.Loader, {
        
        locale: {
            enabled: false,
            language: null,
            path: 'locale',
            types: []
        },
        
        setLocale: function (config) {
            Ext.apply(this.locale, config);
            this.initLocaleManager();
        },
        
        initLocaleManager: function () {
            
            if (!this.locale.enabled) {
                return;
            }
            
            // LOCALE LOADER
            Ext.Class.registerPreprocessor('localeLoader', function (cls, data, callback) {
                var scope = Ext.Loader;
                var className = data.$className;
                var type = null;
    
    
                // LOADING A CLASS THAT REQUIRES A LOCALE FILE
                if (scope.locale.enabled && data.localized !== false && className.indexOf(".locale." + scope.locale.language) === -1) {
                    for (var c in scope.locale.types) {
                        var currentType = scope.locale.types[c];
                        if (className.indexOf(currentType) != -1) {
                            type = currentType;
                        }
                    } 
                    if (type != null) {
                        var dependencies = data.requires = data.requires || [];
                        var appName = className.substring(0, className.indexOf("." + type));
                        var dependency = appName + ".locale." + scope.locale.language + "." + className.substring(appName.length + 1);
                        dependencies.push(dependency);
                    }
                }
            }, true).setDefaultPreprocessorPosition('localeLoader', 'first');
            
            // LOCALE APPLIER
            Ext.Class.registerPreprocessor('localeApplier', function (cls, data, fn) {
                var scope = Ext.Loader;
                var className = data.$className;
                var type = null;
                
                // LOADING A LOCALE CLASS
                if (scope.locale.enabled && data.localized !== false && className.indexOf(".locale." + scope.locale.language) === -1) {
                    for (var c in scope.locale.types) {
                        var currentType = scope.locale.types[c];
                        if (className.indexOf(currentType) != -1) {
                            type = currentType;
                        }
                    }
                    if (type != null) {
                        var appName = className.substring(0, className.indexOf("." + type));
                        var dependency = appName + ".locale." + scope.locale.language + "." + className.substring(appName.length + 1);
                        var a = Ext.create(dependency);
                        var localeProperties = { };
                        for (var c in a) {
                            if (c[0] === 'x') {
                                localeProperties[c] = a[c];
                            }
                        };
                        Ext.apply(data, localeProperties);
                    }
                }
            }, true).setDefaultPreprocessorPosition('localeApplier', 'last');
        }
    });

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..."