Skip to content

Commit

Permalink
Add translation support for Custom Post Types and taxonomies #201
Browse files Browse the repository at this point in the history
  • Loading branch information
ronilaukkarinen committed Jan 10, 2024
1 parent c77c676 commit f21aec7
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 44 deletions.
14 changes: 14 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,17 @@
// Run theme setup
add_action( 'after_setup_theme', __NAMESPACE__ . '\theme_setup' );
add_action( 'after_setup_theme', __NAMESPACE__ . '\build_theme_support' );

/*
* First: we register the taxonomies and post types after setup theme
* If air-helper loads (for translations), we unregister the original taxonomies and post types
* and reregister them with the translated ones.
*
* This allows the slugs translations to work before the translations are available,
* and for the label translations to work if they are available.
*/
add_action( 'after_setup_theme', __NAMESPACE__ . '\build_taxonomies' );
add_action( 'after_setup_theme', __NAMESPACE__ . '\build_post_types' );

add_action( 'after_air_helper_init', __NAMESPACE__ . '\rebuild_taxonomies' );
add_action( 'after_air_helper_init', __NAMESPACE__ . '\rebuild_post_types' );
2 changes: 1 addition & 1 deletion inc/includes/localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// $strings[ "Accessibility: {$key}" ] = $value;
// }

return $strings;
return apply_filters( 'air_light_translations', $strings );
} );

function get_default_localization_strings( $language = 'en' ) {
Expand Down
31 changes: 30 additions & 1 deletion inc/includes/post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ abstract class Post_Type {
*/
public $slug;

/**
* Translations used in labels
*
* @var array(string)
*/
public $translations;


public function __construct( $slug ) {
$this->slug = $slug;
$this->translations = [];
}


Expand All @@ -55,14 +63,35 @@ abstract protected function register();
* of failure.
*/
public function register_wp_post_type( $slug, $args ) {
if ( $args['pll_translatable'] ) {
// Register PolyLang translatable only if it's private
if ( $args['pll_translatable'] && false === $args['public'] ) {
add_filter( 'pll_get_post_types', function( $cpts ) use ( $slug ) {
$cpts[ $slug ] = $slug;

return $cpts;
}, 9, 2 );
}

$this->register_translations();
return register_post_type( $slug, $args );
}

// Wrapper for ask__
public function ask__( $key, $value ) {
$pll_key = "{$key}: {$value}";
$this->translations[ $pll_key ] = $value;
if ( function_exists( 'ask__' ) ) {
return ask__( $pll_key );
}

return $value;
}

private function register_translations() {
$translations = $this->translations;

add_filter( 'air_light_translations', function ( $strings ) use ( $translations ) {
return array_merge( $translations, $strings );
}, 10, 2 );
}
}
39 changes: 33 additions & 6 deletions inc/includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* @Author: Niku Hietanen
* @Date: 2020-02-18 15:06:23
* @Last Modified by: Timi Wahalahti
* @Last Modified time: 2023-03-31 14:50:32
* @Last Modified by: Roni Laukkarinen
* @Last Modified time: 2024-01-10 15:35:17
*
* @package air-light
*/
Expand All @@ -16,24 +16,31 @@
abstract class Taxonomy {

/**
* Taxonomy slug or name.
* Taxonomy slug or name
*
* @var string
*/
protected $slug;

/**
* Translations used in labels
*
* @var array(string)
*/
public $translations;

public function __construct( $slug ) {
$this->slug = $slug;
$this->translations = [];
}

/**
* Register taxonomy handler.
* Register taxonomy handler
*/
abstract protected function register();

/**
* Registers a custom taxonomy in WordPress.
* Registers a custom taxonomy in WordPress
*
* @param string $slug Taxonomy slug. Should only contain lowercase letters
* and the underscore character, and not be more than
Expand All @@ -58,13 +65,14 @@ protected function register_wp_taxonomy( $slug, $object_types, $args ) {
}
}, $object_types );

if ( $args['pll_translatable'] ) {
if ( $args['pll_translatable'] && false === $args['public'] ) {
add_filter( 'pll_get_taxonomies', function( $cpts ) use ( $slug ) {
$cpts[ $slug ] = $slug;
return $cpts;
} );
}

$this->register_translations();
register_taxonomy( $slug, $object_types_slugs, $args );

// Note from the Codex: "Better be safe than sorry when registering
Expand All @@ -84,4 +92,23 @@ protected function register_wp_taxonomy( $slug, $object_types, $args ) {

return $registered_object_types;
}

// Wrapper for ask__
public function ask__( $key, $value ) {
$pll_key = "{$key}: {$value}";
$this->translations[ $pll_key ] = $value;
if ( function_exists( 'ask__' ) ) {
return ask__( $pll_key );
}

return $value;
}

private function register_translations() {
$translations = $this->translations;

add_filter( 'air_light_translations', function ( $strings ) use ( $translations ) {
return array_merge( $translations, $strings );
}, 10, 2 );
}
}
50 changes: 43 additions & 7 deletions inc/includes/theme-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ function theme_setup() {
if ( ! isset( $content_width ) ) {
$content_width = THEME_SETTINGS['content_width'];
}

// Run the rest of the setup
build_taxonomies();
build_post_types();
}

/**
Expand All @@ -54,7 +50,10 @@ function build_taxonomies() {
if ( ! file_exists( $file_path ) ) {
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy class file does not exist.', 'air-light' ), $classname );
}
require $file_path;
// Get the class file, only try to require if not already imported
if ( ! class_exists( $classname ) ) {
require $file_path;
}

if ( ! class_exists( $classname ) ) {
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy you attempting to create does not have a class to instance. Possible problems: your configuration does not match the class file name; the class file name does not exist.', 'air-light' ), $classname );
Expand Down Expand Up @@ -82,8 +81,10 @@ function build_post_types() {
if ( ! file_exists( $file_path ) ) {
return new \WP_Error( 'invalid-cpt', __( 'The custom post type class file does not exist.', 'air-light' ), $classname );
}
// Get the class file
require $file_path;
// Get the class file, only try to require if not already imported
if ( ! class_exists( $classname ) ) {
require $file_path;
}

if ( ! class_exists( $classname ) ) {
return new \WP_Error( 'invalid-cpt', __( 'The custom post type you attempting to create does not have a class to instance. Possible problems: your configuration does not match the class file name; the class file name does not exist.', 'air-light' ), $classname );
Expand All @@ -94,6 +95,41 @@ function build_post_types() {
}
}

/**
* Rebuild taxonomies
*/
function rebuild_taxonomies() {
if ( ! is_array( THEME_SETTINGS['taxonomies'] ) || ! THEME_SETTINGS['taxonomies'] ) {
return;
}

foreach ( THEME_SETTINGS['taxonomies'] as $name => $post_types ) {
$slug = strtolower( $name );

unregister_taxonomy( $slug );
}

build_taxonomies();
}

/**
* Rebuild custom post types
*/
function rebuild_post_types() {
if ( ! is_array( THEME_SETTINGS['post_types'] ) || ! THEME_SETTINGS['post_types'] ) {
return;
}

foreach ( THEME_SETTINGS['post_types'] as $name ) {
$slug = strtolower( $name );

unregister_post_type( $slug );
}

build_post_types();
}


/**
* Build theme support
*/
Expand Down
32 changes: 17 additions & 15 deletions inc/post-types/your-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
class Your_Post_Type extends Post_Type {

public function register() {

// Modify all the i18ized strings here.
$generated_labels = [
'menu_name' => __( 'Your Post Type', 'air-light' ),
'name' => _x( 'Your Post Types', 'post type general name', 'air-light' ),
'singular_name' => _x( 'Your Post Type', 'post type singular name', 'air-light' ),
'name_admin_bar' => _x( 'Your Post Type', 'add new on admin bar', 'air-light' ),
'add_new' => _x( 'Add New', 'thing', 'air-light' ),
'add_new_item' => __( 'Add New Your Post Type', 'air-light' ),
'new_item' => __( 'New Your Post Type', 'air-light' ),
'edit_item' => __( 'Edit Your Post Type', 'air-light' ),
'view_item' => __( 'View Your Post Type', 'air-light' ),
'all_items' => __( 'All Your Post Types', 'air-light' ),
'search_items' => __( 'Search Your Post Types', 'air-light' ),
'parent_item_colon' => __( 'Parent Your Post Types:', 'air-light' ),
'not_found' => __( 'No your post types found.', 'air-light' ),
'not_found_in_trash' => __( 'No your post types found in Trash.', 'air-light' ),
// The Post_Type ask__ function wraps the air-helper ask__, and automatically registers the keys to Polylang!
// self::ask__( 'Key', 'Default value' )
// -> Key: Default value => 'Default value'
'menu_name' => self::ask__( 'Your Post Type', 'Your Post Type' ),
'name' => self::ask__( 'Your Post Type', 'Your Post Types' ),
'singular_name' => self::ask__( 'Your Post Type', 'Your Post Type' ),
'name_admin_bar' => self::ask__( 'Your Post Type', 'Your Post Type' ),
'add_new' => self::ask__( 'Your Post Type', 'Add New' ),
'add_new_item' => self::ask__( 'Your Post Type', 'Add New Your Post Type' ),
'new_item' => self::ask__( 'Your Post Type', 'New Your Post Type' ),
'edit_item' => self::ask__( 'Your Post Type', 'Edit Your Post Type' ),
'view_item' => self::ask__( 'Your Post Type', 'View Your Post Type' ),
'all_items' => self::ask__( 'Your Post Type', 'All Your Post Types' ),
'search_items' => self::ask__( 'Your Post Type', 'Search Your Post Types' ),
'parent_item_colon' => self::ask__( 'Your Post Type', 'Parent Your Post Types:' ),
'not_found' => self::ask__( 'Your Post Type', 'No your post types found.' ),
'not_found_in_trash' => self::ask__( 'Your Post Type', 'No your post types found in Trash.' ),
];

// Definition of the post type arguments. For full list see:
Expand Down
28 changes: 14 additions & 14 deletions inc/taxonomies/your-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ class Your_Taxonomy extends Taxonomy {
public function register( array $post_types = [] ) {
// Taxonomy labels.
$labels = [
'name' => _x( 'Your Taxonomies', 'Taxonomy plural name', 'air-light' ),
'singular_name' => _x( 'Your Taxonomy', 'Taxonomy singular name', 'air-light' ),
'search_items' => __( 'Search Your Taxonomies', 'air-light' ),
'popular_items' => __( 'Popular Your Taxonomies', 'air-light' ),
'all_items' => __( 'All Your Taxonomies', 'air-light' ),
'parent_item' => __( 'Parent Your Taxonomy', 'air-light' ),
'parent_item_colon' => __( 'Parent Your Taxonomy', 'air-light' ),
'edit_item' => __( 'Edit Your Taxonomy', 'air-light' ),
'update_item' => __( 'Update Your Taxonomy', 'air-light' ),
'add_new_item' => __( 'Add New Your Taxonomy', 'air-light' ),
'new_item_name' => __( 'New Your Taxonomy', 'air-light' ),
'add_or_remove_items' => __( 'Add or remove Your Taxonomies', 'air-light' ),
'choose_from_most_used' => __( 'Choose from most used Taxonomies', 'air-light' ),
'menu_name' => __( 'Your Taxonomy', 'air-light' ),
'name' => self::ask__( 'Your Taxonomy', 'Taxonomy plural name' ),
'singular_name' => self::ask__( 'Your Taxonomy', 'Taxonomy singular name' ),
'search_items' => self::ask__( 'Your Taxonomy', 'Search Your Taxonomies' ),
'popular_items' => self::ask__( 'Your Taxonomy', 'Popular Your Taxonomies' ),
'all_items' => self::ask__( 'Your Taxonomy', 'All Your Taxonomies' ),
'parent_item' => self::ask__( 'Your Taxonomy', 'Parent Your Taxonomy' ),
'parent_item_colon' => self::ask__( 'Your Taxonomy', 'Parent Your Taxonomy' ),
'edit_item' => self::ask__( 'Your Taxonomy', 'Edit Your Taxonomy' ),
'update_item' => self::ask__( 'Your Taxonomy', 'Update Your Taxonomy' ),
'add_new_item' => self::ask__( 'Your Taxonomy', 'Add New Your Taxonomy' ),
'new_item_name' => self::ask__( 'Your Taxonomy', 'New Your Taxonomy' ),
'add_or_remove_items' => self::ask__( 'Your Taxonomy', 'Add or remove Your Taxonomies' ),
'choose_from_most_used' => self::ask__( 'Your Taxonomy', 'Choose from most used Taxonomies' ),
'menu_name' => self::ask__( 'Your Taxonomy', 'Your Taxonomy' ),
];

$args = [
Expand Down

0 comments on commit f21aec7

Please sign in to comment.