Home > OS >  Translate PHP language file
Translate PHP language file

Time:11-20

I have the following file format :

$language['exemple1']['address_postal']                       = "Postal code";
$language['exemple2']['address_city']                         = "City";
$language['exemple3']['hello']                                = "Hello %s :)";
$language['exemple4']['custom']                               = "Hello \"%s\"  :)
<br/> hope you are doing<b style=\"font-size:5px\">%s </b>
";

I need to translate the "right part" ex: = "my translation"; in several languages, as you can see in the exemple4 it looks hard to translate such a file.

I thought about translating the entire document with DeepL for instance then using a regex to replace the left-part $language[][]... which will be translated by the non-translated left-part so I can use it, but I don't know how to "fetch" the left part with regex

CodePudding user response:

Not pretty but i think i know what you want.
I guess you got a lot of lines|files. So this could be an easy solution.

You may need to play a little with the map.
You need to find some placeholders that will not get touched in the translation.
F.e. { got "lost in translation" when i tried it out.

Have fun =)

// Load PHP file into string.
$string = file_get_contents('tmp/array.php');
// Remove <?php
$parts = explode('<?php', $string);
// Get array code-string without leading and trailing line breaks.
$string = trim($parts[1], PHP_EOL);

// Create a map of characters that need to be replaced before translation.
$map = [
    '\\' => '##BACKSLASH##',
    '/'  => '##SLASH##',
    // add more if you have to
    // Note: i tried {..} and {{..}} and those get "lost in translation" :)
];
// Replace characters with placeholders.
$string = str_replace(array_keys($map), array_values($map), $string);
echo $string . PHP_EOL;
// $language['exemple1']['address_postal']                       = "Postal code";
// $language['exemple2']['address_city']                         = "City";
// $language['exemple3']['hello']                                = "Hello %s :)";
// $language['exemple4']['custom']                               = "Hello ##BACKSLASH##"%s##BACKSLASH##"  :)
//     <br##SLASH##> hope you are doing<b style=##BACKSLASH##"font-size:5px##BACKSLASH##">%s <##SLASH##b>
// ";

// Translated to:
// $language['exemple1']['address_postal'] = "Postleitzahl";
// $language['exemple2']['address_city'] = "Stadt";
// $language['exemple3']['hello'] = "Hallo %s :)";
// $language['exemple4']['custom'] = "Hallo ##BACKSLASH##"%s##BACKSLASH##" :)
//     <br##SLASH##> ich hoffe, es geht Ihnen gut<b style=##BACKSLASH##"font-size:5px##BACKSLASH##">%s <##SLASH##b>
// ";

// Put translated string into another file and replace back.
$string = file_get_contents('tmp/translated.txt');
$string = str_replace(array_values($map), array_keys($map), $string);
echo $string . PHP_EOL;
// $language['exemple1']['address_postal'] = "Postleitzahl";
// $language['exemple2']['address_city'] = "Stadt";
// $language['exemple3']['hello'] = "Hallo %s :)";
// $language['exemple4']['custom'] = "Hallo \"%s\" :)
// <br/> ich hoffe, es geht Ihnen gut<b style=\"font-size:5px\">%s </b>
// ";

CodePudding user response:

You can do it like this:

$language['hello_world'] = [
             'en' => 'Hello world',
             'fr' => 'Bonjour le monde',
             ....... 
    ],

... aand where you will use it, you just write:

$language['hello_world']['en']
           ^ the phrase   ^ the language you need

In my website, i use different locale files, ex: en.php, fr.php and I have only 1 array with all phrases. I take them in the same way, I just change the value of which file to call by selected language.

  • Related