Jean-Marc Lasgouttes wrote: > I do not like this much: > > + FloatList::const_iterator it = floats.begin(); > + FloatList::const_iterator end = floats.end(); > + if (guiname == _("Table of Contents")) > + return "TOC"; > + for (; it != end; ++it) { > + if (_(it->second.name()) == guiname) > + return it->second.type(); > + } > + return guiname; > > You should never have to compare against a translated value. I think > GUIName should be untranslated, and all translation should happen in > the frontends. > > So basically, I'd try to avoid using gettext at all in toc.C.
I had a look at this again. The problem is that we only have the translated names (from the combo) if we are requesting the contents of the toc browser. Since we cannot just "translate back", the only alternative to compare against translated strings is, AFAICS, to store the translated gui names and its corresponding types in another vector (or map, I don't know what is to be perferred). Cf. attached patch. What do you think? Jürgen
Index: toc.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/toc.C,v retrieving revision 1.45 diff -p -u -r1.45 toc.C --- toc.C 4 Jul 2005 13:05:03 -0000 1.45 +++ toc.C 26 Jul 2005 08:05:48 -0000 @@ -17,7 +17,6 @@ #include "bufferparams.h" #include "FloatList.h" #include "funcrequest.h" -#include "gettext.h" #include "LyXAction.h" #include "paragraph.h" #include "pariterator.h" @@ -61,20 +60,22 @@ string const getType(string const & cmdN { // special case if (cmdName == "tableofcontents") - return _("TOC"); + return "TOC"; else return cmdName; } -string const getGuiName(string const & cmdName, Buffer const & buffer) +string const getGuiName(string const & type, Buffer const & buffer) { FloatList const & floats = buffer.params().getLyXTextClass().floats(); - if (floats.typeExist(cmdName)) - return _(floats.getType(cmdName).name()); + if (floats.typeExist(type)) + return floats.getType(type).name(); + else if (type == "TOC") + return "Table of Contents"; else - return getType(cmdName); + return type; } @@ -130,7 +131,7 @@ TocList const getTocList(Buffer const & tocstring = pit->asString(buf, true); TocItem const item(pit->id(), toclevel - min_toclevel, tocstring); - toclist[_("TOC")].push_back(item); + toclist["TOC"].push_back(item); } } return toclist; Index: toc.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/toc.h,v retrieving revision 1.15 diff -p -u -r1.15 toc.h --- toc.h 4 Jul 2005 13:05:03 -0000 1.15 +++ toc.h 26 Jul 2005 08:05:48 -0000 @@ -65,8 +65,8 @@ void asciiTocList(std::string const &, B by ControlToc::getContents() */ std::string const getType(std::string const & cmdName); -/// Returns the guiname from a given CmdName -std::string const getGuiName(std::string const & cmdName, Buffer const &); +/// Returns the guiname from a given type +std::string const getGuiName(std::string const & type, Buffer const &); inline bool operator==(TocItem const & a, TocItem const & b) Index: frontends/controllers/ControlToc.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ControlToc.C,v retrieving revision 1.30 diff -p -u -r1.30 ControlToc.C --- frontends/controllers/ControlToc.C 4 Jul 2005 13:04:59 -0000 1.30 +++ frontends/controllers/ControlToc.C 26 Jul 2005 08:05:49 -0000 @@ -14,6 +14,7 @@ using std::vector; using std::string; +using std::pair; class Buffer; @@ -21,6 +22,13 @@ class Buffer; namespace lyx { namespace frontend { +namespace { + +vector<pair<string, string> > guiNames; + +} //namespace anon + + ControlToc::ControlToc(Dialog & d) : ControlCommand(d, "toc") {} @@ -38,9 +46,27 @@ vector<string> const ControlToc::getType } -string const ControlToc::getGuiName() const +string const ControlToc::getType(string const & guiname) const +{ + vector<pair<string, string> >::iterator it = guiNames.begin(); + vector<pair<string, string> >::iterator end = guiNames.end(); + for (; it != end; ++it) { + if (it->second == guiname) + return it->first; + } + return guiname; +} + + +void ControlToc::setGuiNames(string const & type, string const & guiname) +{ + guiNames.push_back(pair<string, string>(type, guiname)); +} + + +string const ControlToc::getGuiName(string const & type) const { - return toc::getGuiName(params().getCmdName(), kernel().buffer()); + return toc::getGuiName(type, kernel().buffer()); } Index: frontends/controllers/ControlToc.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/controllers/ControlToc.h,v retrieving revision 1.15 diff -p -u -r1.15 ControlToc.h --- frontends/controllers/ControlToc.h 4 Jul 2005 13:04:59 -0000 1.15 +++ frontends/controllers/ControlToc.h 26 Jul 2005 08:05:49 -0000 @@ -33,8 +33,14 @@ public: /// Return the list of types available std::vector<std::string> const getTypes() const; + /// Return the type from a given gui name + std::string const ControlToc::getType(std::string const & guiname) const; + + /// Store the translated gui names + void setGuiNames(std::string const & type, std::string const & guiname); + /// Return the guiname from a given cmdName of the TOC param - std::string const getGuiName() const; + std::string const getGuiName(std::string const & type) const; /// Given a type, returns the contents toc::Toc const getContents(std::string const & type) const; Index: frontends/gtk/GToc.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/GToc.C,v retrieving revision 1.6 diff -p -u -r1.6 GToc.C --- frontends/gtk/GToc.C 4 Jul 2005 13:05:00 -0000 1.6 +++ frontends/gtk/GToc.C 26 Jul 2005 08:05:50 -0000 @@ -88,7 +88,8 @@ void GToc::update() void GToc::updateType() { changing_views_ = true; - string const targettype = controller().getGuiName(); + string const targettype = + toc::getType(controller().params().getCmdName()); typestore_->clear(); vector<string> types = controller().getTypes(); @@ -100,8 +101,12 @@ void GToc::updateType() vector<string>::iterator it = types.begin(); vector<string>::iterator end = types.end(); for(;it != end; ++it) { + // translate guiname now + string const & guiname = _(controller().getGuiName(*it)); + // and store it + controller().setGuiNames(*it, guiname); Gtk::TreeModel::iterator row = typestore_->append(); - (*row)[listCol_] = *it; + (*row)[listCol_] = guiname; if (*it == targettype) typecombo_->set_active(row); } @@ -119,7 +124,8 @@ void GToc::updateContents() } Gtk::TreeModel::iterator it = typecombo_->get_active(); - Glib::ustring const type = (*it)[listCol_]; + Glib::ustring const guiname = (*it)[listCol_]; + string const type = controller().getType(guiname); toc::Toc const contents = controller().getContents(type); // Check if all elements are the same. Index: frontends/qt2/QToc.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QToc.C,v retrieving revision 1.30 diff -p -u -r1.30 QToc.C --- frontends/qt2/QToc.C 4 Jul 2005 13:05:00 -0000 1.30 +++ frontends/qt2/QToc.C 26 Jul 2005 08:05:51 -0000 @@ -56,12 +56,16 @@ void QToc::updateType() dialog_->typeCO->clear(); vector<string> const & choice = controller().getTypes(); - string const & guiname = controller().getGuiName(); + string const & type = toc::getType(controller().params().getCmdName()); for (vector<string>::const_iterator it = choice.begin(); it != choice.end(); ++it) { - dialog_->typeCO->insertItem(toqstr(*it)); - if (*it == guiname) { + // translate the guiname now + string const & guiname = _(controller().getGuiName(*it)); + // store the translation + controller().setGuiNames(*it, guiname); + dialog_->typeCO->insertItem(toqstr(guiname)); + if (*it == type) { dialog_->typeCO->setCurrentItem(it - choice.begin()); setTitle(guiname); } @@ -78,7 +82,8 @@ void QToc::update_contents() void QToc::updateToc(int newdepth) { - string type = fromqstr(dialog_->typeCO->currentText()); + string const & type = controller().getType( + fromqstr(dialog_->typeCO->currentText())); toc::Toc const & contents = controller().getContents(type); @@ -157,6 +162,7 @@ void QToc::updateToc(int newdepth) dialog_->tocLV->setUpdatesEnabled(true); dialog_->tocLV->update(); + setTitle(dialog_->typeCO->currentText()); } Index: frontends/xforms/FormToc.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/xforms/FormToc.C,v retrieving revision 1.61 diff -p -u -r1.61 FormToc.C --- frontends/xforms/FormToc.C 4 Jul 2005 13:05:01 -0000 1.61 +++ frontends/xforms/FormToc.C 26 Jul 2005 08:05:52 -0000 @@ -85,18 +85,27 @@ void FormToc::updateType() { // Update the choice list from scratch fl_clear_choice(dialog_->choice_toc_type); - string const choice = getStringFromVector(controller().getTypes(), "|"); - fl_addto_choice(dialog_->choice_toc_type, choice.c_str()); + vector<string> const & choice = controller().getTypes(); + for (vector<string>::const_iterator it = choice.begin(); + it != choice.end(); ++it) { + // translate the guiname now + string const & guiname = _(controller().getGuiName(*it)); + // and store it + controller().setGuiNames(*it, guiname); + fl_addto_choice(dialog_->choice_toc_type, guiname.c_str()); + } // And select the correct one - string const guiname = controller().getGuiName(); - fl_set_choice_text(dialog_->choice_toc_type, guiname.c_str()); + string const & type = toc::getType(controller().params().getCmdName()); + string const act_guiname = _(controller().getGuiName(type)); + fl_set_choice_text(dialog_->choice_toc_type, act_guiname.c_str()); } void FormToc::updateContents() { - string const type = getString(dialog_->choice_toc_type); + string const type = controller().getType( + getString(dialog_->choice_toc_type)); if (type.empty()) { fl_clear_browser(dialog_->browser_toc); fl_add_browser_line(dialog_->browser_toc, Index: insets/insetfloat.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetfloat.C,v retrieving revision 1.140 diff -p -u -r1.140 insetfloat.C --- insets/insetfloat.C 9 May 2005 17:29:21 -0000 1.140 +++ insets/insetfloat.C 26 Jul 2005 08:05:54 -0000 @@ -434,12 +434,12 @@ void InsetFloat::addToToc(lyx::toc::TocL // Find a caption layout in one of the (child inset's) pars for (; pit != end; ++pit) { if (pit->layout()->labeltype == LABEL_SENSITIVE) { - string const name = floatname(params_.type, buf.params()); + string const type = params_.type; string const str = - convert<string>(toclist[name].size() + 1) + convert<string>(toclist[type].size() + 1) + ". " + pit->asString(buf, false); lyx::toc::TocItem const item(pit->id(), 0 , str); - toclist[name].push_back(item); + toclist[type].push_back(item); } } } Index: insets/insetwrap.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetwrap.C,v retrieving revision 1.76 diff -p -u -r1.76 insetwrap.C --- insets/insetwrap.C 9 May 2005 17:29:22 -0000 1.76 +++ insets/insetwrap.C 26 Jul 2005 08:05:59 -0000 @@ -242,12 +242,12 @@ void InsetWrap::addToToc(lyx::toc::TocLi // Find a caption layout in one of the (child inset's) pars for (; pit != end; ++pit) { if (pit->layout()->labeltype == LABEL_SENSITIVE) { - string const name = floatname(params_.type, buf.params()); + string const type = params_.type; string const str = - convert<string>(toclist[name].size() + 1) + convert<string>(toclist[type].size() + 1) + ". " + pit->asString(buf, false); lyx::toc::TocItem const item(pit->id(), 0 , str); - toclist[name].push_back(item); + toclist[type].push_back(item); } } }