Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API route to download ZIP files of translations #1

Merged
merged 9 commits into from
Feb 21, 2018
Merged

Conversation

swissspidy
Copy link
Collaborator

With this PR, translation information for projects will be available available over https://translate.required.local/api/translations/<path>, e.g. https://translate.required.local/api/translations/klingler/klingler-theme.

The format is similar to the one returned by the WordPress.org translations API. This way, WordPress can easily consume this additional data source.

Whenever translations are updated, a cron event is scheduled to generate a ZIP file containing both the PO and MO file for the project.

In the API, this looks as follows:

{
   "translations":[
      {
         "language":"fr_FR",
         "updated":"2018-01-12 13:47:57",
         "english_name":"French (France)",
         "native_name":"Fran\u00e7ais",
         "package":"https:\/\/translate.required.local\/content\/traduttore\/klingler-theme-fr_FR.zip",
         "iso":[
            "fr"
         ]
      },
      {
         "language":"de_CH",
         "updated":"2018-02-06 15:50:16",
         "english_name":"German (Switzerland)",
         "native_name":"Deutsch (Schweiz)",
         "package":"https:\/\/translate.required.local\/content\/traduttore\/klingler-theme-de_CH.zip",
         "iso":[
            "de"
         ]
      }
   ]
}

Our own plugins and themes can use this in combination with https://github.com/wearerequired/traduttore-registry. It would look like this:

Required\Traduttore_Registry\add_project( 'plugin', 'klingler-post-types', 'https://translate.required.com/api/projects/klingler/klingler-post-types' );

After that, when you run wp language core update on that site, plugin translations will be loaded from our GlotPress site (given that the IP address is whitelisted or Restrict Site Access is disabled on the GP site).

Note that the code is written in such a way that it could be eventually open sourced eventually.

Also note that the registry library doesn't yet fully support themes. The theme transient needs to be filtered first.

I've only really tested this locally and then copied everything over into that registry plugin. So it's definitely possible that something's not working correctly yet :-)

'action' => 'traduttore_zip_generated',
'description' => __( 'When a new translation ZIP files is built', 'traduttore' ),
'message' => function( $success, GP_Translation_Set $translation_set ) {
if ( ! $success ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be an undefined variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why's that?

This should be the variable passed by the traduttore_zip_generated hook on line 71.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I only saw the variable once I commented. I thought I deleted the comment.

Copy link
Contributor

@grappler grappler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good.

To generate the zip files from WP-CLI. The value --0=1 is the translation ID. I am not sure how to easily get a list of ID's.

wp cron event schedule traduttore_generate_zip --0=1 --url=translate.required.ch
wp cron event run traduttore_generate_zip --url=translate.required.ch

'english_name' => $locale->english_name,
'native_name' => $locale->native_name,
'package' => file_exists( $zip_provider->get_zip_path() ) ? $zip_provider->get_zip_url() : false,
'iso' => array_filter( [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we want to align the values?

}

// Make sure the cache directory exists.
if ( ! @is_dir( $this->get_cache_dir() ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is @ needed? The result should either true or false.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I copied that from core to suppress errors. Not a biggie imho

"keywords": [
"wordpress",
"glotpress"
],
"require": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a

   "suggest": {
        "wpackagist-plugin/slack": "Send Slack notification on generation ZIP completion"
    }


$zip_provider = new ZipProvider( $set );

$result[] = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the result only show if a zip file exists?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a benefit in that. When the URL isn't set, it's more obvious that the ZIP doesn't exist and WordPress will fail to download it anyway.

@swissspidy
Copy link
Collaborator Author

Regarding WP-CLI, I could totally add a separate command for this to make generation easier. Or perhaps the 15 minute timeframe needs to be adjusted, I don't know :)

@grappler
Copy link
Contributor

@ocean90 Can we merge this now?

@ocean90
Copy link
Member

ocean90 commented Feb 20, 2018

I wasn’t able to test the registry yet so I didn’t approve it.

@swissspidy
Copy link
Collaborator Author

I've just added a new CLI command that might be useful for testing during development.

Example usage:

wp @dev --url=https://translate.required.local traduttore generate_zip klingler/klingler-theme
wp @dev --url=https://translate.required.local traduttore generate_zip 12345

Regarding the registry, just note that the GlotPress API needs to be available to external users. Check the Restricted Site Access settings or temporarily disable the plugin altogether.

For production we obviously need a better solution that only grants access to the API (and the ZIP files)

@ocean90
Copy link
Member

ocean90 commented Feb 20, 2018

For local development you also have to disable SSL verification on the API request.

@swissspidy
Copy link
Collaborator Author

Right. I reported this in wp-cli/language-command#30 as you can't easily do that when calling the API via WP-CLI.

@grappler
Copy link
Contributor

Regarding the registry, just note that the GlotPress API needs to be available to external users. Check the Restricted Site Access settings or temporarily disable the plugin altogether.

We can add the IP addresses of the servers to the whitelist.

@swissspidy
Copy link
Collaborator Author

We can add the IP addresses of the servers to the whitelist.

That's what I meant with the Restricted Site Access settings :-)

@swissspidy swissspidy merged commit 0c84c01 into master Feb 21, 2018
@swissspidy swissspidy deleted the zip-api branch February 21, 2018 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants