SA-MP Forums Archive
[Tutorial] Language system. (Easy and understandable) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Language system. (Easy and understandable) (/showthread.php?tid=270053)

Pages: 1 2


Language system. (Easy and understandable) - Zh3r0 - 18.07.2011

Language System
How to do it right!

Introduction.
- A Language System is useful when your server is an International one. Players need to understand how to play/how to do this and that in that server, many think making such a system is very hard, well it is very simple, I will explain it step by step. Got inspired to do this tutorial, make it right, since I saw a terrible one that just disgusted me.
NOTE: I'm writing this with my finger bandaged.


Lets begin.

Steps
  1. Creating one variable to stock value that later will correspond to the language.
  2. Create language definitions for a smoother and easier use
  3. Important notice! Setting player's predominant language(Server predominant language)
  4. How to use the created variable.
  5. 3 Custom functions for an easier usage.
    - SetPlayerLanguage(playerid, language);
    - GetPlayerLanguage(playerid);
    - SendLanguageMessage(playerid, leng[], lrom[], lger[], ljpn);
  6. Important notice!

Step 1 - Create the variable to stock language value.

- Now we will create the variable to stock an incremented value that will correspond to the language.

Insert the bellow code
pawn Код:
new pLanguage[MAX_PLAYERS char];
on top of the script, but under
pawn Код:
#include <a_samp>
as we apparently need the MAX_PLAYERS definition but not only this, most of the functions/publics too.
char - It is used to have a less amount of bytes.




Step 2 - Create 4 language definitions for the values of pLanguage variable for an easier use

Insert those definitions somewhere in the script, preferably before the pLanguage variable.
pawn Код:
#define ENG 0
#define ROM 1
#define GER 2
#define JPN 3
As you can see, we defined the values with a text, those will correspond to the right language.
(Wasn't this step so easy?)
NOTE - You might want to use the shortcuts of whatever language you want.



Step 3 - Important notice before making any code to work!
- First of all, the variable we created is a player variable, as you can see we used MAX_PLAYERS definition. Before we continue, we need to decide what is the predominant language, we need to set it when player disconnects.

Add this code under the right public.
pawn Код:
public OnPlayerConnect(playerid)
{
     //Let's say that the predominant language is English.
     pLanguage{playerid} = ENG;
     SendClientMessage(playerid, -1, "The predominant language of this server is: English");
     SendClientMessage(playerid, -1, "To change language use: (/GER - German, /ROM - Romanian, /ENG - Englisb, /JPN - Japanese");
     return 1;
}

Step 4 - Create the 2 functions for an easier use.
- With this function you will can set player's language, or check what language the player has, so you will can send the right message to the right player in the right language.

Put these somewhere in the script, but UNDER pLanguage variable.
pawn Код:
//Get player's current language.
stock GetPlayerLanguage(playerid)
{
    return pLanguage{playerid};
}

//Set player's language.
stock SetPlayerLanguage(playerid, language)
{
    pLanguage{playerid} = language;
}
//(Could have made those 1 line each, but don't want to rush the beginners)



Step 5 - How to use the above 2 functions.



How to set player's current language
- Lets say that when player connects to the server he must first chose his language. WE will not use the dialogs as that seems too complicated since you started this tutorial its clear that you still need practice.
We will use 4 simple commands. (instead of 1 with sscanf and zcmd based, we need KISS)

This code goes straight under OnPlayerCommandText(playerid, cmdtext[]) public.
pawn Код:
if(!strcmp(cmdtext, "/eng"))
{
    SendClientMessage(playerid, -1, "You selected the English(ENG) language, now messages will be written in English");
    pLanguage{playerid} = ENG; //(Now you see why that definition helps?)
    return 1; //Tell the server that the command was performed and ended with success, and avoid ("Unknown Command") message.
}
if(!strcmp(cmdtext, "/ger"))
{
    //****** translated, don't kill me!
    SendClientMessage(playerid, -1, "Sie wдhlte die deutsche (GER) Sprache, nun Nachrichten in deutscher Sprache verfasst werden.");
    pLanguage{playerid} = GER;
    return 1;
}
if(!strcmp(cmdtext, "/rom"))
{
    //Native Language, YOU CAN'T KILL ME FOR THIS!
    SendClientMessage(playerid, -1, "Ai selectat limba Romana(ROM), mesajele vor fi scri scrise in Romana.");
    pLanguage{playerid} = ROM;
    return 1;
}
if(!strcmp(cmdtext, "/jpn"))
{
    //
    SendClientMessage(playerid, -1, "Japanese text here, Japanese text here, Japanese text here, Japanese text here, ");
    pLanguage{playerid} = JPN;
    return 1;
}



How to send language messages.
- When a player for example, types /help, we need to see what language he has, so we can send him the right message in the right language.
- There are 2 ways, simplified and complicated. We go first with the complicated one.

pawn Код:
if(!strcmp(cmdtext, "/help"))
{
    switch(GetPlayerLanguage(playerid))
    {
         case ENG:
         {
              SendClientMessage(playerid, -1, "Welcome to the Help Center");
              SendClientMessage(playerid, -1, "Further help text goes under here.");
         }
         case ROM:
         {
              SendClientMessage(playerid, -1, "Bun venit in centrul de informatii");
              SendClientMessage(playerid, -1, "Mai mult text informativ mai jos...");
         }
         case GER:
         {
              SendClientMessage(playerid, -1, "Willkommen auf der Hilfe-Center.");
              SendClientMessage(playerid, -1, "Weitere Hilfe-Text.");
         }
         case JPN:
         {
              SendClientMessage(playerid, -1, "Anybody knows Japanese?");
              SendClientMessage(playerid, -1, "Hope yes...");
         }
    }
    return 1; //Tell the server the command processed well.
}



How to send language messages with the simplified way.
- First, we need a function to do this, we will create a function that will have 6 parameters, 4 languages + 1 playerid + 1 color parameters.

Place this function near the 2 ones above.
pawn Код:
stock SendLanguageMessage(playerid, color, leng[], lrom[], lger[], ljpn[])
{
    switch(GetPlayerLanguage(playerid))
    {
         case ENG:SendClientMessage(playerid, color, leng);
         case ROM:SendClientMessage(playerid, color, lrom);
         case GER:SendClientMessage(playerid, color, lger);
         case JPN:SendClientMessage(playerid, color, ljpn);
    }
    return 1;
}


Usage of the simplified function to send language messages.
Lets say, again, that the player types the /help command.
Let me show you how to do it.

pawn Код:
if(!strcmp(cmdtext, "/help"))
{
    /*
                          color,        English                   Romanian             German         Japanese */

    SendLanguageMesage(playerid, -1, "Language Help Center", "Centrul de ajutor", "Sprache Hilfe", "Japanese stuff");
    return 1;
}
Easy isn't it?



Problems? Questions? Want an ice-cream? Tell me!


Re: Language system. (Easy and understandable) - Pro SA:MP Craiova - 18.07.2011

Looks very nice !


Re: Language system. (Easy and understandable) - [MG]Dimi - 18.07.2011

Very useful since I saw a lot of players recently asking for this. Good Job! +1


Re: Language system. (Easy and understandable) - JaTochNietDan - 19.07.2011

Wouldn't it be a lot better and cleaner to just use language files? Use some sort of quick ini parser and store the lines in a file for each message that's sent, then just have an array with the paths to the languages files and a variable with a value that corrosponds with the array. Doing all of the switch statements is pretty messy. Also another thing is that with that function it would probably become quite easy to go over the line length limit.

In fact you don't even need to use files, just have a multi-dimensional array with the first cell set being the message and the second cell set being the language. What I mean is this:

pawn Код:
new languages[][][] = {
    {{"Hello"}, {"Hola"}, {"Guten tag"}},
    {{"Yes"}, {"Sн"}, {"Ja"}}
};

new pLanguage[MAX_PLAYERS];

stock SendLanguageMessage(messageid)
{
    SendClientMessage(playerid, 0xFFFFFF, languages[messageid][pLanguage[playerid]]);
    return 1;
}
So then for example SendLanguageMessage(0) would send either Hello, Hola or Guten tag, depending on what the variable is set to for that player.

Not to hijack your tutorial or anything, just a thought, it cuts out all of these switch statements and super long functions.


Re: Language system. (Easy and understandable) - Zh3r0 - 19.07.2011

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Wouldn't it be a lot better and cleaner to just use language files? Use some sort of quick ini parser and store the lines in a file for each message that's sent, then just have an array with the paths to the languages files and a variable with a value that corrosponds with the array. Doing all of the switch statements is pretty messy. Also another thing is that with that function it would probably become quite easy to go over the line length limit.

In fact you don't even need to use files, just have a multi-dimensional array with the first cell set being the message and the second cell set being the language. What I mean is this:

pawn Код:
new languages[][][] = {
    {{"Hello"}, {"Hola"}, {"Guten tag"}},
    {{"Yes"}, {"Sн"}, {"Ja"}}
};

new pLanguage[MAX_PLAYERS];

stock SendLanguageMessage(messageid)
{
    SendClientMessage(playerid, 0xFFFFFF, languages[messageid][pLanguage[playerid]]);
    return 1;
}
So then for example SendLanguageMessage(0) would send either Hello, Hola or Guten tag, depending on what the variable is set to for that player.

Not to hijack your tutorial or anything, just a thought, it cuts out all of these switch statements and super long functions.
That's a nice piece of information, but I just wanted to keep it simple stupid
Thanks for the info (3D Arrays are so weird xD)


Re: Language system. (Easy and understandable) - Lorenc_ - 19.07.2011

Such short code like:
pawn Код:
stock GetPlayerLanguage(playerid)
{
    return pLanguage{playerid};
}
Can be made with a simple define lol

pawn Код:
#define GetPlayerLanguage(%1)         (pLanguage{%1})// Not tested!
Looks hot though, great for people that don't have a clue to this =D


Re: Language system. (Easy and understandable) - !!--Ryder_RO--!! - 19.07.2011

you had written something no joke
Bravo Zh3r0
The strong Roman Scripter!


Re: Language system. (Easy and understandable) - Y_Less - 19.07.2011

Quote:
Originally Posted by JaTochNietDan
View Post
Wouldn't it be a lot better and cleaner to just use language files? Use some sort of quick ini parser and store the lines in a file for each message that's sent, then just have an array with the paths to the languages files and a variable with a value that corrosponds with the array. Doing all of the switch statements is pretty messy. Also another thing is that with that function it would probably become quite easy to go over the line length limit.

In fact you don't even need to use files, just have a multi-dimensional array with the first cell set being the message and the second cell set being the language. What I mean is this:

Code:
new languages[][][] = {
	{{"Hello"}, {"Hola"}, {"Guten tag"}},
	{{"Yes"}, {"Sн"}, {"Ja"}}
};

new pLanguage[MAX_PLAYERS];

stock SendLanguageMessage(messageid)
{
	SendClientMessage(playerid, 0xFFFFFF, languages[messageid][pLanguage[playerid]]);
	return 1;
}
So then for example SendLanguageMessage(0) would send either Hello, Hola or Guten tag, depending on what the variable is set to for that player.

Not to hijack your tutorial or anything, just a thought, it cuts out all of these switch statements and super long functions.
This (the INI method) is what YSI has been doing for about 4 years now!


Re: Language system. (Easy and understandable) - JaTochNietDan - 19.07.2011

Quote:
Originally Posted by Zh3r0
Посмотреть сообщение
That's a nice piece of information, but I just wanted to keep it simple stupid
Thanks for the info (3D Arrays are so weird xD)
I don't know but your way looks more complicated to me, in a messy sense


Re: Language system. (Easy and understandable) - Zh3r0 - 19.07.2011

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
I don't know but your way looks more complicated to me, in a messy sense
Yeah but nobody knows about 3D arrays since they are actually viewing this tutorial.
I thought an easy function like that should do it...


Re: Language system. (Easy and understandable) - JaTochNietDan - 19.07.2011

Quote:
Originally Posted by Zh3r0
Посмотреть сообщение
Yeah but nobody knows about 3D arrays since they are actually viewing this tutorial.
I thought an easy function like that should do it...
Isn't that the point of a tutorial? To teach them stuff they don't know or understand?


Re: Language system. (Easy and understandable) - Zh3r0 - 19.07.2011

Quote:
Originally Posted by JaTochNietDan
View Post
Isn't that the point of a tutorial? To teach them stuff they don't know or understand?
True, oh well, I'll leave it as that, next time, I'll use my knowledge wiser


Re: Language system. (Easy and understandable) - Y_Less - 19.07.2011

Quote:
Originally Posted by Zh3r0
View Post
True, oh well, I'll leave it as that, next time, I'll use my knowledge wiser
Why not edit this one? Are you likely to ever write another tutorial on multi-language systems?


Re: Language system. (Easy and understandable) - FANEX - 21.07.2011

Pfuuu ;x
Thanks alot.


Re: Language system. (Easy and understandable) - Venice - 22.07.2011

Nice tutorial


Re: Language system. (Easy and understandable) - Zh3r0 - 22.07.2011

Quote:
Originally Posted by costel_nistor96
View Post
You don't knew to do this ? =] ... ( esti praf, doar gura e de tine )

Nice tutorial Zh3r0.
Keep it quiet, remember when you were a noob too? Remember? No? You should.
FYI his English is way better than yours


Re: Language system. (Easy and understandable) - Mean - 24.07.2011

I've already wrote a tutorial for this, but nice.


Re: Language system. (Easy and understandable) - Zh3r0 - 25.07.2011

Quote:
Originally Posted by Mean
View Post
I've already wrote a tutorial for this, but nice.
Looked into your posts, can't find it at all!


Re: Language system. (Easy and understandable) - Guest3598475934857938411 - 25.07.2011

Zh3r0, nice tutorial. Someday this might help me of wasting time coding my own


AW: Language system. (Easy and understandable) - Tigerkiller - 28.07.2011

how to make an include with this ?