Raised This Month: $12 Target: $400
 3% 

Function to check if translation exists


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joao7yt
Senior Member
Join Date: Nov 2014
Location: Brazil
Old 06-25-2017 , 02:46   Function to check if translation exists
Reply With Quote #1

Version 1

Version 2


The code will cache the valid translations in an array. Then it will simply check if that translation is available in the array in the client language, server language or English.
It's coded using SMC Parser, which will prevent any bugs related with multi-line comments in the file.
PHP Code:
Handle langs INVALID_HANDLE;
Handle transid INVALID_HANDLE;
Handle translang INVALID_HANDLE;

char transidbuffer[64];
bool parsefirstsection;
bool getlangs;
bool parsecontains[16];

public 
void OnPluginStart()
{
    
langs CreateArray(16);
    
transid CreateArray(64);
    
translang CreateArray(16);
    
    
LoadTranslations("common.phrases");
    
LoadTranslationsEx("common.phrases");
}

stock bool TranslationExists(int client, const char[] phrase)
{
    
int langservernum GetServerLanguage();
    
int langnum;
    if (
client == 0)
    {
        
langnum GetServerLanguage();
    }
    else
    {
        
langnum GetClientLanguage(client);
    }
    
    
char langservercode[16];
    
char langcode[16];
    
GetLanguageInfo(langservernumlangservercodesizeof(langservercode));
    
GetLanguageInfo(langnumlangcodesizeof(langcode));
    
    for (
int i 0GetArraySize(transid); i++)
    {
        
char buffer1[64];
        
GetArrayString(transidibuffer1sizeof(buffer1));
        
        if (
StrEqual(phrasebuffer1))
        {
            
char buffer2[64];
            
GetArrayString(translangibuffer2sizeof(buffer2));
            
            if (
StrEqual(langcodebuffer2) || StrEqual("en"buffer2) || StrEqual(langservercodebuffer2))
            {
                return 
true;
            }
        }
    }
    
    return 
false;
}

stock void LoadTranslationsEx(const char[] file)
{
    
char cfgFile[PLATFORM_MAX_PATH];
    
char dirName[PLATFORM_MAX_PATH];
    
    
BuildPath(Path_SMdirNamePLATFORM_MAX_PATH"configs");
    
BuildPath(Path_SMcfgFilePLATFORM_MAX_PATH"configs/languages.cfg");
    
    if (
DirExists(dirName) && FileExists(cfgFile))
    {
        
Handle smc SMC_CreateParser();
        
        
parsefirstsection true;
        
getlangs true;
        
        
SMC_SetReaders(smcNewSectionKeyValueEndSection);
        
SMC_ParseFile(smccfgFile);
        
        
CloseHandle(smc);
    }
    
    for (
int i 0GetArraySize(langs); i++)
    {
        
char buffer[64];
        
GetArrayString(langsibuffersizeof(buffer));
        
        if (
StrEqual(buffer"en"false))
        {
            
BuildPath(Path_SMdirNamePLATFORM_MAX_PATH"translations");
            
BuildPath(Path_SMcfgFilePLATFORM_MAX_PATH"translations/%s.txt"file);
            
            if (
DirExists(dirName) && FileExists(cfgFile))
            {
                
Handle smc SMC_CreateParser();
                
                
parsefirstsection true;
                
                
SMC_SetReaders(smcNewSectionKeyValueEndSection);
                
SMC_ParseFile(smccfgFile);
                
                
CloseHandle(smc);
            }
        }
        else
        {
            
BuildPath(Path_SMdirNamePLATFORM_MAX_PATH"translations/%s"buffer);
            
BuildPath(Path_SMcfgFilePLATFORM_MAX_PATH"translations/%s/%s.txt"bufferfile);
            
            if (
DirExists(dirName) && FileExists(cfgFile))
            {
                
Handle smc SMC_CreateParser();
                
                
parsefirstsection true;
                
                
SMC_SetReaders(smcNewSectionKeyValueEndSection);
                
SMC_ParseFile(smccfgFile);
                
                
CloseHandle(smc);
            }
        }
    }
}

stock SMCResult NewSection(Handle parser, const char[] namebool quotes)
{
    for (
int i 1sizeof(parsecontains); i++)
    {
        
parsecontains[i] = false;
    }
    
    if (
getlangs)
    {
        if (!
StrEqual(name"Languages"))
        {
            
getlangs false;
            return 
SMCParse_HaltFail;
        }
        else
        {
            return 
SMCParse_Continue;
        }
    }
    else
    {
        if (
parsefirstsection)
        {
            if (!
StrEqual(name"Phrases"))
            {
                return 
SMCParse_HaltFail;
            }
            else
            {
                
parsefirstsection false;
                return 
SMCParse_Continue;
            }
        }
    }
    
    
Format(transidbuffersizeof(transidbuffer), name);
    return 
SMCParse_Continue;
}

stock SMCResult KeyValue(Handle parser, const char[] key, const char[] valuebool key_quotesbool value_quotes)
{
    if (
getlangs)
    {
        for (
int i 0GetArraySize(langs); i++)
        {
            
char buffer[16];
            
GetArrayString(langsibuffersizeof(buffer));
            if (
StrEqual(keybufferfalse))
            {
                return 
SMCParse_Continue;
            }
        }
        
        
PushArrayString(langskey);
        return 
SMCParse_Continue;
    }
    else
    {
        if (
StrEqual(key"#format"false))
        {
            for (
int i 1sizeof(parsecontains); i++)
            {
                
char buffer[4];
                
Format(buffersizeof(buffer), "{%d:"i);
                if (
StrContains(valuebuffer) != -1)
                {
                    
parsecontains[i] = true;
                }
                else
                {
                    
parsecontains[i] = false;
                }
            }
            return 
SMCParse_Continue;
        }
        else if (
StrEqual(value""))
        {
            return 
SMCParse_Continue;
        }
        
        for (
int i 0GetArraySize(transid); i++)
        {
            
char buffer1[64];
            
GetArrayString(transidibuffer1sizeof(buffer1));
            if (
StrEqual(transidbufferbuffer1))
            {
                
char buffer2[16];
                
GetArrayString(translangibuffer2sizeof(buffer2));
                if (
StrEqual(keybuffer2false))
                {
                    return 
SMCParse_Continue;
                }
            }
        }
        
        for (
int i 1sizeof(parsecontains); i++)
        {
            if (
parsecontains[i])
            {
                
char buffer[4];
                
Format(buffersizeof(buffer), "{%d}"i);
                if (
StrContains(valuebuffer) == -1)
                {
                    return 
SMCParse_Continue;
                }
            }
        }
        
        
PushArrayString(transidtransidbuffer);
        
PushArrayString(translangkey);
        
        return 
SMCParse_Continue;
    }
}

stock SMCResult EndSection(Handle parser)
{
    if (
getlangs)
    {
        
getlangs false;
    }

Call it like this:
PHP Code:
if (TranslationExists(client"Changing map"))
{
    
PrintToChat(client"%t""Changing map""de_dust2");
}
else
{
    
PrintToChat(client"Changing map to %s...""de_dust2");


Last edited by joao7yt; 06-27-2017 at 17:33.
joao7yt is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 06-25-2017 , 12:33   Re: Function to check if translation exists
Reply With Quote #2

This seems pretty broken and super slow (sorry). You absolutely need to use GetClientLanguage as you have this hard coded for English at the moment.
KyleS is offline
joao7yt
Senior Member
Join Date: Nov 2014
Location: Brazil
Old 06-25-2017 , 13:05   Re: Function to check if translation exists
Reply With Quote #3

Quote:
Originally Posted by KyleS View Post
This seems pretty broken and super slow (sorry). You absolutely need to use GetClientLanguage as you have this hard coded for English at the moment.
GetClientLanguage is not necessary since I tested and if a client have a language that isn't English and the phrase in his language is missing, sourcemod tries to get the English phrase, so I'm checking the English files directly.

Last edited by joao7yt; 06-25-2017 at 13:07.
joao7yt is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 06-26-2017 , 21:22   Re: Function to check if translation exists
Reply With Quote #4

What if there is no english phrase in the file but only spanish?
You'd need to search for the file in all language subfolders inside the translations folder too making this check even slower.
__________________

Last edited by Peace-Maker; 06-26-2017 at 21:26.
Peace-Maker is offline
joao7yt
Senior Member
Join Date: Nov 2014
Location: Brazil
Old 06-26-2017 , 21:31   Re: Function to check if translation exists
Reply With Quote #5

Quote:
Originally Posted by Peace-Maker View Post
What if there is no english phrase in the file but only spanish?
idk if sourcemod just search for en phrases or if it searches for the translation file in the main folder, need tests, but I think it's the second option
joao7yt is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 06-26-2017 , 22:36   Re: Function to check if translation exists
Reply With Quote #6

It tries the target's language (assuming %T), then it tries the server language, then it tries English.
Fyren is offline
joao7yt
Senior Member
Join Date: Nov 2014
Location: Brazil
Old 06-27-2017 , 16:34   Re: Function to check if translation exists
Reply With Quote #7

Hey guys, I updated the code, now it will cache the valid translations in an array. And it will simply check in that array if the translation is available in the client language, server language or English. Do you think it's less slow and less broken right now? Give me your feed back.
joao7yt is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 11-21-2017 , 22:46   Re: Function to check if translation exists
Reply With Quote #8

For those who stumble upon this in the future, and those who don't know about it yet: Latest SourceMod 1.9 builds now support two new functions in order to detect if a translation phrase exists. Here's a snippet from the include.

PHP Code:
/**
 * Determines if the specified phrase exists within the plugin's
 * translation cache.
 *
 * @param phrase    Phrase to look for.
 * @return            True if phrase exists.
 */
native bool TranslationPhraseExists(const char[] phrase);

/**
 * Determines if a there is a translation for the speicifed language.
 *
 * @param phrase    Phrase to check.
 * @param language    Language number.
 * @return            True if translation exists.
 */
native bool IsTranslatedForLanguage(const char[] phraseint language); 

Last edited by headline; 11-21-2017 at 22:48.
headline is offline
joao7yt
Senior Member
Join Date: Nov 2014
Location: Brazil
Old 11-22-2017 , 03:31   Re: Function to check if translation exists
Reply With Quote #9

Quote:
Originally Posted by Headline View Post
For those who stumble upon this in the future, and those who don't know about it yet: Latest SourceMod 1.9 builds now support two new functions in order to detect if a translation phrase exists. Here's a snippet from the include.

PHP Code:
/**
 * Determines if the specified phrase exists within the plugin's
 * translation cache.
 *
 * @param phrase    Phrase to look for.
 * @return            True if phrase exists.
 */
native bool TranslationPhraseExists(const char[] phrase);

/**
 * Determines if a there is a translation for the speicifed language.
 *
 * @param phrase    Phrase to check.
 * @param language    Language number.
 * @return            True if translation exists.
 */
native bool IsTranslatedForLanguage(const char[] phraseint language); 
That’s really great!
joao7yt is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:10.


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