Auto translate themes or plugins

Advertisement

With the help of WP CLI command we can translate the theme and plugin from the existing translated strings.

You are going to see:

How to use? How to use?

  • Create fresh WordPress setup.
  • Clone the plugin wp-i18n into your /wp-content/plugins/ directory.
  • Import the database latest.sql with WP CLI commnd as below:
    wp db import latest.sql
  • Now use below commands to translate any plugin/theme from wp.org.

To translate the plugin use 2 WP CLI commands:

wp wpi18n download_plugin_po {slug} --lang={mr}
wp wpi18n generate_plugin_po {slug} --lang={mr}

Here,

E.g. To translate the Contact Form 7 plugin we can use below commands:

wp wpi18n download_plugin_po contact-form-7  --lang=mr
wp wpi18n generate_plugin_po contact-form-7  --lang=mr

First command download the .po file of plugin.
And second command generate the translated strings .po file.


And, To translate the theme use below WP CLI command.

wp wpi18n generate_theme_po {slug} --lang={mr}
wp wpi18n generate_theme_po  {slug} --lang={mr}

Example 2: In below example I’m translating contact-form-7 plugin.

E.g. To download the plugin .po files Im using command wp wpi18n download_plugin_po contact-form-7 --lang=mr

D:\xampp\htdocs\wpi18n\wp-content\plugins\wp-i18n (master)
? wp wpi18n download_plugin_po contact-form-7 --lang=mr
contact-form-7 | CREATED - po-files/plugins/contact-form-7/mr/wp-plugins-contact-form-7-dev-mr.po | https://translate.wordpress.org/projects/wp-plugins/contact-form-7/dev/mr/default/export-translations
contact-form-7 | CREATED - po-files/plugins/contact-form-7/mr/wp-plugins-contact-form-7-dev-readme-mr.po | https://translate.wordpress.org/projects/wp-plugins/contact-form-7/dev-readme/mr/default/export-translations
contact-form-7 | CREATED - po-files/plugins/contact-form-7/mr/wp-plugins-contact-form-7-stable-mr.po | https://translate.wordpress.org/projects/wp-plugins/contact-form-7/stable/mr/default/export-translations
contact-form-7 | CREATED - po-files/plugins/contact-form-7/mr/wp-plugins-contact-form-7-stable-readme-mr.po | https://translate.wordpress.org/projects/wp-plugins/contact-form-7/stable-readme/mr/default/export-translations

Now, Im using command wp wpi18n generate_plugin_po contact-form-7 --lang=mr to auto generate the .po files.

E.g. It looks like below:

D:\xampp\htdocs\wpi18n\wp-content\plugins\wp-i18n (master)
? wp wpi18n generate_plugin_po contact-form-7 --lang=mr
contact-form-7 | 4 | 178 | 4508 UPDATED Documentation | WITH ?ñí?Ñï?ñò?Ñì?ñ»?Ñü?ñ«?Ñç?ñé?ñƒ?ñ??ñ¿
contact-form-7 | 4 | 177 | 0 NOT EXIST Support
...
...
contact-form-7 | 1 | 2 | 0 NOT EXIST Fix: Updated wrong 5 star rating link from dashboard page
contact-form-7 | 1 | 1 | 0 NOT EXIST Initial release
contact-form-7 | 1 | COMPLETE - TRANSLATED 36 STRINGS
contact-form-7 | 1 | po-files/plugins/contact-form-7/mr/wp-plugins-contact-form-7-stable-readme-mr-translated.po
contact-form-7 | TOTAL TRANSLATED 36
ALL TRANSLATED STRINGS COUNT: 2901

After executing both commands we have 36 auto translated strings. Also we have 2901 available strings in Marathi language.

All translated strings are stored in directory:
/wp-i18n/po-files/plugins/contact-form-7/mr/

Now file wp-plugins-contact-form-7-dev-mr.po is original file and wp-plugins-contact-form-7-dev-mr-translated.po is translated file.

We can use this auto translated file and import into Glotpress.

To confirm the translated strings the log file is generated which show all the translated strings. E.g.
/wp-i18n/po-files/plugins/contact-form-7/mr/log.txt

This file show the content like below:

1 | Documentation | ????????????
2 | Version %s | ??????? %s
...
...
36 | Features | ??????????

Top ↑

How it works? How it works?

In WordPress there are lot of strings are 100% translated. See https://translate.wordpress.org/stats/

I notice that there are lot of common strings which are already translated in WordPress core releases.

E.g. Strings Leave a comment, Comment Content, Comment Date, Completed are some of the common strings examples which already translated into WordPress core releases.

In this article I’m giving an example of Marathi (?????) (mr) which have now 160 contributors.

In Marathi language there are 9+ core release:

  • dev
  • 4.8.x
  • 4.7.x
  • 4.6.x
  • 4.5.x
  • 4.4.x
  • 4.3.x
  • 4.3.x
  • 4.1.x

Each release have 4 sub projects:

So, For dev project core release have 4 sub projects such as:

https://translate.wordpress.org/projects/wp/dev/mr/default/
https://translate.wordpress.org/projects/wp/dev/cc/mr/default
https://translate.wordpress.org/projects/wp/dev/admin/mr/default
https://translate.wordpress.org/projects/wp/dev/admin/network/mr/default

And, Core version 4.8.x have 4 sub projects such as:

https://translate.wordpress.org/projects/wp/4.8.x/ru/default
https://translate.wordpress.org/projects/wp/4.8.x/cc/ru/default
https://translate.wordpress.org/projects/wp/4.8.x/admin/ru/default
https://translate.wordpress.org/projects/wp/4.8.x/admin/network/ru/default

And so on.


For each sub project we have a Export option.

Auto translate themes or plugins 1

With this option we can export the translated project translated strings in below formats:

  • Android XML (.xml)
  • Portable Object Message Catalog (.po/.pot)
  • Machine Object Message Catalog (.mo)
  • .NET Resource (.resx)
  • Mac OS X / iOS Strings File (.strings)
  • Java Properties File (.properties)
  • JSON (.json)
  • Jed 1.x (.json)
  • NGX-Translate (.json)

Top ↑

Development Background Development Background

I have create a fresh WordPress setup and created a plugin wpi18n.

In this plugin I have register a post type which store the original string as a post title.

And its translated language version in post meta.

E.g.

Auto translate themes or plugins 2

Now,

  • I have downloaded the .po files of all the core projects from 4.1.x to dev with the help of WP CLI command wp wpi18n download_all_core_files

We have all the translated files of each core release of each language. To process all languages it takes too much time. So, I have added the individual language support for importing the translated strings.

The CLI command wp wpi18n import_translated_strings mr download the original & translated string in Marathi language.

After importing strings, I notice that there are lot of strings which are translated as different in different core projects.

E.g. The string Leave a comment is translated as below:

  • ????? ??? (4-1-x)
  • ??????????? ???? (4-2-x)
  • ????? ??? (4-3-x)
  • ????? ??? (4-4-x)
  • ????? ??? (4-5-x)
  • ????? ??? (4-6-x)
  • ????? ??? (4-7-x)
  • ????? ??? (4-8-x)
  • ????? ??? (4-9-x-development)

So, To keep the consistency I have used most repetitive strings as a translated strings. E.g.

  • (8 times) ????? ???
  • (1 times) ??????????? ????

So, I have used string ????? ??? to auto translate the string Leave a comment in theme and plugin.

Some other examples:

Example 2: String – Password: have 3 variations in Marathi (mr) language as below:

(4 times) ???????:
(2 times) ????????
(10 times) ???????:
--------- FROM PROJECTS ---------
(4-1-x) ???????:
(4-2-x) ???????:
(4-3-x) ????????
(4-5-x) ???????:
(4-6-x) ???????:
(4-7-x) ???????:
(4-8-x) ???????:
(4-9-x-development) ???????:

Example 3: String – Sites have 3 variations in Russian (ru) language as below:

(1 times) ??????
(3 times) ?????
--------- FROM PROJECTS ---------
(4-1-x-administration-network-admin) ??????
(4-1-x) ?????
(4-2-x) ?????
(4-3-x) ?????

To detect all the repeated strings I have created the command which generate the file of all repeated strings.


To discuss with other teammates and confirm it works for all languages I have created a log file which show the original string and its log of repetitive strings with most repetitive string.

Below are the some language files:

  • ru [85] – http://tinyurl.com/yb9ew4yk
  • ary [264] – http://tinyurl.com/y7achghc
  • mr [683] – http://tinyurl.com/y8m4vdb2
  • hi [1103] – http://tinyurl.com/y9pgvllx

To translate the string into any language I have create a command which set the most repeated string into the meta box.

E.g. String Sites is most 3 time repeated with ????? So, I have set this most repeated string and used it while translating the string with CLI.

I have created some commands to auto generate the .po file of any theme/plugin.

// Commands to download the .po files of theme/plugin.
wp wpi18n download_plugin_po {plugin-slug} --lang={language}
wp wpi18n download_theme_po {theme-slug} --lang={language}
// Commands to generate the .po files of theme/plugin.
wp wpi18n generate_theme_po {theme-slug} --lang={language}
wp wpi18n generate_plugin_po {plugin-slug} --lang={language}
// Other commands.
wp wpi18n translate_all_plugin_files --lang={language}
wp wpi18n generate_blank_lang_po {language}
wp wpi18n generate_filled_lang_po {language}
wp wpi18n import_translated_strings {language}
wp wpi18n set_most_repeated_string_as_top {language}
wp wpi18n detect_all_repeated_string {language}

To confirm strings, I have first used my own native language and I found that its 100% works. After that I have tried another language.

After translating plugins and themes I found that the Import Translation feature just import any .po file and detect the string into it.

Finally, I have create each language .po file which set the most repeated string as a translation string. And imported it into the theme/plugin. And it works as expected.


Top ↑

Known Issues Known Issues

After discuss with yui, I found that the repeated strings are depends on plural/singular, male, female or neutral or sometimes its participle highly depends on context.

The string Sites is translated as in (4-1-x-administration-network-admin) as ?????? because it has the context.

And in (4-1-x) its ????? because there is no context.

For reference see 4-1-x-administration.

While translating strings It does not check its context. I’ll discuss with community and maybe change this or maybe implement another way to do this same thing.


Top ↑

GlotPress Background GlotPress Background

WordPress use the GlotPress to translate the strings from WordPress Core, Themes, Plugins etc.

In GlotPress Roadmap there is a target Translation Memory which is scheduled for version x+5. It is the almost same as reusing strings from existing approved strings.

I have read about this from below reference links:


Top ↑

Whats next? Whats next?

I’ll discuss this with the Polyglot team on slack channel. You can also try this for your theme/plugin. Its super easy. Just you wan’t to setup the WP CLI on your local environment.

If you don’t have setup the WP CLI then read How to setup WP CLI (WordPress Command Line Interface).

Leave a Reply