AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Colors in translation file (https://forums.alliedmods.net/showthread.php?t=314844)

Dragokas 03-09-2019 13:42

Colors in translation file
 
1 Attachment(s)
By request.

Stock:

PHP Code:

/**
*   @note Used for in-line string translation.
*
*   @param  iClient     Client Index, translation is apllied to.
*   @param  format      String formatting rules. By default, you should pass at least "%t" specifier.
*   @param  ...            Variable number of format parameters.
*   @return char[192]    Resulting string. Note: output buffer is hardly limited.
*/
stock char[] Translate(int iClient, const char[] formatany ...)
{
    
char buffer[192];
    
SetGlobalTransTarget(iClient);
    
VFormat(buffersizeof(buffer), format3);
    return 
buffer;
}

/**
*   @note Prints a message to a specific client in the chat area. Supports named colors in translation file.
*
*   @param  iClient     Client Index.
*   @param  format        Formatting rules.
*   @param  ...            Variable number of format parameters.
*   @no return
*/
stock void CPrintToChat(int iClient, const char[] formatany ...)
{
    
char buffer[192];
    
SetGlobalTransTarget(iClient);
    
VFormat(buffersizeof(buffer), format3);
    
ReplaceColor(buffersizeof(buffer));
    
PrintToChat(iClient"\x01%s"buffer);
}

/**
*   @note Prints a message to all clients in the chat area. Supports named colors in translation file.
*
*   @param  format        Formatting rules.
*   @param  ...            Variable number of format parameters.
*   @no return
*/
stock void CPrintToChatAll(const char[] formatany ...)
{
    
char buffer[192];
    for( 
int i 1<= MaxClientsi++ )
    {
        if( 
IsClientInGame(i) && !IsFakeClient(i) )
        {
            
SetGlobalTransTarget(i);
            
VFormat(buffersizeof(buffer), format2);
            
ReplaceColor(buffersizeof(buffer));
            
PrintToChat(i"\x01%s"buffer);
        }
    }
}

/**
*   @note Prints a hint message to all clients. Supports individual string translation for each client.
*
*   @param  format        Formatting rules.
*   @param  ...            Variable number of format parameters.
*   @no return
*/
stock void CPrintHintTextToAll(const char[] formatany ...)
{
    
char buffer[192];
    for( 
int i 1<= MaxClientsi++ )
    {
        if( 
IsClientInGame(i) && !IsFakeClient(i) )
        {
            
SetGlobalTransTarget(i);
            
VFormat(buffersizeof(buffer), format2);
            
PrintHintText(ibuffer);
        }
    }
}

/**
*   @note Prints a center screen message to all clients. Supports individual string translation for each client.
*
*   @param  format        Formatting rules.
*   @param  ...            Variable number of format parameters.
*   @no return
*/
stock void CPrintCenterTextAll(const char[] formatany ...)
{
    
char buffer[192];
    for( 
int i 1<= MaxClientsi++ )
    {
        if( 
IsClientInGame(i) && !IsFakeClient(i) )
        {
            
SetGlobalTransTarget(i);
            
VFormat(buffersizeof(buffer), format2);
            
PrintCenterText(ibuffer);
        }
    }
}

/**
*   @note Converts named color to control character. Used internally by string translation functions.
*
*   @param  char[]        Input/Output string for convertion.
*   @param  maxLen        Maximum length of string buffer (includes NULL terminator).
*   @param  team         Team number to replace {teamcolor} with appropriate color.
*   @no return
*/
void ReplaceColor(char[] messageint maxLenint team 0)
{
    
ReplaceString(messagemaxLen"{normal}""\x01"false);
    
ReplaceString(messagemaxLen"{default}""\x01"false);
    
ReplaceString(messagemaxLen"{white}""\x01"false);
    
ReplaceString(messagemaxLen"{darkred}""\x02"false);
    switch(
team)
    {
        case 
ReplaceString(messagemaxLen"{teamcolor}""\x0B"false);
        case 
ReplaceString(messagemaxLen"{teamcolor}""\x05"false);
        default: 
ReplaceString(messagemaxLen"{teamcolor}""\x01"false);
    }
    
ReplaceString(messagemaxLen"{pink}""\x03"false);
    
ReplaceString(messagemaxLen"{green}""\x04"false);
    
ReplaceString(messagemaxLen"{highlight}""\x04"false);
    
ReplaceString(messagemaxLen"{yellow}""\x05"false);
    
ReplaceString(messagemaxLen"{lightgreen}""\x05"false);
    
ReplaceString(messagemaxLen"{lime}""\x06"false);
    
ReplaceString(messagemaxLen"{lightred}""\x07"false);
    
ReplaceString(messagemaxLen"{red}""\x07"false);
    
ReplaceString(messagemaxLen"{gray}""\x08"false);
    
ReplaceString(messagemaxLen"{grey}""\x08"false);
    
ReplaceString(messagemaxLen"{olive}""\x09"false);
    
ReplaceString(messagemaxLen"{orange}""\x10"false);
    
ReplaceString(messagemaxLen"{silver}""\x0A"false);
    
ReplaceString(messagemaxLen"{lightblue}""\x0B"false);
    
ReplaceString(messagemaxLen"{blue}""\x0C"false);
    
ReplaceString(messagemaxLen"{purple}""\x0E"false);
    
ReplaceString(messagemaxLen"{darkorange}""\x0F"false);


For Left4dead only


Using:
PHP Code:

#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

public void OnPluginStart()
{
    
LoadTranslations("test.phrases");
    
    
RegConsoleCmd("sm_test"CmdTest);
}

public 
Action CmdTest(int clientint args)
{
    
char sUser[64];
    
GetClientName(clientsUsersizeof(sUser));
    
    
CPrintToChat(client"%t""alias-client"sUser);
    
CPrintToChatAll("%t""alias-all");
    
    return 
Plugin_Handled;


Translation file example:
addons/sourcemod/translations/test.phrases.txt
Code:

/*
        Following named colors are supported:
        - {white}        (use instead of \x01 )
        - {cyan}        (use instead of \x03 )
        - {orange}        (use instead of \x04 )
        - {green}        (use instead of \x05 )
*/

"Phrases"
{
        "alias-client"
        {
                "#format"        "{1:s}"
                "ru"                "{cyan}Привет {orange}{1}!"
                "en"                "{cyan}Hello {orange}{1}!"
        }
        "alias-all"
        {
                "ru"                "{cyan}Привет всем!"
                "en"                "{cyan}Hello everybody!"
        }
}

ReplaceColor() is a partial copy-paste (I don't remember credits, sorry).

Dragokas 05-06-2019 08:29

Re: Colors in translation file
 
Appended with:

CPrintCenterTextAll()
CPrintHintTextToAll()
Translate()

and description.


All times are GMT -4. The time now is 23:25.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.