Poedit Parse Phtml Files
Please note: The following tutorial is mostly about creating language files from scratch. If your theme provides an up-to-date language file, either.po or.pot you can create translation files without much of the following information. There are a lot of excellent tutorials on the web regarding how to prepare your WordPress theme for translation, as well as how to translate it using.
How to load HTML from a file in Java. Cookbook contents Introduction. Parsing and traversing a Document; Input. Parse a document from a String. Thanks for Piotr Szmyd for your help. With the Orchard module Translation Manager I can update my.po files. But I had initial problems: Update.po files with one click; Open.po files in POEdit (the extracted files from Translation Manager are not marked as UTF-8) The solution is a little VBS script, that maybe help some of you with the same problem. Mo.lua - Parser of MO files. Join GitHub today. There are a lot of programs to translate.po file. One of them is poedit.
I am not going to repeat what already has been written, so here is a small list of tutorials on the subject (in case you are too lazy to search for them):. Translation with POEdit – Internationalize / Localize WP Themes Guide – – –. These should be enough to get things started and do things properly, right?
Probably, you already thought that instead of typing a text-domain over and over, you will use a variable or a constant, so that you can easily copy-paste from theme to theme, right? You don’t know enough yet. You definitely need to read the following two articles by Otto:. Alright, I’m ready!
No you are not. Now comes my time to speak write whatever At this point, I’m assuming that you have read and understood what has been written on the articles/tutorials I gave you, and that you are familiar with all WordPress’, as well as the other related functions mentioned in the same page. The reason I’m writing this post, is the failure of every other post I’ve read to properly inform me on how to “configure” Poedit for use with WordPress. I quoted the word configure, because it’s not really Poedit’s configuration, rather than what options Poedit will pass to, but whatever, I’ll be writing Poedit for easiness.
I also found troubles when actually translating a website from Greek to English. Let’s start by a simple rule: Use English as your base language during development, and only use English with the localization functions. And here is why: It’s very common that you need a website in insert random language (let’s call it Greek) and English.
The client (or you) needs the website up and running ASAP of course, so he/she wants the Greek version delivered tomorrow and the English version next week. The person that will be managing the site prefers the back-end to be in English. The client also wants to see progress, so you give him access to the development server. You install WordPress, install a theme, install all necessary plugins, and start copying the content that he gave you.
Of course, clients being clients, start whining about that section of the theme that says “Latest videos” instead of “Πρόσφατα βίντεο”, so you think you’ll just edit the theme’s files, and change that e(‘Latest videos’, ‘theme’); call to e(‘Πρόσφατα βίντεο’, ‘theme’); You end up translating the whole theme to Greek that way, and the client is happy. Then, you need to work on the English version. And this is when the shit hits the fan. You have an English WordPress installation, with a hard-coded Greek theme. “No problem” you say, “I’ll use that premium plugin that allows me to translate from within WordPress”. Sure, that’s what I did.
Problem is, both gettext and most plugins assume that the files and gettext calls are written in English. So, somehow I ended up with this Since I set on the plugin that I want Greek as the primary language, I was able to translate Greek to English, but how could I translate English to English? I really don’t want to remember that project.
From that time on, everything is built is English and then translated to any other language, even if the “other” language will be the only one. Anyway, this post is really about Poedit and gettext secrets, so voila! Plural forms Some of the tutorials mention that you need to add a plural form. Indeed, each language has it’s own peculiarities on plurals, and gettext needs to know how to handle them. English and Greek use the same plural form: nplurals=2; plural=(n!= 1) You will understand why plural forms are needed, later in the tutorial.
For those who want a quick explanation, nplurals=2 says that there are two forms, a singular and a plural. Plural=(n!=1) is a C-syntax expression, where n is a variable with the number passed (.e.g. From the n function) and plural stores the evaluation result of the expression (must be npluralsplural=0) i.e. If n=1 then plural=1!=1 so plural=0 (false), and if n=10 then plural=10!=1 so plural=1 (true).
You don’t need to understand what this does (unless you want to), as it requires you to really understand how gettext works. A list of plural forms for other languages, and if you absolutely need to know what, why and. In order to add a plural form, open your.po file into Poedit, and from the Catalog menu select Properties Then, under the Translation Properties tab, next to the Plural Forms label, enter your plural form. Alternatively, you can open your.po file using a text editor. You will see on top, quite a few lines that are enclosed with double quotes. Search for the one that starts with “Plural-Forms: and append your form before the n” (backslash-n-double-quote). If you can’t find it, just add it in a new line: 'Plural-Forms: nplurals=2; plural=(n!= 1); n' To put it into context, it should look something like this.
Which brings me to the next set of options Sources Paths There are usually two scenarios:. Your theme’s language files are in a sub-directory. Your theme’s language files are not in a sub-directory. If your theme’s language files are indeed in a direct sub-directory within the theme’s folder (e.g.
Your theme’s folder is /wp-content/themes/your-theme/ and your language files are in /wp-content/themes/your-theme/lang/ ), then set your Base path to. (dot) and add two more paths, a. (two dots) as such: Or if you edit your.po file directly make sure the following lines are as such. You did know that the dot means current directory and the double dot means parent directory, didn’t you? Just so you know, Poedit works recursively, that when you set a directory as a path, it will also search all its sub-directories.
Setting the base path to a dot, means that we instruct Poedit to set the base path to wherever the.po file lives. Then, the other paths ( search paths) tells where Poedit should look for gettext calls, relative to the base path. In effect, we are saying: I’m here; get whatever text you find here and in my parent directory, and all our subdirectories.
If you theme’s language file is not in a sub-directory, don’t add the. (double dot) on the paths, or if you are editing the.po manually, discard the “X-Poedit-SearchPath-1:. N” Now, let’s go to the good part. Keywords lists, WordPress functions, whaaaaaa? All the tutorials that I gave you, along with those that I didn’t give you, mention that you should add the functions and e in the Sources Keywords tab.
Some also include functions such as x and n. Even less mention that when adding x and n there is a need of some cryptic numbers after them. And only one of them explains what those numbers are. Take a deep breath Poedit (gettext actually) needs to know what PHP functions it must look for and get information from.
In reality, gettext knows by itself what to look for in a.php file, but since WordPress uses its own wrapper functions to make our (the programmers’) lives easier, this pre-programmed knowledge is no longer applicable. Hence, we need to let Poedit know what functions to look for and how to handle them. The complete functions list that it needs to know about are these (as of WordPress v.3.4.2). Looking at the Codex documentation will reveal all of these functions, except from c and nc which are deprecated but I include for completeness.
I don’t know why every online resource I found failed to document all these. Unless you are absolutely certain that you only used and e throughout your code, you should include the full list above. So, go on, add them in your.po file’s Sources keywords tab.
The screenshot has different things from what you just gave me. What about those cryptic numbers? You’re right here you go. Let’s explain a bit, shall we? e doesn’t need any numbers, as it only accepts two default parameters: the text to be translated and the domain. Any function that only accepts a text and a domain, doesn’t need the colon and the numbers. n needs the:1,2 as 1 says the first parameter is the first plural (singular) and 2 is the second plural.
x needs the:1,2c as 1 says the first parameter is the first plural (it doesn’t care really if it’s plural or not, it cares it has to translate it) and 2c says the second parameter is a comment, that is, the disambiguation text (context). For all of you that don’t see your disambiguation text (context) in Poedit, this is what is needed. This is the exact reason I started researching and writing this tutorial in the first place, so, I dedicate it to all of you:) nx is really a combination of n and x, so:4c,1,2 says that the first and second arguments of the function are the plural forms, and the fourth is the disambiguation text. I couldn’t find any resources so that I can understand the rules governing statements like:4c,1,2 and didn’t had time to experiment, to see if placing the 4c last would make any difference, so, if you did experiment if you did find some documentation about it, let me know in the comments. Anyway, since adding them one by one from within Poedit is a bit pain in the a.s, you can edit the.po file directly and paste the following. That x:1,2c rule was what I was looking for too:) But here’s another puzzle I have that you might know how to solve: IS it possible to adapt the keyword list function rules to only include strings from one particular domain? Case in point: I use lots of translation strings in a plugin in a similar context as used by WordPress itself so they are already in the main PO database and most likely already translated for most users.
So if I use for example ('Category') instead of ('Category','my-plugin-domain') that string gets translated even if there is no translation for my plugin yet. As long as the context is similar, this is an advantage. But the downside is that these strings find their way into the plugins PO file too where they are dead weight among the other plugin specific strings, since translating them differently there will have no effect. And too the translator there is no visible difference between ‘valid’ plugin strings and strings that belong to the main WordPress domain Any thoughts?:). Great, great article. Want to add still a couple of things (yes, i18n never ends, does it?) On the one hand, both ngettext and ngettextnoop are deprecated, as c and nc. On the other hand, there is the ability to add comments for translators (from the very code), which is a good practice if you want someone else to work in your files.
These are a bit trickier, though. To add a comment for translators, you need to add a PHP comment block ( /.
comment block./) in the line before the i18 function, and this comment block must start by the tag “translators”. Like this: /.
translators: This isn’t foo, is a bar!./ $foobar = x( ‘foo’, ‘bar’, ‘foobar-domain’ ); And.also., you need a minor bit of tweaking with POEdit to make them show. Go to File-Preferences-Parsers tab, and select PHP, then Edit. Then, look for Parser command, which will be something like: xgettext –language=PHP –add-comments=TRANSLATORS –force-po -o%o%C%K%F You need to replace the uppercase TRANSLATORS with lowercase translators, like this: xgettext –language=PHP –add-comments=translators –force-po -o%o%C%K%F And that’s it, you’re good to go with developer comments for translators. And a great comment too! I wasn’t aware that POEdit wouldn’t parse the translator comments, since I never used them myself, so great advice right there!
I hope you are ok if I update the post with this extra bit of info. Perhaps you already know this, but the translators’ comment can also be on the same line as the gettext call, as long as it’s right before it. It can be useful for situations like: php/php.
Thanks for the article! Am wondering if it would be possible for POEdit (or any other program) to only read and include strings from a given domain.
Java Parse Html
Why i’m asking this is, because i’m using WooCommerce and am overriding the templates in my theme directory. The woocommerce translations are just fine, so i’m using their domain name. Now my.po file is a mixture of two domains. Is there any way to avoid that and have POEdit read from specific domain? Solution i’m using now, is putting all my theme strings into a single file in a separate directory and point POEdit to that specific directory. Tl;dr Assuming everything is set-up properly (e.g.
Plural form expression), I’d imagine you have multiple instances of the same text in your files, but only some of them are translated, or the domain does not match. Long answer: Consider these calls: php /php They produce the following output inside the.po(t) file (generated by the latest version of Poedit at the time of writing). code #: functions.php:2 functions.php:3 functions.php:8 msgid 'One' msgidplural 'Many' msgstr0 ' msgstr1 ' #: functions.php:4 msgctxt 'context' msgid 'One' msgstr ' #: functions.php:5 functions.php:6 msgid 'Many' msgstr ' #: functions.php:7 msgctxt 'context' msgid 'Many' msgstr ' /code If you follow the msgid and msgidplural strings, you’ll make some observations. For example, you have 2 occurrences of “One” but 3 occurrences of “Many”, although in the PHP code above, each word appears exactly four times.
Obviously, the calls with a context create a new entry in the.pot file to differentiate from the calls without a context. What is really interesting however, is that the first block that contains the n strings is grouped together with the first and e calls of lines 2-3, but there is separate block generated for the respective calls of “Many” in lines 5-6.
Thanks for that! This is really helpful! But its hard to do a step by step translation, because I always have to extract and sync again. In POEdit I just would have to click the 'refreh' button. Another disadvantage: If I have an existing german UTF-8 translation und run 'sync', it overwrites the UFT-8 formatting and my german translation is useless.
And the 'extract' command extracts multiple entries (i.e. I have a T-string in 2 files with the same name), but maybe this is the normal behaviour? – Mar 30 '15 at 6:28. Thanks for Piotr Szmyd for your help. With the Orchard module I can update my.po files. But I had initial problems:.
Update.po files with one click. Open.po files in POEdit (the extracted files from Translation Manager are not marked as UTF-8) The solution is a little VBS script, that maybe help some of you with the same problem. Just save this to updateHelper.vbs (Don't forget to set the configuration values): ' #################### ' Configuration Const OrchardPath = ' ' Path to Orchard Const Modules = ' ' e.g. Orchard.Blogs,Orchard.Pages Const DefaultCulture = 'en-us' ' default language ' #################### OrchardWeb = OrchardPath & ' src Orchard.Web' OrchardBin = OrchardWeb & ' bin ' WScript.Echo 'Extracting T-strings from module.'
O general split ac installation guide. Set objShell = WScript.CreateObject('WScript.Shell') objShell.Run OrchardBin & ' Orchard extract default translation /Output:' & OrchardWeb & ' /Extensions:' & Modules, 0, true WScript.Echo 'Extracting archive.' ZipFile = OrchardWeb & ' Orchard.' & DefaultCulture & '.po.zip' ExtractTo = OrchardWeb Set fso = CreateObject('Scripting.FileSystemObject') If NOT fso.FolderExists(ExtractTo) Then fso.CreateFolder(ExtractTo) End If Set objShell = CreateObject('Shell.Application') Set FilesInZip=objShell.NameSpace(ZipFile).items objShell.NameSpace(ExtractTo).CopyHere(FilesInZip) WScript.Echo 'Deleting archive.'