diff --git a/addons/source-python/packages/source-python/translations/strings.py b/addons/source-python/packages/source-python/translations/strings.py index 6984584bd..21b331838 100644 --- a/addons/source-python/packages/source-python/translations/strings.py +++ b/addons/source-python/packages/source-python/translations/strings.py @@ -217,12 +217,6 @@ def _replace_escaped_sequences(given_string): # Return the replaced string return given_string - def get_strings(self, key, **tokens): - """Return a TranslationStrings object with updated tokens.""" - strings = self[key] - strings.tokens.update(tokens) - return strings - class TranslationStrings(dict): """Stores and get language strings for a particular string.""" @@ -250,11 +244,39 @@ def get_string(self, language=None, **tokens): # Possibly raise an error silently here return '' - # Update the stored tokens with the given ones - self.tokens.update(tokens) + # Expose all TranslationStrings instances in self.tokens + exposed_tokens = {} + for token_name, token in self.tokens.items(): + if isinstance(token, TranslationStrings): + + # Pass additional tokens - these will be used to format + # the string + token = token.get_string(language, **tokens) + + exposed_tokens[token_name] = token + + # Expose all TranslationsStrings instances in **tokens + for token_name, token in tokens.items(): + if isinstance(token, TranslationStrings): + + # Don't pass any additional tokens, the token should either + # be trivial or rely on itself (self.tokens) + token = token.get_string(language) + + exposed_tokens[token_name] = token + + # Get the language shortname to be used + language = self.get_language(language) + + # Was a valid language found? + if language is None: + + # Return an empty string + # Possibly raise an error silently here + return '' # Return the formatted message - return self[language].format(**self.tokens) + return self[language].format(**exposed_tokens) def get_language(self, language): """Return the language to be used.""" @@ -291,5 +313,20 @@ def get_language(self, language): # Return None as the language, as no language has been found return None + def tokenized(self, **tokens): + """Create a new TranslationStrings instance and store tokens in it. + + :param dict tokens: Tokens to store in the instance. + :return: New TranslationStrings instance with tokens stored in it. + :rtype: TranslationStrings + """ + result = TranslationStrings() + result.tokens.update(tokens) + + for key, value in self.items(): + result[key] = value + + return result + # Get the translations language strings _translation_strings = LangStrings('_core/translations_strings') diff --git a/addons/source-python/packages/source-python/weapons/_base.py b/addons/source-python/packages/source-python/weapons/_base.py index 1b08dce6a..6a99d1f9c 100644 --- a/addons/source-python/packages/source-python/weapons/_base.py +++ b/addons/source-python/packages/source-python/weapons/_base.py @@ -34,6 +34,14 @@ def _validate_clip(self): ) or self._clip == -1: raise ValueError('Weapon does not have a clip.') + def has_clip(self): + """Check if the weapon has a clip.""" + try: + self._validate_clip() + except ValueError: + return False + return True + def get_clip(self): """Return the amount of ammo in the weapon's clip.""" self._validate_clip() @@ -67,6 +75,14 @@ def _validate_ammo(self): return player + def has_ammo(self): + """Check if the weapon has a valid ammoprop and an owner.""" + try: + self._validate_ammo() + except ValueError: + return False + return True + def get_ammo(self): """Return the amount of ammo the player has for the weapon.""" player = self._validate_ammo() @@ -98,6 +114,14 @@ def _validate_secondary_fire_clip(self): if self._secondary_fire_clip == -1: raise ValueError('Weapon does not have a secondary fire clip.') + def has_secondary_fire_clip(self): + """Check if the weapon has a secondary fire clip.""" + try: + self._validate_secondary_fire_clip() + except ValueError: + return False + return True + def get_secondary_fire_clip(self): """Return the amount of ammo in the weapon's secondary fire clip.""" self._validate_secondary_fire_clip() @@ -114,7 +138,8 @@ def set_secondary_fire_clip(self, value): doc="""Property to get/set the weapon's secondary fire clip.""") def _validate_secondary_fire_ammo(self): - """Test if the weapon has a valid secondary fire ammoprop and an owner.""" + """Test if the weapon has a valid secondary fire ammoprop + and an owner.""" if self.secondary_fire_ammoprop == -1: raise ValueError( 'Unable to get secondary fire ammoprop for {0}'.format( @@ -126,6 +151,15 @@ def _validate_secondary_fire_ammo(self): return player + def has_secondary_fire_ammo(self): + """Check if the weapon has a valid secondary fire ammoprop + and an owner.""" + try: + self._validate_secondary_fire_ammo() + except ValueError: + return False + return True + def get_secondary_fire_ammo(self): """Return the secondary fire ammo the player has for the weapon.""" player = self._validate_secondary_fire_ammo()