Skip to content

Commit

Permalink
Add multiple language support via env file.
Browse files Browse the repository at this point in the history
  • Loading branch information
50t0r25 committed Oct 12, 2024
1 parent 5bfc190 commit aea55db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 11 additions & 3 deletions achievement_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def convert_from_unixtime(unix_time):
local_achievements_path = os.getenv("LOCAL_ACHIEVEMENTS_PATH", r"%appdata%\GSE saves")
games_path = os.getenv("GAMES_PATH", r"C:\games")

# Load the preferred language from the environment variable (default to 'english')
preferred_language = os.getenv('LANGUAGE', 'english')

# Resolve environment variables like %appdata% to actual paths
local_achievements_path = os.path.expandvars(local_achievements_path)
games_path = os.path.expandvars(games_path)
Expand All @@ -35,6 +38,11 @@ def load_json(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)

# Function to get the achievement text in the preferred language, with a fallback to English
def get_achievement_text(recent_achievement, key):
return recent_achievement.get(key, {}).get(preferred_language) or \
recent_achievement.get(key, {}).get('english', 'Unknown')

# Find matching games with steam_appid.txt (normal mode)
matching_games = []
appid_files = glob.glob(f"{games_path}/**/steam_appid.txt", recursive=True)
Expand Down Expand Up @@ -97,13 +105,13 @@ def load_json(file_path):
earned = local_achievement['earned']
earned_time = convert_from_unixtime(local_achievement['earned_time'])

# Display achievement details
print(Fore.WHITE + "Achievement: " + Fore.CYAN + game_achievement['displayName']['english'])
# Display achievement details using the preferred language with a fallback to English
print(Fore.WHITE + "Achievement: " + Fore.CYAN + get_achievement_text(game_achievement, 'displayName'))

if game_achievement.get('hidden', 0) == 1 and not earned and not args.nohide:
print(Fore.YELLOW + "This achievement is hidden.")
else:
print(Fore.WHITE + "Description: " + Fore.CYAN + game_achievement['description']['english'])
print(Fore.WHITE + "Description: " + Fore.CYAN + get_achievement_text(game_achievement, 'description'))

# Check for progress tracking
if 'progress' in local_achievement and 'max_progress' in local_achievement:
Expand Down
16 changes: 11 additions & 5 deletions achievement_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def load_json(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)

# Function to get the display text for a specific language, with fallback to English
def get_display_text(recent_achievement, key, language):
# Attempt to get the text in the specified language; fallback to English if not available
return recent_achievement.get(key, {}).get(language) or recent_achievement.get(key, {}).get('english', 'Unknown')

# Function to find the most recent earned achievement
def find_recent_achievement(local_achievements):
return max(
Expand Down Expand Up @@ -81,6 +86,7 @@ class AchievementHandler(FileSystemEventHandler):
def __init__(self, game_cache):
super().__init__()
self.game_cache = game_cache
self.language = os.getenv('LANGUAGE', 'english') # Load the language from the environment variable

def on_modified(self, event):
self.process_achievement_file(event)
Expand Down Expand Up @@ -149,11 +155,11 @@ def process_achievement_file(self, event):
icon_path = recent_achievement.get('icon', None)
if icon_path:
icon_path = os.path.join(os.path.dirname(game_data["achievements_path"]), icon_path)
asyncio.run(self.send_notification(
recent_achievement['displayName']['english'],
recent_achievement['description']['english'],
icon_path
))

title = get_display_text(recent_achievement, 'displayName', self.language)
description = get_display_text(recent_achievement, 'description', self.language)

asyncio.run(self.send_notification(title, description, icon_path))

# Update the last earned timestamp in cache to the latest achievement
if achievement_data['earned_time'] > game_data['last_earned_time']:
Expand Down

0 comments on commit aea55db

Please sign in to comment.