arraypress/wp-i18n-utils

A lean WordPress library for common internationalized options and labels

dev-main 2025-07-02 20:19 UTC

This package is auto-updated.

Last update: 2025-09-02 14:47:51 UTC


README

A lean WordPress library providing commonly used internationalized options and labels that WordPress doesn't provide out of the box. Perfect for forms, Gutenberg blocks, REST APIs, and admin interfaces.

Features

  • 🎯 Fills WordPress Gaps: Provides options WordPress core doesn't offer
  • 🌍 Translation Ready: All strings properly wrapped for internationalization
  • 📋 Dual Formats: Both traditional key/value and modern value/label formats
  • 🔧 Gutenberg Ready: Perfect for block development and REST APIs
  • 🎨 Filterable: Every option set includes context-aware filters
  • Lean & Fast: Only the essentials, no bloat

Requirements

  • PHP 7.4 or later
  • WordPress 5.0 or later

Installation

composer require arraypress/wp-i18n-utils

Basic Usage

Traditional Format (for forms, selects)

use ArrayPress\I18nUtils\Common;
use ArrayPress\I18nUtils\Sizes;
use ArrayPress\I18nUtils\Statuses;

// Get yes/no options
$options = Common::get_yes_no();
// Returns: ['yes' => 'Yes', 'no' => 'No']

// Get size options
$sizes = Sizes::get_basic();
// Returns: ['small' => 'Small', 'medium' => 'Medium', 'large' => 'Large']

// Get workflow statuses
$statuses = Statuses::get_workflow();
// Returns: ['pending' => 'Pending', 'processing' => 'Processing', ...]

Value/Label Format (for Gutenberg, REST APIs)

// Get options in value/label format
$options = Common::get_yes_no( true );
// Returns: [['value' => 'yes', 'label' => 'Yes'], ['value' => 'no', 'label' => 'No']]

// Perfect for Gutenberg block controls
$alignments = Common::get_alignments( true );
// Returns: [['value' => 'left', 'label' => 'Left'], ...]

// Great for REST API responses
$priorities = Statuses::get_priority( true );
// Returns: [['value' => 'low', 'label' => 'Low'], ...]

Available Classes

Common

Basic binary and common options that every project needs:

Common::get_yes_no()              // Yes/No
Common::get_enable_disable()      // Enable/Disable
Common::get_show_hide()           // Show/Hide
Common::get_sort_order()          // Ascending/Descending
Common::get_alignments()          // Left/Center/Right
Common::get_frequencies()         // Once/Daily/Weekly/Monthly
Common::get_adjustment_types()    // Percentage/Flat
Common::get_login_statuses()      // Logged In/Logged Out

Sizes

Size-related options for UI elements and products:

Sizes::get_basic()      // Small/Medium/Large
Sizes::get_extended()   // XS through XXL
Sizes::get_clothing()   // XS/S/M/L/XL/XXL/XXXL

Units

Measurement units for e-commerce and data:

Units::get_weight()     // g/kg/oz/lb
Units::get_length()     // mm/cm/m/in/ft
Units::get_volume()     // ml/l/fl oz/gal

Operators

Comparison operators for filtering and logic:

Operators::get_boolean()  // Equal to/Not equal to
Operators::get_numeric()  // >=, <=, >, <, ==, !=
Operators::get_string()   // Contains, starts with, etc.
Operators::get_array()    // Contains/Not contains

Statuses

Status options for various workflows:

Statuses::get_basic()      // Active/Inactive
Statuses::get_workflow()   // Pending/Processing/Completed/etc.
Statuses::get_payment()    // Paid/Unpaid/Refunded/Failed
Statuses::get_commission() // Unpaid/Paid/Revoked
Statuses::get_task()       // Not Started/In Progress/Completed/etc.
Statuses::get_priority()   // Low/Medium/High
Statuses::get_discount()   // Active/Inactive/Expired/Used
Statuses::get_customer()   // Active/Inactive/Suspended/Pending

WordPress

Enhanced WordPress-specific options:

WordPress::get_post_statuses()  // Simplified post statuses
WordPress::get_image_sizes()    // Clean image size labels

Advanced Usage

Using Context for Filtering

All methods accept an optional context parameter for custom filtering:

// Add custom options based on context
add_filter( 'arraypress_yes_no_options', function ( $options, $context ) {
	if ( $context === 'product_featured' ) {
		return [
			'yes' => 'Featured Product',
			'no'  => 'Regular Product'
		];
	}

	return $options;
}, 10, 2 );

// Use with context
$options = Common::get_yes_no( false, 'product_featured' );

Format Conversion Utilities

use ArrayPress\I18nUtils\Utils;

// Convert between formats
$key_value   = [ 'small' => 'Small', 'large' => 'Large' ];
$value_label = Utils::to_value_label( $key_value );
// Returns: [['value' => 'small', 'label' => 'Small'], ...]

$back_to_key_value = Utils::from_value_label( $value_label );
// Returns: ['small' => 'Small', 'large' => 'Large']

Real-World Examples

Gutenberg Block Control

// In your block's attributes
$attributes = [
	'size' => [
		'type'    => 'string',
		'default' => 'medium'
	]
];

// In your block's edit function
$size_options = Sizes::get_basic( true );
// Perfect for SelectControl options prop

WooCommerce Product Form

// Weight unit dropdown
$weight_units = Units::get_weight();
foreach ( $weight_units as $value => $label ) {
	echo "<option value='{$value}'>{$label}</option>";
}

REST API Endpoint

// Return status options via REST
register_rest_route( 'myapi/v1', '/statuses', [
	'callback' => function () {
		return Statuses::get_workflow( true );
	}
] );

Filtering Examples

Each option set can be filtered using the provided hooks:

// Customize alignment options
add_filter( 'arraypress_alignment_options', function ( $options, $context ) {
	if ( $context === 'text_blocks' ) {
		$options['justify'] = __( 'Justify', 'textdomain' );
	}

	return $options;
}, 10, 2 );

// Add custom workflow status
add_filter( 'arraypress_workflow_statuses', function ( $options ) {
	$options['archived'] = __( 'Archived', 'textdomain' );

	return $options;
} );

Why This Library?

Before:

// Scattered across every project
$sizes = [
	'small'  => __( 'Small', 'textdomain' ),
	'medium' => __( 'Medium', 'textdomain' ),
	'large'  => __( 'Large', 'textdomain' ),
];

// Convert to Gutenberg format manually
$gutenberg_sizes = [];
foreach ( $sizes as $value => $label ) {
	$gutenberg_sizes[] = [ 'value' => $value, 'label' => $label ];
}

After:

// One line, two formats
$sizes           = Sizes::get_basic();              // Traditional
$gutenberg_sizes = Sizes::get_basic( true ); // Gutenberg/REST

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-2.0-or-later License.

Support