From be21fbe2f094abbf26f6af550395f1a4b7265130 Mon Sep 17 00:00:00 2001 From: Markus Meskanen Date: Wed, 4 Mar 2015 02:34:49 +0200 Subject: [PATCH 1/3] Added **kwargs support for menu translations All menus now support keyword arguments when translating them. This includes _MenuData classes (texts and options), descriptions, and titles. --- .../packages/source-python/menus/base.py | 23 +++++++--- .../packages/source-python/menus/esc.py | 46 +++++++++++++++---- .../packages/source-python/menus/radio.py | 26 +++++++---- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/addons/source-python/packages/source-python/menus/base.py b/addons/source-python/packages/source-python/menus/base.py index f281805a9..7445fbbd7 100644 --- a/addons/source-python/packages/source-python/menus/base.py +++ b/addons/source-python/packages/source-python/menus/base.py @@ -228,13 +228,17 @@ class _MenuData(object): All data types should inherit from this class. """ - def __init__(self, text): + def __init__(self, text, **kwargs): """Initialize the instance. @param : The text that should be displayed. + + @param : + Keyword arguments passed to the _translate_text function. """ self.text = text + self.kwargs = kwargs def _render(self, player_index, choice_index=None): """Render the data. @@ -317,14 +321,18 @@ def _render(self, player_index, choice_index=None): The number should be required to select this item. It depends on the menu type if this parameter gets passed. """ - return str(_translate_text(self.text, player_index)) + '\n' + return '{0}\n'.format( + _translate_text(self.text, player_index, **self.kwargs) + ) class _BaseOption(_MenuData): """This class is used to display an enumerated option.""" - def __init__(self, text, value=None, highlight=True, selectable=True): + def __init__( + self, text, value=None, highlight=True, + selectable=True, **kwargs): """Initialize the option. @param : @@ -338,8 +346,11 @@ def __init__(self, text, value=None, highlight=True, selectable=True): @param : Set this to True if the option should be selectable. + + @param : + Keyword arguments passed to the _translate_text function. """ - super(_BaseOption, self).__init__(text) + super(_BaseOption, self).__init__(text, **kwargs) self.value = value self.highlight = highlight self.selectable = selectable @@ -360,13 +371,13 @@ def _render(self, player_index, choice_index=None): # ============================================================================= # >> HELPER FUNCTIONS # ============================================================================= -def _translate_text(text, player_index): +def _translate_text(text, player_index, **kwargs): """Translate if it is an instance of TranslationStrings. Otherwise the original text will be returned. """ if isinstance(text, TranslationStrings): - return text.get_string(get_client_language(player_index)) + return text.get_string(get_client_language(player_index), **kwargs) return text diff --git a/addons/source-python/packages/source-python/menus/esc.py b/addons/source-python/packages/source-python/menus/esc.py index 9075db020..a174abf29 100644 --- a/addons/source-python/packages/source-python/menus/esc.py +++ b/addons/source-python/packages/source-python/menus/esc.py @@ -44,7 +44,8 @@ class SimpleESCMenu(_BaseMenu): def __init__( self, data=None, select_callback=None, build_callback=None, - description=None, title=None, title_color=WHITE): + description=None, title=None, title_color=WHITE, + description_kwargs={}, title_kwargs={}): """Initialize the SimpleESCMenu instance. @param : @@ -73,12 +74,20 @@ def __init__( @param : The color of the title. + + @param : + Descriptions's keyword arguments for _translate_text. + + @param : + Title's keyword arguments for _translate_text. """ super(SimpleESCMenu, self).__init__( data, select_callback, build_callback) self.description = description + self.description_kwargs = description_kwargs self.title = title self.title_color = title_color + self.title_kwargs = title_kwargs def _get_menu_data(self, player_index): """Return all relevant menu data as a KeyValues instance. @@ -88,7 +97,13 @@ def _get_menu_data(self, player_index): """ data = KeyValues('menu') data.set_string( - 'msg', _translate_text(self.description or '', player_index)) + 'msg', + _translate_text( + self.description or '', + player_index, + **self.description_kwargs + ) + ) page = self._player_pages[player_index] page.options = {} @@ -212,7 +227,9 @@ def _format_header(self, player_index, page, data): if self.title: data.set_string('title', '{0} {1}'.format( - _translate_text(self.title, player_index), info)) + _translate_text(self.title, player_index, **self.title_kwargs), + info + )) else: data.set_string('title', info) @@ -283,7 +300,13 @@ def _get_menu_data(self, player_index): """ data = KeyValues('menu') data.set_string( - 'msg', _translate_text(self.description or '', player_index)) + 'msg', + _translate_text( + self.description or '', + player_index, + **self.description_kwargs + ) + ) # Get the player's current page page = self._player_pages[player_index] @@ -333,7 +356,7 @@ class SimpleESCOption(_BaseOption): def __init__( self, choice_index, text, value=None, - highlight=True, selectable=True): + highlight=True, selectable=True, **kwargs): """Initialize the option. @param : @@ -350,9 +373,12 @@ def __init__( @param : Does not work with ESC menus. + + @param : + Keyword arguments passed to the _translate_text function. """ super(SimpleESCOption, self).__init__( - text, value, highlight, selectable) + text, value, highlight, selectable, **kwargs) self.choice_index = choice_index def _render(self, player_index, choice_index=None): @@ -366,7 +392,9 @@ def _render(self, player_index, choice_index=None): menu type if this parameter gets passed. """ return '{0}. {1}'.format( - self.choice_index, _translate_text(self.text, player_index)) + self.choice_index, + _translate_text(self.text, player_index, **self.kwargs) + ) class PagedESCOption(_BaseOption): @@ -384,4 +412,6 @@ def _render(self, player_index, choice_index=None): menu type if this parameter gets passed. """ return '{0}. {1}'.format( - choice_index, _translate_text(self.text, player_index)) + choice_index, + _translate_text(self.text, player_index, **self.kwargs) + ) diff --git a/addons/source-python/packages/source-python/menus/radio.py b/addons/source-python/packages/source-python/menus/radio.py index be94834ec..e08c462fd 100644 --- a/addons/source-python/packages/source-python/menus/radio.py +++ b/addons/source-python/packages/source-python/menus/radio.py @@ -135,7 +135,7 @@ def __init__( self, data=None, select_callback=None, build_callback=None, description=None, title=None, top_seperator='-' * 30, bottom_seperator='-' * 30, - fill=True): + fill=True, description_kwargs={}, title_kwargs={}): """Initialize the PagedRadioMenu instance. @param : @@ -171,13 +171,21 @@ def __init__( @param : If True the menu will be filled so that it will always have the same size. + + @param : + Descriptions's keyword arguments for _translate_text. + + @param : + Title's keyword arguments for _translate_text. """ super(PagedRadioMenu, self).__init__( data, select_callback, build_callback ) self.title = title + self.title_kwargs = title_kwargs self.description = description + self.description_kwargs = description_kwargs self.top_seperator = top_seperator self.bottom_seperator = bottom_seperator self.fill = fill @@ -201,12 +209,14 @@ def _format_header(self, player_index, page, slots): # Create the page info string info = '[{0}/{1}]\n'.format(page.index + 1, self.page_count) - buffer = '{0} {1}'.format(_translate_text( - self.title, player_index), info) if self.title else info + buffer = '{0} {1}'.format( + _translate_text(self.title, player_index, **self.title_kwargs), + info) if self.title else info # Set description if present if self.description is not None: - buffer += _translate_text(self.description, player_index) + '\n' + buffer += _translate_text( + self.description, player_index, **self) + '\n' # Set the top seperator if present if self.top_seperator is not None: @@ -361,7 +371,7 @@ class SimpleRadioOption(_BaseRadioOption): def __init__( self, choice_index, text, value=None, - highlight=True, selectable=True): + highlight=True, selectable=True, **kwargs): """Initialize the option. @param : @@ -380,7 +390,7 @@ def __init__( Set this to True if the option should be selectable. """ super(SimpleRadioOption, self).__init__( - text, value, highlight, selectable) + text, value, highlight, selectable, **kwargs) self.choice_index = choice_index def _render(self, player_index, choice_index=None): @@ -396,7 +406,7 @@ def _render(self, player_index, choice_index=None): return '{0}{1}. {2}\n'.format( self._get_highlight_prefix(), self.choice_index, - _translate_text(self.text, player_index) + _translate_text(self.text, player_index, **self.kwargs) ) @@ -417,5 +427,5 @@ def _render(self, player_index, choice_index): return '{0}{1}. {2}\n'.format( self._get_highlight_prefix(), choice_index, - _translate_text(self.text, player_index) + _translate_text(self.text, player_index, **self.kwargs) ) From 06012f883354024abe05471707ce5e969e843b19 Mon Sep 17 00:00:00 2001 From: Markus Meskanen Date: Wed, 4 Mar 2015 20:41:03 +0200 Subject: [PATCH 2/3] Removed default parameter {} fromPagedRadioMenu --- addons/source-python/packages/source-python/menus/radio.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/source-python/packages/source-python/menus/radio.py b/addons/source-python/packages/source-python/menus/radio.py index e08c462fd..80a635322 100644 --- a/addons/source-python/packages/source-python/menus/radio.py +++ b/addons/source-python/packages/source-python/menus/radio.py @@ -135,7 +135,7 @@ def __init__( self, data=None, select_callback=None, build_callback=None, description=None, title=None, top_seperator='-' * 30, bottom_seperator='-' * 30, - fill=True, description_kwargs={}, title_kwargs={}): + fill=True, description_kwargs=None, title_kwargs=None): """Initialize the PagedRadioMenu instance. @param : @@ -183,9 +183,9 @@ def __init__( ) self.title = title - self.title_kwargs = title_kwargs + self.title_kwargs = title_kwargs or {} self.description = description - self.description_kwargs = description_kwargs + self.description_kwargs = description_kwargs or {} self.top_seperator = top_seperator self.bottom_seperator = bottom_seperator self.fill = fill From 8534c5485a6f6de9929b56a9d98e077feb011e86 Mon Sep 17 00:00:00 2001 From: Markus Meskanen Date: Wed, 4 Mar 2015 20:42:05 +0200 Subject: [PATCH 3/3] Removed default parameter {} from SimpleESCMenu --- addons/source-python/packages/source-python/menus/esc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/source-python/packages/source-python/menus/esc.py b/addons/source-python/packages/source-python/menus/esc.py index a174abf29..865760ff4 100644 --- a/addons/source-python/packages/source-python/menus/esc.py +++ b/addons/source-python/packages/source-python/menus/esc.py @@ -45,7 +45,7 @@ class SimpleESCMenu(_BaseMenu): def __init__( self, data=None, select_callback=None, build_callback=None, description=None, title=None, title_color=WHITE, - description_kwargs={}, title_kwargs={}): + description_kwargs=None, title_kwargs=None): """Initialize the SimpleESCMenu instance. @param : @@ -84,10 +84,10 @@ def __init__( super(SimpleESCMenu, self).__init__( data, select_callback, build_callback) self.description = description - self.description_kwargs = description_kwargs + self.description_kwargs = description_kwargs or {} self.title = title self.title_color = title_color - self.title_kwargs = title_kwargs + self.title_kwargs = title_kwargs or {} def _get_menu_data(self, player_index): """Return all relevant menu data as a KeyValues instance.