How to edit .po language files in WordPress themes

If you have just bought some fancy theme and while trying to to localise run into .po, .mo, and .pot files – you will need special tool to make changes to them. This article explains how to take a .po file that is included with your WordPress theme and/or plugin download and translate it into your native language.

So what the heck are those .mo, .po, and .pot files anyhow and why are they included in my download?

Well, the files aren’t really important if English is your primary language but if you want to have WordPress, a WordPress theme, or even a plugin localized in your native language then those files are golden.

  • .mo stands for Machine Object
  • .po stands for Portable Object
  • .pot stands for Portable Object Template

The file that you want is ideally a .po file since it’s the raw editable text scraped from the entire WordPress theme/plugin. The .mo file is the compiled export of the .po file which is used by WordPress.

Here are the steps to translate/localize a .po or .pot file into another language

  • Download a gettext file editor like Poedit and install it.
  • Open the English .po file that came with your WordPress theme or plugin with poedit. If you only got a .pot, just rename it to .po and open it in poedit.
  • Now go through and translate all the text one line at a time in the bottom box.
  • Then “File” > “Save as” to your desktop or a folder on your computer. This will output both .po and .mo file.

Some text characters need to be converted into html entities otherwise they will not display correctly. A very common example is a word containing an apostrophe or single quote (‘) which needs to be replaced with ' — for example, Chloe O’Brian should be written as Chloe O'Brian. For a complete list of html entities, visit W3Schools.

You will also need to make a change to your WordPress wp-config.php file (located in your WP root directory) with the correct language codes like the example below. If you don’t have a WPLANG entry then you can create one by adding line below int your wp-config.php file. The sample below is for Brazilian language and you can find all language codes in here.

define ('WPLANG', 'pt_BR');

For more resources about how to translate WordPress into your language click here.

Disable button onclick to prevent double submition

Today I had an simple task: to disable a button after a click to prevent double submitting the form data. I wanted to solve it as simple as possible. So here’s the final solution

<form method="POST" action="">
<input type="submit" value="Submit" name="submitBtn" onclick="this.disabled=true;this.form.submit();" >
</form>

It works like a charm – it disables the submit button and it submits the data.

NOTE
I have found a interesting bug in Chrome. My input button had its name set to “submit” (name=”submit”), but then chrome was reporting following error:

Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function generate-form.php:onclick

The reason for the error when trying to call form.submit() is that your submit button is called “submit”. This means that the “submit” property of your Form object is now a reference to the submit button, overriding the “submit” method of the form’s prototype. Renaming the submit button allowed me to call the submit() method without that error, so I renamed it to “submitBtn”.

Tags: disable button onclick, onclick disable button, uncaught typeerror: property \submit\ of object #<htmlformelement> is not a function

Validating URL in PHP without regular expressions

Validating many things in PHP is often done using regular expressions, but since those might be complicated to understand, versions of PHP later than 5.20 have new validating mechanism built in. It’s done using filter_var function. Here are some basic examples of both old (regular expression) and new (filter_var) validation functions:

Validating URL using regular expressions:

function isValidURL($url) {
	return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

Validating URL using filter_var:

function isValidURL($url) {
	if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) return true;
	else return false;
}

You will notice FILTER_VALIDATE_URL and FILTER_FLAG_HOST_REQUIRED flags in filter_var function. There are many more and here are some more real world examples

var_dump((bool) filter_var('http://www.website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
var_dump((bool) filter_var('http://website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
var_dump((bool) filter_var('www.website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
var_dump((bool) filter_var('website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));

Output:

bool(true)
bool(true)
bool(true)
bool(false)

Here are some most common URL validation flags explanation

  • FILTER_FLAG_SCHEME_REQUIRED – Require the scheme (eg, http://, ftp:// etc) within the URL.
  • FILTER_FLAG_HOST_REQUIRED – Require host of the URL (eg, www.google.com)
  • FILTER_FLAG_PATH_REQUIRED – Require a path after the host of the URL. ( eg, /folder/file.ext)
  • FILTER_FLAG_QUERY_REQUIRED – Require a query at the end of the URL (eg, ?key=value)

Validating Email using regular expressions:

function isValidEmail($email) {
    return preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $email);
}

Validating Email using filter_var:

function isValidEmail($email) {
	if (filter_var($email, FILTER_VALIDATE_EMAIL)) return true;
	else return false;
}

Conclusion
Validating URLs, Emails, IPs and many other things using regular expressions is history now. Code for validations using filter_var is easier to understand easier to write and looks more php geeky. You don’t have to search for “working” regular expressions for your validations… It’s all there and it’s well documented and all you need is just need to use it.
Continue Reading

Tags: url validation in javascript without http, php validate url

Mysqldump: Got error: 1016: Can’t open file (errno: 24) when using LOCK TABLES

Ok so today I had to dump 700mb mysql database. I have tried using standard syntax:

mysqldump -uUSER -pPASS db_name > dump.sql

But I have got the following error:
Mysqldump: Got error: 1016: Can’t open file: ‘./db_name/xxx.frm’ (errno: 24) when using LOCK TABLES

I’ve searched a bit and I have found that solution was to add parameter –lock-tables=false so the final working mysqldump command was like this

mysqldump -uUSER -pPASS db_name --lock-tables=false > dump.sql

Web host is adding ?PHPSESSID to the end of all URLs

Today I a customer of mine came to me with a problem with web site that he have just moved from one host to another. And on that new host all his links on his site suddenly had ?PHPSESSID=k234j2knk… in the end. Since that is totally unusable since that site doesn’t even uses sessions and could affect his search engine rankings, he wanted it out of the way. The new host he moved that site to was shared and it doesn’t allow editing of php.ini any way so I had to make some other solution.

After failing with adding one of those on top of php files, as suggested on most pages that I found on Google:

// stop PHP from automatically embedding PHPSESSID on local URLs
ini_set('session.use_trans_sid', false);

// only use cookies (no url based sessions)
ini_set('session.use_only_cookies', true);

I suggested him to just switch hosts again, but he said he already paid up front for the whole year… and that it’s not an option… and that he wants that off his site… So, I’ve Googled some more and finally found a simple solution (that doesn’t require editing of 100’s of files in his case because of poorly programmed site). All I had to do is just put one line of code in .htaccess file and BOOM! All those nasty ?PHPSESSID were gone!

php_flag session.use_trans_sid off