Skip to content

Added **kwargs support for menu translations #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions addons/source-python/packages/source-python/menus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <text>:
The text that should be displayed.

@param <kwars>:
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.
Expand Down Expand Up @@ -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 <text>:
Expand All @@ -338,8 +346,11 @@ def __init__(self, text, value=None, highlight=True, selectable=True):

@param <selectable>:
Set this to True if the option should be selectable.

@param <kwars>:
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
Expand All @@ -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 <text> 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

Expand Down
46 changes: 38 additions & 8 deletions addons/source-python/packages/source-python/menus/esc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=None, title_kwargs=None):
"""Initialize the SimpleESCMenu instance.

@param <data>:
Expand Down Expand Up @@ -73,12 +74,20 @@ def __init__(

@param <title_color>:
The color of the title.

@param <description_kwargs>:
Descriptions's keyword arguments for _translate_text.

@param <title_kwargs>:
Title's keyword arguments for _translate_text.
"""
super(SimpleESCMenu, self).__init__(
data, select_callback, build_callback)
self.description = description
self.description_kwargs = description_kwargs or {}
self.title = title
self.title_color = title_color
self.title_kwargs = title_kwargs or {}

def _get_menu_data(self, player_index):
"""Return all relevant menu data as a KeyValues instance.
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 <choice_index>:
Expand All @@ -350,9 +373,12 @@ def __init__(

@param <selectable>:
Does not work with ESC menus.

@param <kwars>:
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):
Expand All @@ -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):
Expand All @@ -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)
)
26 changes: 18 additions & 8 deletions addons/source-python/packages/source-python/menus/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=None, title_kwargs=None):
"""Initialize the PagedRadioMenu instance.

@param <data>:
Expand Down Expand Up @@ -171,13 +171,21 @@ def __init__(
@param <fill>:
If True the menu will be filled so that it will always have the same
size.

@param <description_kwargs>:
Descriptions's keyword arguments for _translate_text.

@param <title_kwargs>:
Title's keyword arguments for _translate_text.
"""
super(PagedRadioMenu, self).__init__(
data, select_callback, build_callback
)

self.title = title
self.title_kwargs = title_kwargs or {}
self.description = description
self.description_kwargs = description_kwargs or {}
self.top_seperator = top_seperator
self.bottom_seperator = bottom_seperator
self.fill = fill
Expand All @@ -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:
Expand Down Expand Up @@ -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 <choice_index>:
Expand All @@ -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):
Expand All @@ -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)
)


Expand All @@ -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)
)