def create_localized_string(identifier: Union[int, str, LocalizedString], tokens: Tuple[Any]=(), localize_tokens: bool=True, text_color: CommonLocalizedStringColor=CommonLocalizedStringColor.DEFAULT) -> LocalizedString: """create_localized_string(identifier, tokens=(), localize_tokens=True, text_color=CommonLocalizedStringColor.DEFAULT) Create a LocalizedString formatted with the specified tokens. :param identifier: An identifier to locate a LocalizedString with, text that will be turned into a LocalizedString, or a LocalizedString itself. :type identifier: Union[int, str, LocalizedString] :param tokens: A collection of objects to format into the localized string. (They can be anything. LocalizedString, str, int, SimInfo, just to name a few) :type tokens: Tuple[Any] :param localize_tokens: If True, the specified tokens will be localized. If False, the specified tokens will be formatted into the LocalizedString as they are. Default is True :type localize_tokens: bool :param text_color: The color the text will be when displayed. :type text_color: CommonLocalizedStringColor :return: A localized string ready for display. :rtype: LocalizedString """ if identifier is None: return CommonLocalizationUtils.create_localized_string(CommonStringId.STRING_NOT_FOUND_WITH_IDENTIFIER, tokens=('None',), text_color=text_color) if localize_tokens: tokens = CommonLocalizationUtils._normalize_tokens(*tokens) if isinstance(identifier, LocalizedString) and hasattr(identifier, 'tokens'): create_tokens(identifier.tokens, tokens) return CommonLocalizationUtils.colorize(identifier, text_color=text_color) if isinstance(identifier, int): return CommonLocalizationUtils.colorize(CommonLocalizationUtils.create_from_int(identifier, *tokens), text_color=text_color) if hasattr(identifier, 'sim_info'): return identifier.sim_info if hasattr(identifier, 'get_sim_info'): return identifier.get_sim_info() if isinstance(identifier, str): return CommonLocalizationUtils.create_localized_string(CommonLocalizationUtils.create_from_string(identifier), tokens=tokens, text_color=text_color) return CommonLocalizationUtils.create_localized_string(str(identifier), tokens=tokens, text_color=text_color)
def create_pie_menu_message(sim, context, choice_menu, reference_id, pie_menu_action, target=None): msg = interaction_protocol.PieMenuCreate() msg.sim = sim.id if sim is not None else 0 msg.client_reference_id = reference_id msg.server_reference_id = 0 if not choice_menu: fire_service = services.get_fire_service() if fire_service.fire_is_active: msg.disabled_tooltip = fire_service.INTERACTION_UNAVAILABLE_DUE_TO_FIRE_TOOLTIP() return msg if pie_menu_action == PieMenuActions.INTERACTION_QUEUE_FULL_TOOLTIP: msg.disabled_tooltip = PieMenuActions.INTERACTION_QUEUE_FULL_STR(sim) return msg create_tokens(msg.category_tokens, sim, target, None if target is None else target.get_stored_sim_info()) if choice_menu is not None: msg.server_reference_id = choice_menu.revision for (option_id, item) in choice_menu: with ProtocolBufferRollback(msg.items) as item_msg: item_msg.id = item.choice.aop_id choice = item.choice logger.debug('%3d: %s' % (option_id, choice)) name = choice.affordance.get_name(choice.target, context, **choice.interaction_parameters) (name_override_tunable, name_override_result) = choice.affordance.get_name_override_tunable_and_result(target=choice.target, context=context) if _make_pass: name = InteractionCommandsTuning.MAKE_PASS_INTERACTION_NAME(name) if _show_interaction_tuning_name: affordance_tuning_name = str(choice.affordance.__name__) name = InteractionCommandsTuning.INTERACTION_TUNING_NAME(name, affordance_tuning_name) item_msg.loc_string = name tooltip = item.result.tooltip if tooltip is not None: tooltip = choice.affordance.create_localized_string(tooltip, context=context, target=choice.target, **choice.interaction_parameters) item_msg.disabled_text = tooltip else: success_tooltip = choice.affordance.get_display_tooltip(override=name_override_tunable, context=context, target=choice.target, **choice.interaction_parameters) if success_tooltip is not None: item_msg.success_tooltip = success_tooltip pie_menu_icon = choice.affordance.pie_menu_icon category_key = item.category_key if name_override_tunable.new_pie_menu_icon is not None: pie_menu_icon = name_override_tunable.new_pie_menu_icon if name_override_tunable is not None and name_override_tunable.new_pie_menu_category is not None: category_key = name_override_tunable.new_pie_menu_category.guid64 if pie_menu_icon is not None: item_msg.icon_infos.append(create_icon_info_msg(IconInfoData(icon_resource=pie_menu_icon))) if category_key is not None: item_msg.category_key = category_key if item.result.icon is not None: item_msg.icon_infos.append(create_icon_info_msg(IconInfoData(icon_resource=item.result.icon))) if choice.show_posture_incompatible_icon: item_msg.icon_infos.append(create_icon_info_msg(IconInfoData(icon_resource=PieMenuActions.POSTURE_INCOMPATIBLE_ICON))) handle_pie_menu_item_coloring(item_msg, item, sim, choice, name_override_result) for visual_target in choice.affordance.visual_targets_gen(choice.target, context, **choice.interaction_parameters): while visual_target is not None: item_msg.target_ids.append(visual_target.id) item_msg.score = item.choice.content_score if item.choice.content_score is not None else 0 item_msg.pie_menu_priority = choice.affordance.pie_menu_priority return msg
def AddTokens(localizedString: localization.LocalizedString, *tokens) -> None: """ Add these tokens to a localization string. :param localizedString: The localized string the tokens are to be added to. :type localizedString: localization.LocalizedString :param tokens: Valid tokens include any object with a function called "populate_localization_token", numbers, strings, LocalizedString objects, and arrays. Array tokens seem to be considered lists of sims and all objects inside them require the "populate_localization_token" function. :return: """ if not isinstance(localizedString, localization.LocalizedString): raise Exceptions.IncorrectTypeException( localizedString, "localizedString", (localization.LocalizedString, )) # noinspection PyUnresolvedReferences localization.create_tokens(localizedString.tokens, *tokens)
def GetLocalizationStringByKey(key: int, *tokens) -> localization.LocalizedString: """ Find localized string by key. :param key: The key for the desired localization string. :type key: int :param tokens: Valid tokens include any object with a function called "populate_localization_token", numbers, strings, LocalizedString objects, and arrays. Array tokens seem to be considered lists of sims and all objects inside them require the "populate_localization_token" function. :rtype: localization.LocalizedString """ if not isinstance(key, int): raise Exceptions.IncorrectTypeException(key, "key", (int, )) localizedString = localization.LocalizedString() localizedString.hash = key # noinspection PyUnresolvedReferences localization.create_tokens(localizedString.tokens, *tokens) return localizedString
def get_localized_string(object_value, tokens=()): if object_value is None: return TurboL18NUtil.get_localized_string(0) verified_tokens = list() for token in tokens: verified_tokens.append(TurboL18NUtil.get_localized_string(token)) if isinstance(object_value, LocalizedString): create_tokens(object_value.tokens, verified_tokens) return object_value if isinstance(object_value, int): return TurboL18NUtil.get_localized_string_from_stbl_id( object_value, tokens=verified_tokens) if isinstance(object_value, str): return TurboL18NUtil.get_localized_string( TurboL18NUtil.get_localized_string_from_text(object_value), tokens=verified_tokens) if hasattr(object_value, 'populate_localization_token'): return object_value return TurboL18NUtil.get_localized_string(str(object_value), tokens=verified_tokens)
def _GetDialogLocalizationString (key: int, *tokens) -> typing.Callable[[], localization.LocalizedString]: localizationString = localization.LocalizedString() localizationString.hash = key # noinspection PyUnresolvedReferences localization.create_tokens(localizationString.tokens, *tokens) return lambda *args, **kwargs: localizationString