From 7d5688988002200bda067ebc9302c67877b6ae13 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Tue, 6 Aug 2024 09:46:37 -0300 Subject: [PATCH] feat: Add user language detection to platform_text The changes in this commit add a function to detect the user's language and include it in the `platform_text` without assuming English as a fallback. The language detection is done by checking the `locale` module and common environment variables. If a language is detected, it is added to the `platform_text` with a note about how it was obtained. If no language is detected, no language information is added to `platform_text`. --- aider/coders/base_coder.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index f2a04f4a0..21f06f5b6 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -11,6 +11,7 @@ import sys import threading import time import traceback +import locale from collections import defaultdict from datetime import datetime from json.decoder import JSONDecodeError @@ -739,6 +740,21 @@ class Coder: ] self.cur_messages = [] + def get_user_language(self): + try: + lang = locale.getlocale()[0] + if lang: + return lang.split('_')[0] # Extract just the language code + except: + pass + + for env_var in ['LANG', 'LANGUAGE', 'LC_ALL', 'LC_MESSAGES']: + lang = os.environ.get(env_var) + if lang: + return lang.split('_')[0] # Extract just the language code + + return None + def fmt_system_prompt(self, prompt): lazy_prompt = self.gpt_prompts.lazy_prompt if self.main_model.lazy else "" @@ -750,6 +766,11 @@ class Coder: val = os.getenv(var) platform_text += f"- The user's shell: {var}={val}\n" + + user_lang = self.get_user_language() + if user_lang: + platform_text += f"- The user's language: {user_lang} (detected from locale or environment variables)\n" + dt = datetime.now().isoformat() platform_text += f"- The current date/time: {dt}"