When a user visits a Next.js site, the app tries to detect their language preference. First, it looks at the URL path to see if a locale code like 'fr' or 'de' is present. If it finds one, it uses that locale. If not, it checks the browser's Accept-Language header to guess the preferred language. If that is missing or empty, it defaults to English ('en'). This approach respects explicit user choices in the URL and falls back gracefully. The detection function uses a simple regular expression to extract the locale from the URL and splits the Accept-Language header to get the first language code. This logic helps the app render pages in the right language automatically.