Marcin
Marcin

Reputation: 1024

PHP Multiple require, same function

I'm trying to generate three pdf files with three different translations

The translations are stored within the directory in the files named by its the language prefix:

lang/pl/global.php
lang/en/global.php
lang/de/global.php

Translations are then stored within GLOBAL VARIABLES for example:

define("SOMEVAR", "SOME VALUE PL"); // for lang/pl/global.php
define("SOMEVAR", "SOME VALUE EN"); // for lang/en/global.php
define("SOMEVAR", "SOME VALUE DE"); // for lang/de/global.php

At the end of some function foo() i'm trying to call the same function translate() three times to generate 3 translated pdf files. Before a call I have to require translation from the directory structure listed above. It looks like:

require('lang/pl/global.php');
translate('pl');

require('lang/en/global.php');
translate('en');

require('lang/de/global.php');
translate('de');

But when I try to print a SOMEVAR global variable from the inside of translate() function it only returns the first prefix translation (SOME VALUE PL) - so in this case it would be 3 times of polish translate (3x SOME VALUE PL). Changing order to en/de/pl results in triple english translate (3x SOME VALUE EN).

I've also tried moving require() to the inside of translate() function but i got no results also.

Any help?

Upvotes: 0

Views: 79

Answers (1)

Royal Wares
Royal Wares

Reputation: 1272

It looks like what you're trying to do is create a global variable which is an array, in which case you would need to do:

$selectedLanguages = [];
$selectedLanguages[] = "pl";
$selectedLanguages[] = "en";
$selectedLanguages[] = "de";

Then when you want to call it for your requires do:

global $selectedLanguages;

foreach($selectedLanguages as $language) {
    require('lang/' . $language . '/global.php');
    // Do stuff with your translation here.
}

Upvotes: 1

Related Questions