Sooand
Sooand

Reputation: 111

php gettext included from different folders doesnt work?

i am trying to localize my code and i want to use gettext and poedit to do so it seems like the most straight forward way to do it.

I initialize all my classes and views from a simple script which includes this.

I am struggling to understand how the gettext() function works here is an example of what i mean:

File structure of the translation from root: i18n/Locale/da_DK/LC_MESSAGES. The messages.po and .mo is in the LC_MESSAGES folder and if my scripts are in the i18n folder and i call c.php in the browser it works

If i put the c.php in a folder further apart it doesn't work so the path is: someFolder/i18n/Locale/da_DK/LC_MESSAGES and the c.php is in someFolder and includes a.php and b.php from i18n.

scripts:

a.php

<?php
// use sessions
session_start();

// get language preference
if (isset($_GET["lang"])) {
    $language = $_GET["lang"];
}
else if (isset($_SESSION["lang"])) {
    $language  = $_SESSION["lang"];
}
else {
    $language = "da_DK";
}

// save language preference for future page requests
$_SESSION["Language"]  = $language;

$folder = "Locale";
$domain = "messages";
$encoding = "UTF-8";

putenv("LANG=" . $language); 
setlocale(LC_ALL, $language);

bindtextdomain($domain, $folder); 
bind_textdomain_codeset($domain, $encoding);

textdomain($domain);

b.php

<?php
echo _('Change language');
?>

c.php

<?php
include('a.php');
include('b.php');
?>

c.php (in someFolder)

<?php
include('i18n/a.php');
include('i18n/b.php');
?>

Upvotes: 0

Views: 789

Answers (1)

Paweł Tomkiel
Paweł Tomkiel

Reputation: 1984

Most probably it's all because of your Folder definition:

$folder = "Locale";

When you move your file to someupper folder, you should modify path to something like:

$folder = "someFolder/Locale";

Let me know if this helps.

EDIT:

Or even better, hardcode your path if you know it:

$folder = "/home/me/myproject/someFolder/i18n/Locale";

Upvotes: 1

Related Questions