Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter page titles to enable translations #450

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/i18n.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Translate Strings

on:
workflow_dispatch:
push:
branches:
- update/generate-i18n-strings
schedule:
- cron: '0 6,18 * * *'

jobs:
translation-strings:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: i18n
uses: WordPress/wporg-repo-tools/.github/actions/i18n@trunk
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --no_taxonomies --post_types=page --url=https://wordpress.org/wp-json/wp/v2/ --textdomain=wporg

- name: Remove the translation context
run: |
sed -i "s/, 'page title'//" extra/translation-strings.php
sed -i "s/_x(/__(/" extra/translation-strings.php

- name: Commit and push
# Using a specific hash here instead of a tagged version, for risk mitigation, since this action modifies our repo.
uses: actions-js/push@a52398fac807b0c1e5f1492c969b477c8560a0ba # 1.3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: update/generate-i18n-strings
message: 'Update translation strings'
52 changes: 52 additions & 0 deletions extra/translation-strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
// phpcs:disable
/**
* Generated file for translation strings.
*
* Used to import additional strings into the GlotPress translation project.
*
* ⚠️ This is a generated file. Do not edit manually. See bin/i18n.php.
* ⚠️ Do not require or include this file anywhere.
*/

__( 'Swag', 'wporg' );
__( 'WordPress 6.5', 'wporg' );
__( 'Data Liberation', 'wporg' );
__( 'State of the Word', 'wporg' );
__( 'WordPress 6.4', 'wporg' );
__( 'Blocks', 'wporg' );
__( 'WordPress 6.3', 'wporg' );
__( 'Remembers', 'wporg' );
__( 'Graphics &amp; Logos', 'wporg' );
__( 'Etiquette', 'wporg' );
__( 'Philosophy', 'wporg' );
__( 'Stats', 'wporg' );
__( 'Privacy', 'wporg' );
__( 'Accessibility', 'wporg' );
__( 'License', 'wporg' );
__( 'Domains', 'wporg' );
__( 'History', 'wporg' );
__( 'Roadmap', 'wporg' );
__( 'Security', 'wporg' );
__( 'Features', 'wporg' );
__( 'Requirements', 'wporg' );
__( 'About', 'wporg' );
__( 'Search', 'wporg' );
__( 'Mobile', 'wporg' );
__( 'Hosting', 'wporg' );
__( 'WordPress and the Journey to 40% of the Web', 'wporg' );
__( 'Media', 'wporg' );
__( 'Integrations', 'wporg' );
__( 'Education', 'wporg' );
__( 'eCommerce', 'wporg' );
__( 'Content Marketing', 'wporg' );
__( 'Enterprise', 'wporg' );
__( 'Home', 'wporg' );
__( 'Counter', 'wporg' );
__( 'Source Code', 'wporg' );
__( 'Beta/Nightly', 'wporg' );
__( 'Release Archive', 'wporg' );
__( 'Download', 'wporg' );
__( 'Erase Personal Data', 'wporg' );
__( 'Export Personal Data', 'wporg' );
__( 'Cookie Policy', 'wporg' );
25 changes: 24 additions & 1 deletion source/wp-content/themes/wporg-main-2022/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
add_filter( 'render_block_core/site-title', __NAMESPACE__ . '\use_parent_page_title', 10, 3 );
add_filter( 'render_block_data', __NAMESPACE__ . '\update_header_template_part_class' );
add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' );
add_filter( 'the_title', __NAMESPACE__ . '\translate_the_title', 1, 2 );
add_filter( 'single_post_title', __NAMESPACE__ . '\translate_the_title', 1, 2 );

/**
* Enqueue scripts and styles.
Expand Down Expand Up @@ -274,7 +276,7 @@ function use_parent_page_title( $block_content, $block, $instance ) {
// Loop up to the first child page, this is the section title.
while ( $parent ) {
$url = get_permalink( $parent->ID );
$title = $parent->post_title;
$title = get_the_title( $parent );
$parent = get_post_parent( $parent );
}

Expand Down Expand Up @@ -319,6 +321,27 @@ function update_header_template_part_class( $block ) {
return $block;
}

/**
* Replace the title with the translated page title.
*
* @param string $title The current title, ignored.
* @param int $post_id The post_id of the page.
*
* @return string
*/
function translate_the_title( $title, $post_id = null ) {
if ( is_admin() ) {
return $title;
}

$post = get_post( $post_id );
if ( $post && 'page' === $post->post_type ) {
$title = translate( $post->post_title, 'wporg' ); // phpcs:ignore
}

return $title;
}

/**
* Prevent easy access to the Site Editor.
*
Expand Down
Loading