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 support for localization via standard WordPress methods (wp_localize_script()) #8

Open
greatislander opened this issue Aug 9, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@greatislander
Copy link

commented Aug 9, 2018

The way that WordPress handles localization for JS files is by creating an object with localized strings. So, for example, I'd use the following PHP code to register and localize a script:

<?php
	
wp_enqueue_script( 'myscript', 'scripts/myscript.js', false, null );
wp_localize_script(
	'myscript', 'myScript', [
		'login' => __( 'Log In', 'myplugin' ),
		'logout' => __( 'Log Out', 'myplugin' ),
	]
);

Then, in JS, I can access the localized strings (__( $string, $textdomain ) is the localization function) as follows:

console.log( myScript.login );

What I'm wondering is if there would be any interest in allowing User Interface Options (and this plugin) to load localized strings via JS objects instead of just by loading JSON message bundles as described here. This would allow the WordPress User Interface Options plugin (and other implementations, such as the one for Pressbooks that I've been slowly working away on) to support user-supplied localizations via WordPress methods.

@waharnum

This comment has been minimized.

Copy link
Member

commented Aug 13, 2018

This sounds interesting - I like the idea of supporting user-supplied localizations via the Wordpress interface, if this is the way Wordpress developers are used to handling localization.

Forgive my relative ignorance about WP development in the following comments / questions:

  • The flow of loading JSON message bundles for UIO ultimately results in Javascript objects on the UIO component containing the strings intended for use by the individual panels, so I believe we could write some custom Javascript for the plug-in that would replace this flow with the WP-based one you describe.

  • if wp_localize_script expects to work with a JS object, we could craft something that would (in the plug-in) parse the various JSON message files and generate appropriate scripts from them, perhaps

  • At a high level, how would we expose the UIO strings for translation in the Wordpress interface? I've done localization work in Drupal ages ago, but never in WP.

I'm uncertain of how much effort this would be without some experimentation (a very generic statement, I know), but it sounds interesting

@greatislander

This comment has been minimized.

Copy link
Author

commented Oct 19, 2018

@waharnum Sorry for the slow response. Replies to your points below.

if wp_localize_script expects to work with a JS object, we could craft something that would (in the plug-in) parse the various JSON message files and generate appropriate scripts from them, perhaps

Since I wrote this ticket, WordPress has published a JavaScript localization package which should allow something like this (no PHP intermediary required):

import { __ } from '@wordpress/i18n';

module.exports = {
	'slidingPanelShowText': __( '+ Show Preferences', 'uio' ),
	'slidingPanelHideText': __( '- Hide Preferences', 'uio' ),
	'showTextAriaLabel': __( 'Show Display Preferences', 'uio' ),
	'hideTextAriaLabel': __( 'Hide Display Preferences', 'uio' ),
	'slidingPanelPanelLabel': __( 'User Preferences', 'uio' )
};

At a high level, how would we expose the UIO strings for translation in the Wordpress interface? I've done localization work in Drupal ages ago, but never in WP.

You could then use this Babel plugin to generate a gettext POT file for localization. The WordPress plugin directory actually provides an interface for localizing plugins — see, for example, the translation page for Akismet. Since it looks like your plugin is not in the plugin directory, I'd recommend Transifex, which offers a free plan for Open Source (we use it at Pressbooks). I know there are other solutions out there as well. Hope this helps! And I'm happy to contribute a little time to this. It would be great to see User Interface Options localized/localize-able.

@waharnum

This comment has been minimized.

Copy link
Member

commented Oct 25, 2018

@greatislander UIO has been localize-able for quite some time, it's been the localization we've been lacking. :) We've recently published a number of localized translations for UIO based on work in the last year - you can see the Spanish ones in action at https://karisma-stories.floeproject.org/ and the bundles themselves are in https://github.com/fluid-project/infusion/tree/master/src/framework/preferences/messages and we're working on a language switcher panel that will expose them more easily to end-users.

I'm uncertain when we'll be able to update the plugin to make use of all that, but just updating as to the current status. Thanks for all the info about WP plugins - it's helpful in thinking about how we might improve the plugin further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.