Your question has already been asked here in Stackoverflow and several of them already answer you how to do what you want, or rather, none of them actually tell you how you should do it, just give you the possibilities because there is no better way depends a lot on how you conduct / develop your website / webapp.
The best way to detect the user's language would be to Accept-Language
, which takes the person's preferred browser language, for example, I prefer English, so most of the sites I go to redirect to the American version ( in-US).
With just this link below you can already find your answers.
How to make a multilingual site and identify the country of origin?
Some use what you want to know, 'pt.domain.com', is nothing more than a subdomain, but believe me, there is nothing "automatic" translation, but only the detection of the user's language and then it is up to them to decide how they will translate, examples:
mysite.com/en-US/index.php,
mysite.com/en-in/index.php,
Or translations of the menus and etc saved in the database and then when detected the language, the translation is searched in the database and returned.
Or as you have seen, a subdomain containing the same system, only translated.
I hope I have helped.