[Tutorial] Supporting Multiple Languages
#1

Introduction

Although I recommend the use of the YSI Library y_language (I believe that's what it's called, but I cannot find a link at the moment), I personally use a different system to manage different languages within my server. It's simple, and to the point, and only involves two (2) dialogs, one (1) player-variable, and one (1) stock.

Along with this tutorial, comes the availability of a new function, SendMessage;

pawn Код:
SendMessage(playerid, colour, Language1[], Language2[])
Now, this function is rather basic, and for my needs, I edit it to be like so:

pawn Код:
SendMessage(playerid, colour, English[], Spanish[])
Due to the fact that I use only two languages on my server, English, and Spanish.

Let's get started..

First of all, create a variable at the top of your script (a 'GLOBAL' variable - global being in quotes due to the fact that it is independent for each user.)

Create it like so:
pawn Код:
new
    player_Language[MAX_PLAYERS] = -1;
    /*
        -1, in this example, is associated with NULL, meaning that there is no language selected at the moment.
            Useful for detecting if a player has selected their language or not.

        0, in this example, will be associated with English. As English is the primary language of this community, why not make it ID 0 so it's first on the list?

        1 will be associated with Spanish, our secondary language for our community.
    */
Second, you're going to need to create the function explained above, which is the main purpose of this tutorial - SendMessage. To do this, go OUTSIDE of a callback in your script, and copy-paste this function. (I will explain further in comments next to the code)

pawn Код:
stock SendMessage(playerid, colour, Language1[], Language2[])
{
    switch( player_Language[playerid] )
    {
        case 0: //Language 1
        {
            SendClientMessage(playerid, colour, Language1);
        }
        case 1: //Language 2
        {
            SendClientMessage(playerid, colour, Language2);
        }
         /*
       case 2: //Language 3
        {
            SendClientMessage(playerid, colour, Language3);
        }
        Our header ( stock SendMessage(...) ) will also read:
                    stock SendMessage(playerid, colour, Language1[], Language2[], Language3[]);
        If you wished to add another language.
        */

    }
    return true;
}
Of course this code could also be compressed like so, to save lines:

pawn Код:
stock SendMessage(playerid, colour, Language1[], Language2[]) {
    switch( player_Language[playerid] ) {
        case 0: {   SendClientMessage(playerid, colour, Language1); }
        case 1: {   SendClientMessage(playerid, colour, Language2); }
    }
    return true;
}
The usage for this function is like so: (I'm using an example from my mode..)

pawn Код:
SendMessage(playerid, -1, ""#CYAN"[Success] "#WHITE"You have reloaded the MOTD!", ""#CYAN"[Йxito] "#WHITE"Usted ha recargado el MOTD!");
See how easy that is? And it even supports hex colors in the message! (I don't see why it wouldn't, but it sounds good)

Now, onto the dialog! (This is optional, and below, I will teach you how to create this in command form aswell!)

First off, I like the define the dialog IDs at the top of the script to avoid overlapping dialog IDs.
(I assume you know how to define a 'string' as an ID)
pawn Код:
#define         DIALOG_LANGUAGE             1
#define             DIALOG_LANGUAGE_SUCCESS     2
NOTE: You will probably have to edit these dialog IDs, as this is not a purely copy-paste tutorial.

The next part is completely dependent on preference. I, personally, like to have them select their language when they connect, so.. under the callback OnPlayerConnect, place the following code: (Can also be placed inside of login, register, and many other incidents of code)

For me, instead of having to go to every single incidence that I allow them to change their language, I created a define at the top of my script to define the available languages..

pawn Код:
#define         LANGUAGE_SELECTION          "English\nSpanish"
pawn Код:
if( player_Language[playerid] == -1) {
            ShowPlayerDialog(playerid, DIALOG_LANGUAGE, DIALOG_STYLE_LIST, ""#CYAN"Select your language "#WHITE"(Seleccione su idioma)", LANGUAGE_SELECTION, "OK", "");
}
Now, of course, this code is edited to my server. Since the language hasn't been selected yet, I have chosen to display the messages in both languages (English in CYAN (a light blue) and Spanish in WHITE (well... white)).
You can change this accordingly, as it is YOUR server.

Now to make the dialogs themselves actually work. Do this by going under your OnDialogResponse callback, which is called every time a player hits a button on their dialog. (response || (OR) !response)

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch( dialogid )
    {
        case DIALOG_LANGUAGE:
        {
            if( response ) //They clicked 'OK'
            {
                switch( listitem ) //Checks through the available languages (Defined above)
                {
                    case 0: //English
                    {
                        player_Language[playerid] = 0;
                        ShowPlayerDialog(playerid, DIALOG_LANGUAGE_SUCCESS, DIALOG_STYLE_MSGBOX, ""#CYAN"You have successfully set your language!", ""#WHITE"Your account has been set to "#CYAN"english!\n"#WHITE"If you wish to change this, type "#CYAN"/changelanguage "#WHITE"at any time.", "OK", "");
                    }
                    case 1:
                    {
                        player_Language[playerid] = 1;
                        ShowPlayerDialog(playerid, DIALOG_LANGUAGE_SUCCESS, DIALOG_STYLE_MSGBOX, ""#CYAN"Ha configurado correctamente su idioma!", ""#WHITE"Su cuenta ha sido establecido en "#CYAN"inglйs!\n"#WHITE"Si desea cambiar esta "#CYAN"/changelanguage "#WHITE"el tipo en cualquier momento.", "Bueno", "");
                    }
                }
            }
            else return ShowPlayerDialog(playerid, DIALOG_LANGUAGE, DIALOG_STYLE_LIST, ""#CYAN"Select your language "#WHITE"(Seleccione su idioma)", LANGUAGE_SELECTION, "OK", ""); //If they hit ESC while on the dialog (trying to bug the system?)
        }
    }
    return true;
}
I didn't really comment the above code as it's pretty self explainatory, but if you don't understand it, feel free to comment below, and I will explain it in further detail.

All done!

If you've followed all the steps correctly, you will have created a language "system", although by request I could create more functions to go along with this. The reason I didn't before was due to efficiency.

Processing:
pawn Код:
if(player_Language[playerid] == 1) { //english
Is faster than:
pawn Код:
if(GetPlayerLanguage(playerid) == English) {
If you would like the function however, here you are:

pawn Код:
stock GetPlayerLanguage ( playerid )
{
    return player_Language[playerid];
    // Returns 0 for English, and 1 for Spanish.
}
You're now on your way to creating a multi-language gamemode!
Well, have a nice day, and I hope you have learned something!
Reply
#2

Nice tut, thanks.
Reply
#3

Quote:
Originally Posted by Nonameman
Посмотреть сообщение
Nice tut, thanks.
Not a problem, did you understand everything fine, or did you find any areas I could improve upon?

#EDITED MY POST: Added the Dialog section.
Reply
#4

Edited this tutorial despite its age due to the fact that I believe it's a good tutorial, and useful. Fixed some errors that nobody mentioned aswell. Thanks.
Reply
#5

This tut shows you how to make a language system then once they choose spanish will they see every text and stuff in spanish or do i have to also make spanish texts
Reply
#6

It would be better if you used y_languages, however neat and nice work.
Reply
#7

pawn Код:
new
    player_Language[MAX_PLAYERS] = -1;
Is here all cells becoming -1 or just the first?
Reply
#8

Quote:
Originally Posted by Roko_foko
Посмотреть сообщение
pawn Код:
new
    player_Language[MAX_PLAYERS] = -1;
Is here all cells becoming -1 or just the first?
When they join the server, unless it's defined as something else, it automatically becomes -1.
Reply
#9

Nice tut .
Reply
#10

Wow this is a nice tut!
Reply
#11

What about formatted messages? How can I make it to work on multiple languages?
Please answer. Thanks!
Reply
#12

Nice tutorial, Good for servers with many languages!
Reply
#13

Quote:
Originally Posted by sgtjones12
Посмотреть сообщение
This tut shows you how to make a language system then once they choose spanish will they see every text and stuff in spanish or do i have to also make spanish texts
Yes, you have to script your GM in two languages, it wont translate itself.
Reply
#14

Quote:
Originally Posted by CONTROLA
View Post
What about formatted messages? How can I make it to work on multiple languages?
Please answer. Thanks!
Simple!

pawn Code:
new enString[128], spString[128], plName[MAX_PLAYER_NAME];

GetPlayerName( playerid, plName, MAX_PLAYER_NAME );

format( enString, sizeof(enString), "Hello %s!", plName );
format( spString, sizeof(spString), "Hola %s!", plName );
SendMessage( playerid, 0xFFFFFFFF, enString, spString );
Reply
#15

Nice tut ill use it
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)