plugin development: storing language specific options

I’m currently rewriting a little plugin that shows visitors a small info text. This plugin is German only so far and so I want to add multi language support as I am working on a page that should be in English and German.
The text that is displayed to the visitor can be set in the settings interface of the plugin. The given values are stored in a single option in the database.
In my modified plugin I store the settings in separate options, each for one language. I’ve added a $_GET paramter which is used in the setting registration to create a unique name for a language specific option.

My important (stripped-down) parts of my plugin class:

class Privacy_Policies {
// the language to which the text is translated
public $translation;
// the options, stored in the database
public $options;

public function __construct() {
// use the current user locale as a fallback
$this->translation = isset($_GET[‘lang’]) ? $_GET[‘lang’] : get_user_locale();
// if an option already exists, edit it
// use $this->translation to determine the right language
$this->options = get_option(‘privacy_policies_’ . $this->translation);
// call registration when settings page gets loaded
add_action(‘admin_init’, array($this, ‘register_settings’);
// create the settings page
add_action(‘admin_menu’, array($this, ‘add_settings_page’);
}

// register a distinct option name for each language
// the function is called when the settings page gets loaded
public function register_settings() {
register_setting(
‘privacy_notice_settings_group’,
‘privacy_notice_settings_’ . $this->translation,
array( $this, ‘validate_settings’ )
);
}

// create the html for the settings page
// stripped down to a simple checkbox
public function get_settings_page() { ?>
<h2><?php echo $this->get_plugin_data(‘Name’); ?></h2>
<form method=”post” action=”options.php”>
<?php settings_fields(‘privacy_notice_settings_group’); ?>
<table class=”form-table”>
<tr valign=”top”>
<td>
<!– important: ‘name’ must reflect the option name! –>
<input type=”checkbox” id=”privacy_checkbox” name=”privacy_notice_settings_<?php echo $this->translation ?>[checkbox]” value=”1″
<?php if (isset($this->options[‘checkbox’])) {
checked(‘1’, $this->options[‘checkbox’]);
} ?> />
</td>
</tr>
</table>
</form>
<?php }

// add settings page
// called in the admin_menu hook
public function add_settings_page() {
add_options_page(
‘Privacy Policies Settings’,
‘Privacy Policies’,
‘manage_options’,
‘privacy_notice_settings_group’,
array( $this, ‘get_settings_page’ )
);
}
}

The funny thing is that, whatever locale is in $this->locale the setting will always be stored in the database as setting of WP’s current locale (get_user_locale()). I.e. a setting’s name that has been composed of ‘privacy_notice_settings_’ . $this->translation gets mysteriously recomposed to ‘privacy_notice_settings_’ . get_user_locale().
I suspect the secret lies somewhere burried in options.php which the form is calling. But I seem to be unable to debug this.

Read more here:: plugin development: storing language specific options

Leave a Reply

Your email address will not be published. Required fields are marked *