fluid-project / uio-wordpress-plugin

A WordPress plugin that adds the Infusion User Interface Options component to a site. This repository has been archived and is now read-only. Please contact one of the fluid-project maintainers if you’d like to request it be unarchived for further development. https://fluidproject.atlassian.net/wiki/spaces/fluid/pages/11547481/Get+Involved
12 stars 7 forks source link

Add support for localization via standard WordPress methods (wp_localize_script()) #8

Closed greatislander closed 3 months ago

greatislander commented 6 years ago

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 commented 6 years ago

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:

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

greatislander commented 6 years ago

@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 commented 6 years ago

@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.

jobara commented 3 months ago

The project is no longer actively maintained and will be archived.