0

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?

4
  • 1
    That's because define is used to set a constant, not to create a global variable. Commented Dec 10, 2018 at 15:12
  • 1
    Constants can only be defined once. Commented Dec 10, 2018 at 15:12
  • Possible duplicate of Redefining constants in PHP Commented Dec 10, 2018 at 15:12
  • Got it, i will change define to variables instead of constants Commented Dec 10, 2018 at 15:17

1 Answer 1

1

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.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i like your solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.