""" Utility functions for TV_APP Helper functions for translations, device detection, and common calculations """ import json import os import re from flask import request, session DEFAULT_LANGUAGE = 'sl' def load_translations(language='sl'): """Load translations for the specified language""" try: locale_file = os.path.join('locales', f'{language}.json') if os.path.exists(locale_file): with open(locale_file, 'r', encoding='utf-8') as f: return json.load(f) except Exception as e: print(f"Error loading translations for {language}: {e}") # Fallback to default language try: default_file = os.path.join('locales', f'{DEFAULT_LANGUAGE}.json') with open(default_file, 'r', encoding='utf-8') as f: return json.load(f) except Exception as e: print(f"Error loading default translations: {e}") return {} def get_current_language(): """Get current language from session or default""" return session.get('language', DEFAULT_LANGUAGE) def get_translations(): """Get translations for current language""" return load_translations(get_current_language()) def is_mobile_device(): """Check if the request is coming from a mobile device""" user_agent = request.headers.get('User-Agent', '').lower() mobile_patterns = [ r'android', r'iphone', r'ipad', r'ipod', r'blackberry', r'iemobile', r'opera mini', r'mobile', r'tablet' ] return any(re.search(pattern, user_agent) for pattern in mobile_patterns) def calculate_tens_from_targets(targets): """Calculate the number of 10s from a targets dictionary""" tens_count = 0 if not targets: return 0 for target in targets.values(): if isinstance(target, dict): for shot_key, shot_value in target.items(): if shot_key.startswith('shot') and shot_value == 10: tens_count += 1 return tens_count def validate_player_data(player_data): """Validate player data structure""" required_fields = ['id', 'name', 'enabled'] return all(field in player_data for field in required_fields) def validate_settings(settings): """Validate settings structure""" required_sections = ['camera_titles', 'display_options'] return all(section in settings for section in required_sections)