0

I'm creating a custom Wordpress theme and I'm using exactly 3 phrases that I need to be translated according to the site language selected. I've digged a little bit through the Internet, but couldn't find an easy solution to add those phrases, so I could translate them through admin panel on my site.

I'm wondering if there is any way to add those phrases (f.e. in functions.php file) without using any PO/MO/POT files?

  • The general idea between having a theme textdomain and a language folder is that people can add .mo files in their own language and edit the translation without touching your .php theme files. – Andrei Gheorghiu Feb 3 '15 at 0:39
0

Here is an idea or two that you can pursue here: (CAVEAT: Untested)

OPTION 1

In your template file (use a child theme if this is not your own theme), use get_locale in a conditional statement to check which language is set in WPLANG and then dish up the text according to the locale

OPTION 2

Create yourself a function (which will be the ideal solution and what you are actually asking) using the gettext or gettext_with_context filter depending on your string context.

In conjuction with get_locale() you can then get and transalte and serve the correct translated string according to the language set

0

From my point of view, finding other alternative ways to translate a theme is not recommended and will be a setback in its popularity, as it will not allow users to add translations the same way they do for other themes and plugins.

For alternatives, see @PieterGoosens' answer. If you asked this because you found the process of adding i18n to your theme overly complicated, here's a step by step, using PO/MO/POT files. (So it doesn't actually answer your specific question, but might be helpful to those who need to add theme i18n). Assuming your theme is yourtheme:

function load_yourtheme_theme_textdomain() {
    $path = get_template_directory() . '/lang';
    load_theme_textdomain( 'yourtheme',  $path);
}
add_action( 'after_setup_theme', 'load_yourtheme_theme_textdomain' );

You will want to wrap the translatable strings in __() function, using 'yourtheme' as textdomain. For example:

echo __('Some text', 'yourtheme');

Here's a list of WP l18n wrappers. Have a look a the the related functions on that page.

Now:

  1. Download and install Poedit. It's free.
  2. Open it, click File»New. Choose default language.
  3. Save file as .pot in /lang folder, inside your theme.
  4. Go to Catalogue»Properties, add in the general details of your project. Use UTF-8.
  5. On Sources Paths (second tab), add ../ as base path.
  6. Click the second button in the row (the one that hovers "New Item") and add a single dot . as the path of your new item. Click Ok to save the setting.
  7. Catalogue»Update from sources.

The file will auto-grab all the translatable strings of your theme. Save again (make sure you save it as .pot) and you're done.

If I skipped anything or you get stuck in any step, I'll update with details.

  • Like your answer, but you've missed the integral part of the question unfortunately. – Pieter Goosen Feb 3 '15 at 6:05
  • And PoEdit isn't really free anymore. But services like POEditor.com are. – kaiser Feb 3 '15 at 8:40
  • @PieterGoosen See my comment under his question. I've added it before adding the answer. Than thought a walk-through with the steps would be useful on this question and added. – Andrei Gheorghiu Feb 3 '15 at 9:47
  • @kaiser It's not? I see a big green "Free Download" on their page. What am I missing? – Andrei Gheorghiu Feb 3 '15 at 9:56
  • You can't generate .pot files with the free version. – kaiser Feb 3 '15 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.