AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Function to check if translation exists (https://forums.alliedmods.net/showthread.php?t=298883)

joao7yt 06-25-2017 02:46

Function to check if translation exists
 
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");



KyleS 06-25-2017 12:33

Re: Function to check if translation exists
 
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.

joao7yt 06-25-2017 13:05

Re: Function to check if translation exists
 
Quote:

Originally Posted by KyleS (Post 2531634)
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.

Peace-Maker 06-26-2017 21:22

Re: Function to check if translation exists
 
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.

joao7yt 06-26-2017 21:31

Re: Function to check if translation exists
 
Quote:

Originally Posted by Peace-Maker (Post 2531932)
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

Fyren 06-26-2017 22:36

Re: Function to check if translation exists
 
It tries the target's language (assuming %T), then it tries the server language, then it tries English.

joao7yt 06-27-2017 16:34

Re: Function to check if translation exists
 
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.

headline 11-21-2017 22:46

Re: Function to check if translation exists
 
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); 


joao7yt 11-22-2017 03:31

Re: Function to check if translation exists
 
Quote:

Originally Posted by Headline (Post 2562086)
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!


All times are GMT -4. The time now is 17:20.

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