How to detect system language?

Is it possible for MV to detect system language(in whatever system it runs, be it Mac, windows or linux) and set a standard variable(those inside the game) according to it?

Example:
If system language is portuguese. Variable = 2. Else(for every other language) its = 1
This is tricky considering the various possible OS and also the browser (if playing in the browser) or mobile OS (android/iphone). There's a thread about it on Stack Overflow and it looks like there are a lot of possibilities.

To at least get you started, experiment with:
Code:
navigator.language

Something like this to set the value of Variable #18.
Code:
if(navigator.language === ???) {
$gameVariables.setValue(18, 2)
} else {
$gameVariables.setValue(18,1)
}
Try navigator.language in the developer console (F8) to see what yours currently is. Mine is "en-us", for example.
You probably need to slice the first two characters and just use that for the language identifier. Here is a helpfullW3 Schools article on string functions.

Keep in mind this is not a complete answer and the method could definitely depend on OS / Browser / etc. Make sure to do a lot of testing on the various possibilities.
I will not port my game to browser, so probably it could be a javascript that works on chrome only(since our games run in a chromium, no matter the system)

Also, how to i check that in console? Couldn't find it >_<
When you open the console (F8), just type navigator.language and press enter.
You can find all sorts of useful stuff in the console, it's the first place to learn how RMMV works.
There is a superb tutorial by Poryg that you should check out here:
https://forums.rpgmakerweb.com/index.php?threads/lesson-about-console-use.85264/

Spoiler: Console Screenshot
Console.png
Ok, so it shows here "pt-br", so it must be working.

Now, when i tried to set it using $gameVariables.setValue(9,1); for english and $gameVariables.setValue(9,2); for portuguese it didn't worked(i changed ??? to en-us )

Also, could you tell me how to do this only once? Like, only when the player open the game FOR THE VERY FIRST TIME it sets the language. Every other time he opens, the plugin doesn't do anything.
Ok, good so far.
Make sure you use " " for any time you are comparing Strings. So if you want to see if the language is English-US the check would be
Code:
navigator.language === "en-us"

In this case, the country doesn't matter, we don't care if it is English from the US or from the UK or from anywhere else. Similarly, we don't care if it is Portuguese from Brazil or Portugal (correct me if I am wrong). So, we take just the first 2 characters by using the .slice method. Any String can be sliced.

Code:
if(navigator.language.slice(0,2) === "pt") {
    $gameVariables.setValue(9, 2);
} else {
    $gameVariables.setValue(9,1);
}

Also, could you tell me how to do this only once? Like, only when the player open the game FOR THE VERY FIRST TIME it sets the language. Every other time he opens, the plugin doesn't do anything.

Unfortunately I don't know how to do this exactly as you phrased it, but I do know how to do it by checking if a save file already exists. I think it's basically the same for you. After the player saves the game one time, this plugin will not do anything when they open the game again.

Code:
(function() {

if(!DataManager.isAnySavefileExists()) {
    if(navigator.language.slice(0,2) === "pt") {
        $gameVariables.setValue(9, 2);
    } else {
        $gameVariables.setValue(9,1);
    }
}

})();
Ok, good so far.
Make sure you use " " for any time you are comparing Strings. So if you want to see if the language is English-US the check would be
Code:
navigator.language === "en-us"

In this case, the country doesn't matter, we don't care if it is English from the US or from the UK or from anywhere else. Similarly, we don't care if it is Portuguese from Brazil or Portugal (correct me if I am wrong). So, we take just the first 2 characters by using the .slice method. Any String can be sliced.

Code:
if(navigator.language.slice(0,2) === "pt") {
    $gameVariables.setValue(9, 2);
} else {
    $gameVariables.setValue(9,1);
}



Unfortunately I don't know how to do this exactly as you phrased it, but I do know how to do it by checking if a save file already exists. I think it's basically the same for you. After the player saves the game one time, this plugin will not do anything when they open the game again.

Code:
(function() {

if(!DataManager.isAnySavefileExists()) {
    if(navigator.language.slice(0,2) === "pt") {
        $gameVariables.setValue(9, 2);
    } else {
        $gameVariables.setValue(9,1);
    }
}

})();
Didn't worked (at least in plat test). navigator.language still giving me pt-br but when game loads, menu still in english(so, variable language still 1)


EDIT:
That may be because of " SRD Option creator"plugin that is setting this variable as 1 for default? If so, how can i work around it?
Hey man, no it's not due to any other plugin actually it's due to an error from me.

The code I gave you before ran immediately when the game started. It did not wait for all the game data to be set up, this includes the Game Variables. Since those weren't set up yet, it was giving an error. You could actually see this error in the console (F8).

I attached here a new version that takes this into account. I also added some descriptions and parameters to make it more friendly.

The plugin reads the player's language when a new game is started. It will not do anything when loading saved games.

Spoiler: In case download doesn't work
Code:
// ======= =======
// SetLanguageVariable.js
// Set Variable Value on Start Based on Language
// ======= =======

/*:
 * @plugindesc Set the value of a variable based on the language of the player.
 * @author Aloe Guvner
 *
 * @param VariableNumber
 * @desc The Variable that should store data about the language
 * @default 9
 * @type number
 *
 * @param Language
 * @desc The 2-character code for the language to search
 * @default pt
 *
 * @param ValueIfTrue
 * @desc The Variable becomes this value if the user's language matches
 * the 2-character code above
 * @type number
 * @default 2
 *
 * @param ValueIfFalse
 * @desc The Variable becomes this value if the user's language does
 * not match the 2-character code above
 * @type number
 * @default 1
 *
 * @help This plugin will set a variable based on the language of the
 * player.
*/


(function() {

var params = PluginManager.parameters("SetLanguageVariable");

var languageId = String(params['Language'] || "pt");
var variableId = Number(params['VariableNumber'] || 9);
var val1 = Number(params['ValueIfTrue'] || 2);
var val2 = Number(params['ValueIfFalse'] || 1);

var alias_DataManager_setupNewGame = DataManager.setupNewGame
DataManager.setupNewGame = function() {
    alias_DataManager_setupNewGame.call(this);
    if(navigator.language.slice(0,2) === languageId) {
        $gameVariables.setValue(variableId, val1);
    } else {
        $gameVariables.setValue(variableId, val2);
    }
};

})();

EDIT: Was sloppy with a few semi-colons and typos, aesthetic edit only.
Perfect! Thank you so much!
Another way to go about this is if you are using Iavra Localization's plugin is to use the navigator.language method mentioned above but by also setting Iavra's locale upon game boot (if no game save is detected where language was configured). This makes it so that the language will be in whatever the user's browser language is set to, which will usually match system locale, upon the first time the player launches the game should your game support that language. Otherwise your game will launch in the default language and people may have a hard time finding the options and changing the set language.

Here's the snippet I used for my game in a new js file:

Code:
var _SceneBoot_initialize = Scene_Boot.prototype.initialize;
var locale = "";

Scene_Boot.prototype.initialize = function()
{
    _SceneBoot_initialize.call(this);
    
    locale = navigator.language.substring(0, 2);

    IAVRA.I18N.language = locale;
};

(function($)
{
    var alias_applyData = $.applyData;
    $.applyData = function(config)
    {
        if (config.language == null) config.language = locale;
        alias_applyData.call(this, config);
    };

})(ConfigManager);

Original thread: https://forums.rpgmakerweb.com/threads/how-to-detect-system-language.88222/