Solved

gettext doesnt read mo/po files

Posted on 2014-09-29
9
115 Views
Last Modified: 2014-09-30
Hello fellow experts. Long time no see. Looks like I'm too tired to see the error at the code.
I have a class which have this as a constructor:

public function __construct()
		{
			//some code before this part
			if (isset($_GET["lang"])) {
				$language = $_GET["lang"];
			}
			else if (defined('LANGUAGE')) {
				$language  = LANGUAGE;
			}
			else {
				$language = "sr";
			}
			$this->setLocalization($language);
		}

Open in new window


And the method setLocalization:

public function setLocalization($code) {
							   $langCodes = array(
								 "sr" => "sr_RS",
								 "de" => "de_DE",
								 "en" => "en_US"
							   );
			if(!isset($langCodes[strtolower($code)])) {
				return;
			}
			$baseLangDir = './locale';
			$appName     = "office";
			$lang = $langCodes[strtolower($code)];
			// Set language
			putenv('LC_ALL='.$lang);
			//putenv("LANG=" . $code); 
			setlocale(LC_ALL, $lang);
		  
			// Specify the location of the translation tables
			bindtextdomain($appName, realpath($baseLangDir));
			bind_textdomain_codeset($appName, 'UTF-8');
			// Choose domain
			textdomain($appName);
		}

Open in new window


Im getting proper language code in it, and a path in bindtextdomain is properly resolved.
My directory structure is:

root
|__locale
            |__en_US
                       |__LC_MESSAGES
            |__sr_RS
                       |__LC_MESSAGES
            |__de_DE
                       |__LC_MESSAGES

There is a pair of office.mo/office.po files in each LC_MESSAGES directory.
For some reason command like:

<?php echo _("Dashboard"); ?>

Open in new window


gives only Dashboard even if its translated in mo/po.
What is the problem here, what am I missing?

Thanks in advance.
0
Comment
Question by:R-Byter
  • 5
  • 4
9 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40351235
There is nothing but the word "Dashboard" in your echo statement.  No file path or function to return anything else.
0
 
LVL 14

Author Comment

by:R-Byter
ID: 40351252
I dont see the problem there cause _() is the same as gettext(). Its the proper way to use it.

http://php.net/manual/en/function.gettext.php
0
 
LVL 14

Author Comment

by:R-Byter
ID: 40351254
As additional note, its Debian Linux and this is what I get when I type locale -a:

C
POSIX

Thanks
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40351269
Except that it isn't working.  Try the simple example on the man page with the values hard coded and see what you get.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Author Comment

by:R-Byter
ID: 40351284
Still doesnt work.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40351327
I won't be able to look at this again until tomorrow.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40351615
On my Ubuntu system, locale -a gives me:

C
en_AG
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NG
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
POSIX

I don't know where it gets all that.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40351623
In my Package Manager, I found a package called 'locales' that supposedly installed all that.  Maybe this will help: https://wiki.debian.org/Locale
0
 
LVL 14

Author Closing Comment

by:R-Byter
ID: 40351889
That's it, missing locales on some strange custom debian installation.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now