randomKek
randomKek

Reputation: 1128

php scalable multilanguage solution

What would be a scalable and low resource solution to apply multi-language in my PHP website? Also how would you guys integrate it with javascript, some javascript also requires translations.

My current solution is just:

define('DEFAULT_LANGUAGE', 'en');

if(!isset($_SESSION['language'])){
    $_SESSION['language'] = DEFAULT_LANGUAGE;
}

function lang($key, $set = null){
    static $lang;

    if($set !== null){
        $lang = $set;
        return true;
    }

    return $lang[$key];
}

include('language/' . $_SESSION['language'] . '.php');
lang(null, $lang);

My doubts to this solution: if the array in the file, is pretty big, 1000+ elements, and we pass it in the function, set the language array, it's doubled in memory right, because we are not passing by reference?

Thanks for reading.

Upvotes: 0

Views: 308

Answers (1)

KingCrunch
KingCrunch

Reputation: 132051

  • 1000 elements is not that much
  • PHP uses "copy-on-write", which means, that it doesn't consume additional memory, as long as you don't change something.
  • Even if, that would not be that much (see first point) and only for a short period, when you use unset($lang)

Upvotes: 1

Related Questions