diff --git a/core/os/main_loop.h b/core/os/main_loop.h index 6506a280a32..52510614236 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -54,6 +54,7 @@ public: NOTIFICATION_WM_QUIT_REQUEST = 7, NOTIFICATION_WM_UNFOCUS_REQUEST = 8, NOTIFICATION_OS_MEMORY_WARNING = 9, + NOTIFICATION_TRANSLATION_CHANGED = 10, }; virtual void input_event( const InputEvent& p_event ); diff --git a/core/translation.cpp b/core/translation.cpp index 22d873f70bc..dd0f59f3431 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -939,6 +939,10 @@ void TranslationServer::set_locale(const String& p_locale) { else { locale=univ_locale; } + + if (OS::get_singleton()->get_main_loop()) { + OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED); + } } String TranslationServer::get_locale() const { diff --git a/scene/gui/button.cpp b/scene/gui/button.cpp index a7e08f9f7e8..f28595b6225 100644 --- a/scene/gui/button.cpp +++ b/scene/gui/button.cpp @@ -34,7 +34,7 @@ Size2 Button::get_minimum_size() const { - Size2 minsize=get_font("font")->get_string_size( text ); + Size2 minsize=get_font("font")->get_string_size( xl_text ); if (clip_text) minsize.width=0; @@ -48,7 +48,7 @@ Size2 Button::get_minimum_size() const { minsize.height=MAX( minsize.height, _icon->get_height() ); minsize.width+=_icon->get_width(); - if (text!="") + if (xl_text!="") minsize.width+=get_constant("hseparation"); } @@ -59,6 +59,13 @@ Size2 Button::get_minimum_size() const { void Button::_notification(int p_what) { + if (p_what==NOTIFICATION_TRANSLATION_CHANGED) { + + xl_text=XL_MESSAGE(text); + minimum_size_changed(); + update(); + } + if (p_what==NOTIFICATION_DRAW) { RID ci = get_canvas_item(); @@ -114,7 +121,7 @@ void Button::_notification(int p_what) { Point2 icon_ofs = (!_icon.is_null())?Point2( _icon->get_width() + get_constant("hseparation"), 0):Point2(); int text_clip=size.width - style->get_minimum_size().width - icon_ofs.width; - Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size( text ) )/2.0; + Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size( xl_text ) )/2.0; switch(align) { case ALIGN_LEFT: { @@ -128,14 +135,14 @@ void Button::_notification(int p_what) { text_ofs+=style->get_offset(); } break; case ALIGN_RIGHT: { - text_ofs.x=size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size( text ).x; + text_ofs.x=size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size( xl_text ).x; text_ofs.y+=style->get_offset().y; } break; } text_ofs.y+=font->get_ascent(); - font->draw( ci, text_ofs.floor(), text, color,clip_text?text_clip:-1); + font->draw( ci, text_ofs.floor(), xl_text, color,clip_text?text_clip:-1); if (!_icon.is_null()) { int valign = size.height-style->get_minimum_size().y; @@ -152,7 +159,8 @@ void Button::set_text(const String& p_text) { if (text==p_text) return; - text=XL_MESSAGE(p_text); + text=p_text; + xl_text=XL_MESSAGE(p_text); update(); _change_notify("text"); minimum_size_changed(); diff --git a/scene/gui/button.h b/scene/gui/button.h index 408e31e111a..710581a5562 100644 --- a/scene/gui/button.h +++ b/scene/gui/button.h @@ -49,6 +49,7 @@ private: bool flat; String text; + String xl_text; Ref icon; bool clip_text; TextAlign align; diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index dda9e4b128f..cd500a62bca 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -68,6 +68,13 @@ int Label::get_line_height() const { void Label::_notification(int p_what) { + if (p_what==NOTIFICATION_TRANSLATION_CHANGED) { + + xl_text=XL_MESSAGE(text); + minimum_size_changed(); + update(); + } + if (p_what==NOTIFICATION_DRAW) { if (clip || autowrap) { @@ -240,8 +247,8 @@ void Label::_notification(int p_what) { for (int i=0;iword_len;i++) { if (visible_chars < 0 || chars_total_shadowword_len;i++) { if (visible_chars < 0 || chars_totalget_char_size(current,text[i+1]).width; + int char_width=font->get_char_size(current,xl_text[i+1]).width; line_width+=char_width; } @@ -396,9 +403,9 @@ void Label::regenerate_word_cache() { WordCache *last=NULL; - for (int i=0;i 0 || last==NULL || last->char_pos!=WordCache::CHAR_WRAPLINE) { space_count++; @@ -455,7 +462,7 @@ void Label::regenerate_word_cache() { word_pos=i; } - char_width=font->get_char_size(current,text[i+1]).width; + char_width=font->get_char_size(current,xl_text[i+1]).width; current_word_size+=char_width; line_width+=char_width; total_char_cache++; @@ -541,12 +548,11 @@ Label::VAlign Label::get_valign() const{ void Label::set_text(const String& p_string) { - String str = XL_MESSAGE(p_string); - if (text==str) + if (text==p_string) return; - - text=str; + text=p_string; + xl_text=XL_MESSAGE(p_string); word_cache_dirty=true; if (percent_visible<1) visible_chars=get_total_character_count()*percent_visible; @@ -693,7 +699,7 @@ Label::Label(const String &p_text) { align=ALIGN_LEFT; valign=VALIGN_TOP; - text=""; + xl_text=""; word_cache=NULL; word_cache_dirty=true; autowrap=false; diff --git a/scene/gui/label.h b/scene/gui/label.h index 8a166bbe52d..80e4c970f6b 100644 --- a/scene/gui/label.h +++ b/scene/gui/label.h @@ -58,6 +58,7 @@ private: Align align; VAlign valign; String text; + String xl_text; bool autowrap; bool clip; Size2 minsize; diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 34b2675c523..9eaf393a218 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -96,7 +96,7 @@ Size2 PopupMenu::get_minimum_size() const { size.width+=check_w+hseparation; } - String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].text; + String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text; size.width+=font->get_string_size(text).width; if (i>0) size.height+=vseparation; @@ -421,7 +421,16 @@ void PopupMenu::_notification(int p_what) { switch(p_what) { + case NOTIFICATION_TRANSLATION_CHANGED: { + for(int i=0;iget_ascent(); - String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].text; + String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text; if (!items[i].separator) { font->draw(ci,item_ofs+Point2(0,Math::floor((h-font_h)/2.0)),text,items[i].disabled?font_color_disabled:(i==mouse_over?font_color_hover:font_color)); @@ -537,6 +546,7 @@ void PopupMenu::add_icon_item(const Ref& p_icon,const String& p_label,i Item item; item.icon=p_icon; item.text=p_label; + item.xl_text=XL_MESSAGE(p_label); item.accel=p_accel; item.ID=p_ID; items.push_back(item); @@ -545,7 +555,8 @@ void PopupMenu::add_icon_item(const Ref& p_icon,const String& p_label,i void PopupMenu::add_item(const String& p_label,int p_ID,uint32_t p_accel) { Item item; - item.text=XL_MESSAGE(p_label); + item.text=p_label; + item.xl_text=XL_MESSAGE(p_label); item.accel=p_accel; item.ID=p_ID; items.push_back(item); @@ -555,7 +566,8 @@ void PopupMenu::add_item(const String& p_label,int p_ID,uint32_t p_accel) { void PopupMenu::add_submenu_item(const String& p_label, const String& p_submenu,int p_ID){ Item item; - item.text=XL_MESSAGE(p_label); + item.text=p_label; + item.xl_text=XL_MESSAGE(p_label); item.ID=p_ID; item.submenu=p_submenu; items.push_back(item); @@ -566,7 +578,8 @@ void PopupMenu::add_icon_check_item(const Ref& p_icon,const String& p_l Item item; item.icon=p_icon; - item.text=XL_MESSAGE(p_label); + item.text=p_label; + item.xl_text=XL_MESSAGE(p_label); item.accel=p_accel; item.ID=p_ID; item.checkable=true; @@ -576,7 +589,8 @@ void PopupMenu::add_icon_check_item(const Ref& p_icon,const String& p_l void PopupMenu::add_check_item(const String& p_label,int p_ID,uint32_t p_accel) { Item item; - item.text=XL_MESSAGE(p_label); + item.text=p_label; + item.xl_text=XL_MESSAGE(p_label); item.accel=p_accel; item.ID=p_ID; item.checkable=true; @@ -649,7 +663,8 @@ void PopupMenu::add_check_shortcut(const Ref& p_shortcut, int p_ID, bo void PopupMenu::set_item_text(int p_idx,const String& p_text) { ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].text=XL_MESSAGE(p_text); + items[p_idx].text=p_text; + items[p_idx].xl_text=XL_MESSAGE(p_text); update(); @@ -1082,8 +1097,8 @@ void PopupMenu::get_translatable_strings(List *p_strings) const { for(int i=0;ipush_back(items[i].text); + if (items[i].xl_text!="") + p_strings->push_back(items[i].xl_text); } } diff --git a/scene/gui/popup_menu.h b/scene/gui/popup_menu.h index be12a5fc3c2..b5fca9a4518 100644 --- a/scene/gui/popup_menu.h +++ b/scene/gui/popup_menu.h @@ -44,6 +44,7 @@ class PopupMenu : public Popup { struct Item { Ref icon; String text; + String xl_text; bool checked; bool checkable; bool separator; diff --git a/scene/main/node.h b/scene/main/node.h index 117ce646452..f9d334629e1 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -222,6 +222,7 @@ public: NOTIFICATION_DRAG_BEGIN=21, NOTIFICATION_DRAG_END=22, NOTIFICATION_PATH_CHANGED=23, + NOTIFICATION_TRANSLATION_CHANGED=24, }; /* NODE/TREE */ diff --git a/scene/main/scene_main_loop.cpp b/scene/main/scene_main_loop.cpp index 418f4362b0e..7ea7e490201 100644 --- a/scene/main/scene_main_loop.cpp +++ b/scene/main/scene_main_loop.cpp @@ -650,6 +650,9 @@ void SceneTree::_notification(int p_notification) { get_root()->propagate_notification(p_notification); } break; + case NOTIFICATION_TRANSLATION_CHANGED: { + get_root()->propagate_notification(Node::NOTIFICATION_TRANSLATION_CHANGED); + } break; case NOTIFICATION_WM_UNFOCUS_REQUEST: { notify_group(GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input",NOTIFICATION_WM_UNFOCUS_REQUEST);