[glom] TranslatableItem: Improve the API slightly.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] TranslatableItem: Improve the API slightly.
- Date: Tue, 20 Dec 2011 08:04:39 +0000 (UTC)
commit 8c20cb0ff6504ab5649ebc82ff95dc1b60e6ee0f
Author: Murray Cumming <murrayc murrayc com>
Date: Mon Dec 19 23:03:24 2011 +0100
TranslatableItem: Improve the API slightly.
* glom/libglom/data_structure/translatable_item.[h|cc]:
Renamed set/get_translation() to set/get_title_translation() to make
it clearer, and to match set/get_title_original(). Add an optional bool
fallback parameter, so we can move the fallback code into this method,
and simplify get_title(). This lets us easily get the title for a
specified locale, with one method, even if that locale is the original/
current locale.
Removed get_title(locale), because it is hardly used, because any
override of the other (virtual) get_title() method overload would make
it hard to call this anyway. And it just forwarded to
get_title_translation() anyway.
Likewise for set_title(locale).
* glom/libglom/document/document.cc:
* glom/mode_design/translation/window_translations.cc: Adapted.
* tests/test_document_load_translations.cc: Adapted and added some
more test lines for get_title_translation()
ChangeLog | 21 +++++
glom/libglom/data_structure/translatable_item.cc | 90 +++++++++----------
glom/libglom/data_structure/translatable_item.h | 19 ++---
glom/libglom/document/document.cc | 2 +-
.../mode_design/translation/window_translations.cc | 20 ++--
tests/test_document_load_translations.cc | 23 +++++
6 files changed, 106 insertions(+), 69 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9603ffc..214c784 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,26 @@
2011-12-19 Murray Cumming <murrayc murrayc com>
+ TranslatableItem: Improve the API slightly.
+
+ * glom/libglom/data_structure/translatable_item.[h|cc]:
+ Renamed set/get_translation() to set/get_title_translation() to make
+ it clearer, and to match set/get_title_original(). Add an optional bool
+ fallback parameter, so we can move the fallback code into this method,
+ and simplify get_title(). This lets us easily get the title for a
+ specified locale, with one method, even if that locale is the original/
+ current locale.
+ Removed get_title(locale), because it is hardly used, because any
+ override of the other (virtual) get_title() method overload would make
+ it hard to call this anyway. And it just forwarded to
+ get_title_translation() anyway.
+ Likewise for set_title(locale).
+ * glom/libglom/document/document.cc:
+ * glom/mode_design/translation/window_translations.cc: Adapted.
+ * tests/test_document_load_translations.cc: Adapted and added some
+ more test lines for get_title_translation().
+
+2011-12-19 Murray Cumming <murrayc murrayc com>
+
PostgreSQL backend: Comment out the stdout exception information again.
* glom/libglom/connectionpool_backends/postgres.cc: This was
diff --git a/glom/libglom/data_structure/translatable_item.cc b/glom/libglom/data_structure/translatable_item.cc
index 04fd85d..a575aed 100644
--- a/glom/libglom/data_structure/translatable_item.cc
+++ b/glom/libglom/data_structure/translatable_item.cc
@@ -71,7 +71,7 @@ bool TranslatableItem::operator!=(const TranslatableItem& src) const
return !(operator==(src));
}
-void TranslatableItem::set_translation(const Glib::ustring& locale, const Glib::ustring& translation)
+void TranslatableItem::set_title_translation(const Glib::ustring& locale, const Glib::ustring& translation)
{
if(translation.empty())
{
@@ -84,13 +84,51 @@ void TranslatableItem::set_translation(const Glib::ustring& locale, const Glib::
m_map_translations[locale] = translation;
}
-Glib::ustring TranslatableItem::get_translation(const Glib::ustring& locale) const
+Glib::ustring TranslatableItem::get_title_translation(const Glib::ustring& locale, bool fallback) const
{
type_map_locale_to_translations::const_iterator iterFind = m_map_translations.find(locale);
if(iterFind != m_map_translations.end())
return iterFind->second;
- else
+
+ //The original is not in m_map_translations,
+ //but we want to handle that locale too:
+ if(locale == get_current_locale())
+ return get_title_original();
+
+ if(!fallback)
return Glib::ustring();
+
+ if(m_map_translations.empty())
+ return get_title_original();
+
+ //Return the first translation from a locale with the same language, if any.
+ //TODO_Performance: This is slow.
+ //Note that this would, for instance, give en_GB translations before en_US translations, if there are no en_AU translations.
+ const Glib::ustring locale_language_id = Utils::locale_language_id(locale);
+ for(type_map_locale_to_translations::const_iterator iter = m_map_translations.begin(); iter != m_map_translations.end(); ++iter)
+ {
+ const Glib::ustring& locale_id = iter->first;
+ if(Utils::locale_language_id(locale_id) == locale_language_id)
+ {
+ if(!(iter->second.empty()))
+ return iter->second;
+ }
+ }
+
+ //Fall back to the original title:
+ if(!m_title.empty())
+ return m_title;
+
+ //Fall back to first translation, if any.
+ //This would be quite unusual.
+ type_map_locale_to_translations::const_iterator iter = m_map_translations.begin();
+ if(iter != m_map_translations.end())
+ {
+ //std::cout << "debug: TranslatableItem::get_title() falling back to the first translation: locale=" << iter->first << std::endl;
+ return iter->second;
+ }
+
+ return Glib::ustring();
}
const TranslatableItem::type_map_locale_to_translations& TranslatableItem::_get_translations_map() const
@@ -109,51 +147,14 @@ Glib::ustring TranslatableItem::get_title() const
if(get_current_locale_not_original()) //Avoid this code if we don't need translations.
{
const Glib::ustring current_locale_id = get_current_locale();
- const Glib::ustring translated_title = get_translation(current_locale_id);
+ const Glib::ustring translated_title = get_title_translation(current_locale_id);
if(!translated_title.empty())
return translated_title;
- else if(!(m_map_translations.empty()))
- {
- //return the first translation from a locale with the same language, if any.
- //TODO_Performance: This is slow.
- //Note that this would, for instance, give en_GB translations before en_US translations, if there are no en_AU translations.
- const Glib::ustring current_locale_language_id = Utils::locale_language_id(current_locale_id);
- for(type_map_locale_to_translations::const_iterator iter = m_map_translations.begin(); iter != m_map_translations.end(); ++iter)
- {
- const Glib::ustring& locale_id = iter->first;
- if(Utils::locale_language_id(locale_id) == current_locale_language_id)
- {
- if(!(iter->second.empty()))
- return iter->second;
- }
- }
-
- if(m_title.empty())
- {
- //return the first translation, if any.
- //This would be quite unusual.
- type_map_locale_to_translations::const_iterator iter = m_map_translations.begin();
- if(iter != m_map_translations.end())
- {
- //std::cout << "debug: TranslatableItem::get_title() falling back to the first translation: locale=" << iter->first << std::endl;
- return iter->second;
- }
- }
- else
- {
- return m_title;
- }
- }
}
return m_title;
}
-Glib::ustring TranslatableItem::get_title(const Glib::ustring& locale) const
-{
- return get_translation(locale);
-}
-
Glib::ustring TranslatableItem::get_title_original() const
{
@@ -170,7 +171,7 @@ void TranslatableItem::set_title(const Glib::ustring& title)
set_title_original(title);
else
{
- set_translation(the_locale, title);
+ set_title_translation(the_locale, title);
}
}
else
@@ -179,11 +180,6 @@ void TranslatableItem::set_title(const Glib::ustring& title)
}
}
-void TranslatableItem::set_title(const Glib::ustring& locale, const Glib::ustring& title)
-{
- set_translation(locale, title);
-}
-
void TranslatableItem::set_title_original(const Glib::ustring& title)
{
m_title = title;
diff --git a/glom/libglom/data_structure/translatable_item.h b/glom/libglom/data_structure/translatable_item.h
index ba6d43c..8c09811 100644
--- a/glom/libglom/data_structure/translatable_item.h
+++ b/glom/libglom/data_structure/translatable_item.h
@@ -57,10 +57,6 @@ public:
*/
virtual Glib::ustring get_title() const;
- /** Get the title's translation for the specifed locale.
- */
- Glib::ustring get_title(const Glib::ustring& locale) const;
-
/** Get the title's original (non-translated, usually English) text.
*/
Glib::ustring get_title_original() const;
@@ -70,17 +66,18 @@ public:
*/
void set_title(const Glib::ustring& title);
- /** Set the title's translation for the current locale.
- */
- void set_title(const Glib::ustring& locale, const Glib::ustring& title);
-
/** Set the title's original (non-translated, usually English) text.
*/
void set_title_original(const Glib::ustring& title);
- //TODO: Rename to set_title_translation()?
- void set_translation(const Glib::ustring& locale, const Glib::ustring& translation);
- Glib::ustring get_translation(const Glib::ustring& locale) const;
+ void set_title_translation(const Glib::ustring& locale, const Glib::ustring& translation);
+
+ /** Get the title's translation for the specified @a locale, optionally
+ * falling back to a locale of the same language, and then falling back to
+ * the original.
+ * Calling this with the current locale is the same as calling get_title_original().
+ */
+ Glib::ustring get_title_translation(const Glib::ustring& locale, bool fallback = true) const;
/// Clear the original title and any translations of the title.
void clear_title_in_all_locales();
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index 92a5fc1..49985e5 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -2527,7 +2527,7 @@ void Document::load_after_translations(const xmlpp::Element* element, Translatab
{
const Glib::ustring locale = get_node_attribute_value(element, GLOM_ATTRIBUTE_TRANSLATION_LOCALE);
const Glib::ustring translation = get_node_attribute_value(element, GLOM_ATTRIBUTE_TRANSLATION_VALUE);
- item.set_translation(locale, translation);
+ item.set_title_translation(locale, translation);
//Remember any new translation locales in our cached list:
if(std::find(m_translation_available_locales.begin(),
diff --git a/glom/mode_design/translation/window_translations.cc b/glom/mode_design/translation/window_translations.cc
index 1c3ab47..fd6ec4a 100644
--- a/glom/mode_design/translation/window_translations.cc
+++ b/glom/mode_design/translation/window_translations.cc
@@ -231,7 +231,7 @@ void Window_Translations::load_from_document()
Gtk::TreeModel::iterator iterTree = m_model->append();
Gtk::TreeModel::Row row = *iterTree;
row[m_columns.m_col_item] = tableinfo;
- row[m_columns.m_col_translation] = tableinfo->get_title(m_translation_locale);
+ row[m_columns.m_col_translation] = tableinfo->get_title_translation(m_translation_locale, false);
row[m_columns.m_col_parent_table] = Glib::ustring(); //Not used for tables.
//The table's field titles:
@@ -243,7 +243,7 @@ void Window_Translations::load_from_document()
sharedptr<Field> field = *iter;
row[m_columns.m_col_item] = field;
- row[m_columns.m_col_translation] = field->get_title(m_translation_locale);
+ row[m_columns.m_col_translation] = field->get_title_translation(m_translation_locale, false);
row[m_columns.m_col_parent_table] = table_name;
}
@@ -259,7 +259,7 @@ void Window_Translations::load_from_document()
Gtk::TreeModel::Row row = *iterTree;
row[m_columns.m_col_item] = relationship;
- row[m_columns.m_col_translation] = relationship->get_title(m_translation_locale);
+ row[m_columns.m_col_translation] = relationship->get_title_translation(m_translation_locale, false);
row[m_columns.m_col_parent_table] = table_name;
}
}
@@ -277,7 +277,7 @@ void Window_Translations::load_from_document()
Gtk::TreeModel::Row row = *iterTree;
row[m_columns.m_col_item] = report;
- row[m_columns.m_col_translation] = report->get_title(m_translation_locale);
+ row[m_columns.m_col_translation] = report->get_title_translation(m_translation_locale, false);
row[m_columns.m_col_parent_table] = table_name;
//Translatable report items:
@@ -293,7 +293,7 @@ void Window_Translations::load_from_document()
Gtk::TreeModel::Row row = *iterTree;
row[m_columns.m_col_item] = item;
- row[m_columns.m_col_translation] = item->get_title(m_translation_locale);
+ row[m_columns.m_col_translation] = item->get_title_translation(m_translation_locale, false);
row[m_columns.m_col_parent_table] = table_name;
}
}
@@ -314,7 +314,7 @@ void Window_Translations::load_from_document()
Gtk::TreeModel::Row row = *iterTree;
row[m_columns.m_col_item] = item;
- row[m_columns.m_col_translation] = item->get_title(m_translation_locale);
+ row[m_columns.m_col_translation] = item->get_title_translation(m_translation_locale, false);
row[m_columns.m_col_parent_table] = table_name;
}
}
@@ -340,7 +340,7 @@ void Window_Translations::save_to_document()
if(item)
{
const Glib::ustring translation = row[m_columns.m_col_translation];
- item->set_title(m_translation_locale, translation);
+ item->set_title_translation(m_translation_locale, translation);
}
}
@@ -386,7 +386,7 @@ void Window_Translations::on_button_copy_translation()
if(item)
{
//Copy the translation from the chosen locale to the current locale:
- const Glib::ustring translation = item->get_title(copy_source_locale);
+ const Glib::ustring translation = item->get_title_translation(copy_source_locale);
row[m_columns.m_col_translation] = translation;
}
}
@@ -547,7 +547,7 @@ void Window_Translations::on_button_export()
{
po_message_t msg = po_message_create();
po_message_set_msgid(msg, item->get_title_original().c_str());
- po_message_set_msgstr(msg, item->get_translation(m_translation_locale).c_str());
+ po_message_set_msgstr(msg, item->get_title_translation(m_translation_locale, false).c_str());
// Add "context" comments, to uniquely identify similar strings, used in different places,
// and to provide a hint for translators.
@@ -649,7 +649,7 @@ void Window_Translations::on_button_import()
if( (item->get_title_original() == msgid) &&
(get_po_context_for_item(item) == msgcontext) ) // This is not efficient, but it should be reliable.
{
- item->set_translation(m_translation_locale, msgstr);
+ item->set_title_translation(m_translation_locale, msgstr);
// Keep examining items, in case there are duplicates. break;
}
}
diff --git a/tests/test_document_load_translations.cc b/tests/test_document_load_translations.cc
index d73c598..0e84ad2 100644
--- a/tests/test_document_load_translations.cc
+++ b/tests/test_document_load_translations.cc
@@ -88,10 +88,33 @@ void check_title(const T_Item& item, const char* title_en, const char* title_de)
g_assert( item->get_title() == title_en );
+ //Check when changing the current locale:
const Glib::ustring locale_original = Glom::TranslatableItem::get_current_locale();
Glom::TranslatableItem::set_current_locale(locale_de);
g_assert( item->get_title() == title_de );
Glom::TranslatableItem::set_current_locale(locale_original);
+
+
+ //Don't do the following checks if get_title() would actually delegate to the
+ //child Field, instead of using the LayoutItem's own title translations:
+ const Glom::sharedptr<const Glom::LayoutItem_Field> layout_field =
+ Glom::sharedptr<const Glom::LayoutItem_Field>::cast_dynamic(item);
+ if(layout_field)
+ {
+ return;
+ }
+
+ //Check when getting the translations directly:
+ g_assert( item->get_title_original() == title_en );
+ g_assert( item->get_title_translation(locale_de) == title_de );
+
+ //Check fallbacks:
+ g_assert( item->get_title_translation(Glib::ustring()) == title_en );
+ g_assert( item->get_title_translation(locale_original) == title_en );
+
+ //Check that fallbacks do not happen when we don't want them:
+ g_assert( item->get_title_translation(Glib::ustring(), false) == Glib::ustring() );
+ g_assert( item->get_title_translation(locale_original, false) == Glib::ustring() );
}
int main()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]