Setting Language Preferences (revisited)

I have an addon which appears to have broken in 9.0 due to something to do with language preferences.
Thanks to replies from an older topic: Setting Language Preferences I have the following code to fetch the current primary languages:

function getPrimaryLanguages() {
    var Cc=Components.classes,Ci=Components.interfaces;
    var langRegistry=Cc['@activestate.com/koLanguageRegistryService;1'].getService(Ci.koILanguageRegistryService);
    var languages=[];

    //    Get Languages
        var languageNames = {};
        langRegistry.getLanguageNames(languageNames , {}); // second {} needed to keep xpcom happy
        languageNames = languageNames.value;
        languageNames.forEach(function(name) {
            var koLanguage = langRegistry.getLanguage(name);
            if(koLanguage.primary) languages.push(name);
        });
    return languages;
}

I do get a lit of languages, but it doesn’t appear to be correct. It doesn’t match the default primary languages, and it doesn’t match the changes I make in the preferences.

My addon sets its own preferred primary languages. After it does, the function does return the correct results, but when I restart Komodo Edit, the list is wrong again. It feels as if the languages settings have changed for version 9, and I’m using an old API.

Also, after the addon changes the primary languages, they do not seem to tak effect until after a restart. Is there a way I can refresh the list without a restart?

Thanks

What code are you using to set a language as primary?

Thanks for your reply. The code I use is below, based on replies to the previous forum question.

As commented below, I did find that the method I had previously used to set primary languages doesn’t appear to work in version 9. I guess that language preferences have moved?

The code to update the list has never worked for me.

Thanks

    var prefs = Components.classes['@activestate.com/koPrefService;1'].getService(Components.interfaces.koIPrefService).prefs;
    var prefset = prefs.QueryInterface(Components.interfaces.koIPreferenceSet);

    var Cc=Components.classes,Ci=Components.interfaces;
    var langRegistry = Cc["@activestate.com/koLanguageRegistryService;1"].getService(Ci.koILanguageRegistryService);
    var languageNames = {};
        langRegistry.getLanguageNames(languageNames , {}); // second {} needed to keep xpcom happy
        languageNames = languageNames.value;
/*    See below
    var langPrefs;
    if (!ko.prefs.hasPrefHere('languages')) {    //    create prefs
        langPrefs = Cc['@activestate.com/koPreferenceSet;1'].createInstance();
        ko.prefs.setPref("languages", langPrefs);
    } else {                                    //    prefs already exists
        langPrefs = ko.prefs.getPref("languages");
        var ids = {};
        langPrefs.getPrefIds(ids, {});
    }
*/
//    Set Primary Languages
    var madeChange = false;

    languageNames.forEach(function(name) {
        var koLanguage = langRegistry.getLanguage(name);
        var shouldBePrimary = languages.indexOf(name) >= 0;
        if (koLanguage.primary != shouldBePrimary) {
            koLanguage.primary = shouldBePrimary;
            madeChange = true;


/*    Not working in version 9?
            var langPrefName = name;
            var langPref = !langPrefs.hasPref(langPrefName) ? Cc["@activestate.com/koPreferenceSet;1"].createInstance() : langPrefs.getPref(langPrefName);

                langPref.setBooleanPref('primary', shouldBePrimary);
                if (!langPrefs.hasPref(langPrefName)) langPrefs.setPref(langPrefName, langPref);
*/
            //    This appears to wor in version 9:
            prefset.setBooleanPref('languages/%s/primary'.sprintf(name), shouldBePrimary);
        }
    });

    //    This doesn’t seem to work at all
    if (madeChange) {
        var obsSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
        obsSvc.notifyObservers(null, 'primary_languages_changed', '');
    }

I believe you should be calling prefs.setBooleanPref, not prefset.setBooleanPref.

Also if you’re just using javascript its safer to use require("ko/prefs"); to get the pref object.

Thanks for that. I have tired using prefs, and it seems to do the job. A few more:

  1. Should I be doing that to set the other preferences as well?
    (newEncoding, newBOM, useTabs, tabWidth, indentWidth)
  2. This doesn’t help in the problem that the list in the status bar is
    not updated until I restart.

Thanks