0

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');
?>
Sooand
  • 91
  • 1
  • 9
  • What do you mean, by doesn't work? Do you have any PHP error or translations aren't correct? – Paweł Tomkiel Mar 13 '15 at 11:22
  • it should show "Skift sprog" which it does when c.php is in the i18n folder but if the c.php is in the someFolder and includes from there, then it just echos "Change language" and doesn't translate – Sooand Mar 13 '15 at 11:29

1 Answers1

1

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";
Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
  • thank you very much! i am just learning to use the gettext() function and the guide i had been following didn't cover this definition. – Sooand Mar 13 '15 at 11:45