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”.

How to backup data on second HDD using Google Drive

If you’re using Google Drive it’s default path is c:\User\USER_NAME\Google Drive\ but often my partition on c: is quite small since I only use it for operating system and I store all my data on other drives. So I’d like Google Drive to backup my data on other drive and still allow me to access it in my user’s folder. The idea is to create a symbolic link so that C:\User\USER_NAME\Google Drive\ links to e:\Backup in my case (or some other path in your case).

First close (quit) the Google Drive application.

1. Now you need to go and remove directory and all it’s files from C:\User\USER_NAME\Google Drive\ (or simply rename the folder or copy all files to your 2nd backup folder on other drive).

2. Than you need to run CMD as Administrator – see the picture below how:

Run CMD as Administrator

3. Then you need to make Smbolic link. But not a shortcut link than directory redirect link, entering mklink /D “c:\Users\USER_NAME\Google Drive” e:\Backup
If your backup path contains spaces put it in double quotes too like the fist path.
Making Symbolic link on Windows 7

Start the Google Drive and Snyc your data.

That’s it!

How to change language in Google Drive application to English

Google Drive is great cloud service where you can store online or backup all your important files that you can later access wherever you are just by logging into your Gmail (Google) account and going to http://drive.google.com. You can drag and drop files to your browser while on that url or you can install their application that will sync a selected folder to the cloud. Problem is I can’t change the language on my Google Drive since there is nowhere to do so. I prefer that my Windows and all applications are in English. I have searched the Google Drive’s settings but seen nowhere to change settings but than I searched a bit more and I found rather simple solution.

Before you start you should close Google Drive.
Then you need to go to Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables.
Then on user variables press New button for Variable name enter LANG and for value enter en_US.
Restart the Google Drive and it should work now in English.

Easy as a pie!

Here’s a screenshot of the windows that might help you find your way trough:
Google Drive language fix

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