1

So, I'm not sure if this has been done before. I have just installed a PHP application on my server and I want to translate the site.

The application uses a language_name.php file for each language. Each language file contains a multi-dimensional array $_LANG.

$_LANG keys range from one level, $_LANG['l1'] = "string"; to about 5 levels, $_LANG['l1']['l2']['l3']['l4']['l5'] = "string";

WHAT I WANT TO ATTEMPT

To create nested foreach loops to loop through and echo the $key - $value pairs. After which I will write a translation function to translate the values before the echo..

THE PROBLEM

  • The original file contains more than 3,750 lines.

  • My echoed version only contains 3,387.


    <?php

    $_LANG['lang']['vars']['go'];
    $_LANG['at']['the'];
    $_LANG['top'];,

    foreach ($_LANG as $key => $value) {
        if (is_array($value)) {
        
            foreach ($value as $key1 => $value1) {            
                if (is_array($value1)) {
                
                    foreach ($value1 as $key2 => $value2) {
                        if (is_array($value2)) {
                        
                            foreach ($value2 as $key3 => $value3) {
                                if (is_array($value3)) {
                                
                                    foreach ($value3 as $key4 => $value4) {
                                        if (is_array($value4)) {
                                        
                                            foreach ($value4 as $key5 => $value5) {
                                                echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'][\''.$key3.'\'][\''.$key4.'\'][\''.$key5.'\'] = "'.$value5.'";'.'<br>';
                                            }
                                        }
                                        else {
                                            echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'][\''.$key3.'\'][\''.$key4.'\'] = "'.$value4.'";'.'<br>';
                                        }
                                    }
                                }
                                else {
                                    echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'][\''.$key3.'\'] = "'.$value3.'";'.'<br>';
                                }
                            }
                        }
                        else {
                            echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'] = "'.$value2.'";'.'<br>';
                        }
                    }
                }
                else {
                    echo '$_LANG[\''.$key.'\'][\''.$key1.'\'] = "'.$value1.'";'.'<br>';
                }
            }
        }
        else {
            echo '$_LANG[\''.$key.'\'] = "'.$value.'";'.'<br>';
        }
    }

    ?>

1 Answers1

0

So, I have searched around on the internet and solved my own problems.

I decided to stick with the foreach() loop and used the count() function to see if I was echoing out the correct number of variables. After verifying that it was indeed returning all the key|value pairs, I moved on the the actual translating... More workarounds...

I found a Git Repo Git Repo that would allow me to use the Google Translate service for free, at which point I modified the foreach() loop to something like this.

foreach ($_LANG as $key => $value) {
    if (is_array($value)) {
        
        foreach ($value as $key1 => $value1) {            
            if (is_array($value1)) {
                
                foreach ($value1 as $key2 => $value2) {
                    if (is_array($value2)) {
                        
                        foreach ($value2 as $key3 => $value3) {
                            if (is_array($value3)) {
                                
                                foreach ($value3 as $key4 => $value4) {
                                    if (is_array($value4)) {
                                        
                                        foreach ($value4 as $key5 => $value5) {
                                            echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'][\''.$key3.'\'][\''.$key4.'\'][\''.$key5.'\'] = "'.$trans->translate('en', 'vi', $value5).'";'.'<br>';
                                        }
                                    }
                                    else {
                                        echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'][\''.$key3.'\'][\''.$key4.'\'] = "'.$trans->translate('en', 'vi', $value4).'";'.'<br>';
                                    }
                                }

                            }
                            else {
                                echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'][\''.$key3.'\'] = "'.$trans->translate('en', 'vi', $value3).'";'.'<br>';
                            }
                        }

                    }
                    else {
                        echo '$_LANG[\''.$key.'\'][\''.$key1.'\'][\''.$key2.'\'] = "'.$trans->translate('en', 'vi', $value2).'";'.'<br>';
                    }
                }

            }
            else {
                echo '$_LANG[\''.$key.'\'][\''.$key1.'\'] = "'.$trans->translate('en', 'vi', $value1).'";'.'<br>';
            }
        }

    }
    else {
        echo '$_LANG[\''.$key.'\'] = "'.$trans->translate('en', 'vi', $value).'";'.'<br>';
    }
}

However, I was only able to receive about 150 key|value pairs back at a time as the Google Translate service has numerous limitations, and therefore I had to change my IP address with a VPN on every request.

I know this is not the ideal way to translate your site content.

  • Copying and pasting 3700 values is definitely not an option.
  • Also definitely not paying for the Google Cloud Translate API.