You can get the browser locale by using the request object:
That'll return a string that'll contain something like en-US, which you can then use to get the internal language object:
So, to get the base language ID:
#set($userLangId = $text.getLanguage('en','US').getId())
To set it for the visit:
And remember, you can't set the language for the session if any HTML has been output to the user. You'll want to do this first thing in the template. You can easily make the code above as dynamic as you need to pass the country and language code entirely from the header, just make sure you have a failsafe in case it's a locale you don't support with a dictionary entry.