[Include] [MySQL R41-2] vlang - Create Messages for Multiple Languages
#4

Quote:
Originally Posted by Variable™
View Post
vlang
Initial Release - Last Update: 9/6/2020

Sometimes I wanted to translate my game script in multiple languages and for all the features I have, not for client messages, game texts or dialogues only but for other features, too.

I didn't really try to search for one because I had some ideas and I wanted to apply them nonetheless, so, anyway, I thought to share it with you guys and see what you think about it too.

Additional Requirements Settings

It's pretty much plug and play, just create a MySQL database using the following table structure:
Code:
    Table Structure
        - database
            * Languages
                + ID ( Language ID - Primary key - Auto increment - INT(11) )
                + LanguageName ( The name of created language - Unique key - TINYTEXT(25) )
            * Messages
                + ID ( Message - Primary key - INT(11) )
                + LanguageId ( The language ID that this message is created for - INT(11) )
                + Identifier ( The identifier used to search for the message, short description - VARCHAR(255) )
                + Message ( The actual long message we want to send :) - VARCHAR(1024) )
Created the tables 'Languages' and 'Messages'? You can now configure the include a bit more:
Code:
    Configuration
        If you want to enable per player language functions - define PVLANG before including this.
        You can define MAX_VLANGS and MAX_VMESSAGES as you like, by default they're 100 and 1024 if you don't manually define them before including this.
Functions
Code:
Functions

    - vL_CreateLanguage(MySQL: vLangDB, const LangName[25])
        Description: Create a new language - or load it if already exists
        Parameters:
            vLangDB - Your gamemode's handle for MySQL connection
            LangName - Your langauge name
        Returns:
            -2 if the MySQL handle is invalid
            0 if the number of languages created exceeded the limitation
            1 if else

    - vL_CreateMessage(MySQL: vLangDB, const LangName[25], const Identifier[255], const Message[1024])
        Description: Create a new message for the specified language, or load it if already exists
        Parameters:
            vLangDB - The MySQL handle
            LangName - The Language name to create this message for
            Identifier - The common definition for this message to be used when returning a message
            Message - The long text behind this identifier
        Returns:
            -2 if the MySQL handle is invalid
            -1 if the language ID was not found based on the specified LangName
            1 if else

    - vL_LoadMessages(MySQL: vLangDB, const LangName[25])
        Description: Load all messages found in database for the specified language
        Parameters:
            vLangDB - The MySQL connection handle
            LangName - The language name to load all existing messages for
        Returns:
            -2 if MySQL handle is invalid
            1 if else

    - vL_ReturnMessage(const LangName[25], const Identifier[255], va_args<>)
        Description: Return a message
        Parameters:
            LangName - The language to look for the message in
            Identifier - The identifier of the message
            va_args - If the message has specifiers, add their values here
        Returns:
            The message - null if it fails to find it for any reason

    PVLANG FUNCTIONS:

    - SetPlayerLanguage(playerid, const LangName[25])
        Description: Set the language for said player ID, doesn't need to exist at all

    - GetPlayerLanguage(playerid)
        Description: Returns the language name of this player ID

    INTERNAL:

    - vL_LoadMessage(MySQL: vLangDB, const LangId, const Identifier[255])
        Description: This function is only used internally to load a message created with vL_CreateMessage
Usage

Just read the functions above, you have to create a language first, after your server is connected to MySQL, use your own MySQL handle for the functions above.

You can either manually create languages and messages from the database, if you know what you're doing, that way you don't need the 'vL_CreateMessage' function but simply use 'vL_LoadMessages' for the language you want. If you want the server to do this you need to use 'vL_CreateMessage' after you create the language, and then you can simply use 'vL_ReturnMessage' to return the message.

Example:
Code:
    vL_CreateLanguage(Database, "English");
    vL_CreateMessage(Database, "English", "TEST_IDENT", "Let's count %d %d .. %d!");
    print(vL_ReturnMessage("English", "TEST_IDENT", 1, 2, 10));
Output (if the MySQL handle is valid):
Code:
Let's count 1 2 .. 10!
Repository (GitHub)

Before giving the link, please keep the credits and contribute if you can. Share your opinion in replies below, too.
I thank anyone that I used their work to create this include (MySQL plugin maintainers, YSI maintainers and SA-MP team).

You can find it here https://github.com/h2o-variable/samp...udes/vlang.inc

Thanks!
Why use MySQL for storing messages?
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)