Mở rộng
Mở rộngDịch thêm các phần tử Bricks

Dịch thêm các phần tử Bricks

Gato AI Translations for Polylang có thể dịch các trang và mẫu Bricks dựa trên phần tử.

Plugin đi kèm với hỗ trợ cho tất cả các phần tử do Bricks cung cấp. Đối với các phần tử Bricks tùy chỉnh hoặc của bên thứ ba, bạn có thể mở rộng hỗ trợ dịch thông qua hook PHP.

Dịch chuỗi

Để khai báo các thuộc tính có thể dịch bổ sung trên một phần tử Bricks, hãy sử dụng bộ lọc gatompl:bricks_element_type_translatable_properties.

Bộ lọc nhận một map [elementName => properties]. Mục properties có thể chứa:

  • Tên điều khiển phẳng — ví dụ: 'separatorText'
  • Đường dẫn chấm — ví dụ: 'home.text' (tương đương settings.home.text)
  • Trường repeater — được khai báo là mảng con [repeaterName => [...subFields]]

Các loại này có thể kết hợp tự do, và phần lồng nhau có thể đạt tới bất kỳ độ sâu nào.

Ví dụ, hook này làm cho:

  • Điều khiển phẳng separatorText và đường dẫn chấm home.text có thể dịch được trên phần tử breadcrumbs
  • Trường con title của repeater có thể dịch được bên trong repeater items của phần tử team-members
add_filter(
    'gatompl:bricks_element_type_translatable_properties',
    static function (array $translatableProperties): array {
        $translatableProperties['breadcrumbs'][] = 'separatorText';
        $translatableProperties['breadcrumbs'][] = 'home.text';
        $translatableProperties['team-members']['items'][] = 'title';
        return $translatableProperties;
    },
    10,
    1
);

Cùng một bộ lọc hoạt động cho cả điều khiển đơn giản lẫn trường repeater — không có hook riêng biệt cho repeater.

Dịch tham chiếu thực thể

Một thuộc tính có thể lưu trữ ID của một thực thể (một bài đăng, thuật ngữ phân loại, mục phương tiện hoặc menu) cần được ánh xạ lại sang thực thể tương ứng trong ngôn ngữ đích vào thời điểm dịch. Sử dụng bộ lọc tương ứng:

Loại tham chiếuBộ lọc
Custom post và mediagatompl:bricks_element_type_custompost_and_media_reference_properties
Thuật ngữ phân loạigatompl:bricks_element_type_taxonomy_term_reference_properties
Menu theo IDgatompl:bricks_element_type_menu_reference_by_id_properties
Menu theo sluggatompl:bricks_element_type_menu_reference_by_slug_properties

Cấu trúc giống như bộ lọc thuộc tính có thể dịch — tên phẳng, đường dẫn chấm hoặc mảng con cho repeater.

// Custom post / media reference
add_filter(
    'gatompl:bricks_element_type_custompost_and_media_reference_properties',
    static function (array $properties): array {
        $properties['featured-post'][] = 'post_id';
        $properties['gallery']['items'][] = 'image_id';
        return $properties;
    }
);
 
// Taxonomy term reference
add_filter(
    'gatompl:bricks_element_type_taxonomy_term_reference_properties',
    static function (array $properties): array {
        $properties['related-category'][] = 'category_id';
        return $properties;
    }
);
 
// Menu reference by ID
add_filter(
    'gatompl:bricks_element_type_menu_reference_by_id_properties',
    static function (array $properties): array {
        $properties['menu-picker'][] = 'menu_id';
        return $properties;
    }
);
 
// Menu reference by slug
add_filter(
    'gatompl:bricks_element_type_menu_reference_by_slug_properties',
    static function (array $properties): array {
        $properties['menu-picker'][] = 'menu_slug';
        return $properties;
    }
);

Khám phá tên phần tử và tên thuộc tính

Chạy query GraphQL Translate custom posts và kiểm tra trường bricksData trong phản hồi. Mỗi phần tử hiển thị name và cây settings của nó — đó là nơi bạn sẽ tìm thấy các tên thuộc tính (bao gồm đường dẫn chấm lồng nhau và trường repeater) cần truyền vào các hook ở trên.

Kiểm tra bricksData trong phản hồi GraphQL của Translate custom posts
Kiểm tra bricksData trong phản hồi GraphQL của Translate custom posts

Xem hướng dẫn Truy xuất dữ liệu page builder để dịch để biết cách chạy query đó.

Nơi tìm ví dụ

Các tích hợp của chính plugin là một tài liệu tham khảo hữu ích. Khám phá tệp này bên trong plugin bạn đã cài đặt:

  • wp-content/plugins/gato-ai-translations-for-polylang/src/ConditionalOnContext/LicenseIsActive/ConditionalOnModule/Bricks/Constants/ElementTypes.php