]> git.lyx.org Git - lyx.git/blobdiff - src/HunspellChecker.cpp
Fairly trivial change here: Just protect against inheriting limits to
[lyx.git] / src / HunspellChecker.cpp
index af3d8404c01b09ad7e8fb8ab35792249cbe13a32..e055ae6ab694e362aff699b8135a1e1d08d1642e 100644 (file)
@@ -57,9 +57,9 @@ struct HunspellChecker::Private
 
        const string dictPath(int selector);
        bool haveLanguageFiles(string const & hpath);
-       bool haveDictionary(string const & lang, string & hpath);
-       bool haveDictionary(string const & lang);
-       Hunspell * addSpeller(string const & lang, string & hpath);
+       bool haveDictionary(Language const * lang, string & hpath);
+       bool haveDictionary(Language const * lang);
+       Hunspell * addSpeller(Language const * lang, string & hpath);
        Hunspell * addSpeller(Language const * lang);
        Hunspell * speller(Language const * lang);
        /// ignored words
@@ -77,8 +77,13 @@ struct HunspellChecker::Private
 
        /// the location below system/user directory
        /// there the aff+dic files lookup will happen
-       const string dictDirectory(void) const { return "dict"; }
+       const string dictDirectory(void) const { return "dicts"; }
        int maxLookupSelector(void) const { return 3; }
+       const string HunspellDictionaryName(Language const * lang) {
+               return lang->variety().empty() 
+                       ? lang->code()
+                       : lang->code() + "-" + lang->variety();
+       }
 };
 
 
@@ -132,43 +137,47 @@ const string HunspellChecker::Private::dictPath(int selector)
 }
 
 
-bool HunspellChecker::Private::haveDictionary(string const & lang, string & hpath)
+bool HunspellChecker::Private::haveDictionary(Language const * lang, string & hpath)
 {
        if (hpath.empty()) {
                return false;
        }
 
        LYXERR(Debug::FILES, "check hunspell path: " << hpath << " for language " << lang);
-       string h_path = addName(hpath, lang);
+       string h_path = addName(hpath, HunspellDictionaryName(lang));
+       // first we try lang code+variety
+       if (haveLanguageFiles(h_path)) {
+               hpath = h_path;
+               return true;
+       }
+       // another try with code, '_' replaced by '-'
+       h_path = addName(hpath, subst(lang->code(), '_', '-'));
        if (!haveLanguageFiles(h_path)) {
-               // try with '_' replaced by '-'
-               h_path = addName(hpath, subst(lang, '_', '-'));
-               if (!haveLanguageFiles(h_path)) {
-                       // FIXME: We should indicate somehow that this language is not
-                       // supported, probably by popping a warning. But we'll need to
-                       // remember which warnings we've issued.
-                       return false;
-               }
+               return false;
        }
        hpath = h_path;
        return true;
 }
 
 
-bool HunspellChecker::Private::haveDictionary(string const & lang)
+bool HunspellChecker::Private::haveDictionary(Language const * lang)
 {
        bool result = false;
        for ( int p = 0; !result && p < maxLookupSelector(); p++ ) {
                string lpath = dictPath(p);
                result = haveDictionary(lang, lpath);
        }
+       // FIXME: if result is false... 
+       // we should indicate somehow that this language is not
+       // supported, probably by popping a warning. But we'll need to
+       // remember which warnings we've issued.
        return result;
 }
 
 
 Hunspell * HunspellChecker::Private::speller(Language const * lang)
 {
-       Spellers::iterator it = spellers_.find(lang->code());
+       Spellers::iterator it = spellers_.find(lang->lang());
        if (it != spellers_.end())
                return it->second;
 
@@ -176,10 +185,10 @@ Hunspell * HunspellChecker::Private::speller(Language const * lang)
 }
 
 
-Hunspell * HunspellChecker::Private::addSpeller(string const & lang,string & path)
+Hunspell * HunspellChecker::Private::addSpeller(Language const * lang,string & path)
 {
        if (!haveDictionary(lang, path)) {
-               spellers_[lang] = 0;
+               spellers_[lang->lang()] = 0;
                return 0;
        }
 
@@ -187,7 +196,7 @@ Hunspell * HunspellChecker::Private::addSpeller(string const & lang,string & pat
        FileName const dict(path + ".dic");
        Hunspell * h = new Hunspell(affix.absFileName().c_str(), dict.absFileName().c_str());
        LYXERR(Debug::FILES, "Hunspell speller for langage " << lang << " at " << dict << " found");
-       spellers_[lang] = h;
+       spellers_[lang->lang()] = h;
        return h;
 }
 
@@ -197,7 +206,7 @@ Hunspell * HunspellChecker::Private::addSpeller(Language const * lang)
        Hunspell * h = 0;
        for ( int p = 0; p < maxLookupSelector() && 0 == h; p++ ) {
                string lpath = dictPath(p);
-               h = addSpeller(lang->code(), lpath);
+               h = addSpeller(lang, lpath);
        }
        if (0 != h) {
                string const encoding = h->get_dic_encoding();
@@ -360,7 +369,7 @@ bool HunspellChecker::hasDictionary(Language const * lang) const
 {
        if (!lang)
                return false;
-       return (d->haveDictionary(lang->code()));
+       return (d->haveDictionary(lang));
 }