Skip to content

Commit

Permalink
Don't translate relative links (#393)
Browse files Browse the repository at this point in the history
* Don't translate href tags pointing to root-relative or path-relative urls.

* Example changes.
  • Loading branch information
dd32 authored Feb 23, 2024
1 parent 1224416 commit dd6c3bf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
23 changes: 21 additions & 2 deletions env/export-content/includes/parsers/HTMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,31 @@ public function to_strings( array $block ) : array {
$found_strings = $matches['string'];
}

// Don't translate anchor links, they correspond to other points on the page.
// Don't translate certain types of links, they correspond to other points on the page.
if ( 'href' === $attr ) {
$found_strings = array_filter(
$found_strings,
function( $value ) {
return ! str_starts_with( $value, '#' );
// Anchors.
if ( str_starts_with( $value, '#' ) ) {
return false;
}

// root relative URLs, which are not protocol-relative.
if ( str_starts_with( $value, '/' ) && ! str_starts_with( $value, '//' ) ) {
return false;
}

// relative to current page urls.
if (
! str_starts_with( $value, 'https://' ) &&
! str_starts_with( $value, 'http://' ) &&
! str_starts_with( $value, '//' )
) {
return false;
}

return true;
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions source/wp-content/themes/wporg-main-2022/patterns/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<!-- /wp:button -->

<!-- wp:button {"className":"is-style-outline-on-dark"} -->
<div class="wp-block-button is-style-outline-on-dark"><a class="wp-block-button__link wp-element-button" href="<?php _e( '/patterns', 'wporg' ); ?>"><?php _e( 'Browse block patterns', 'wporg' ); ?></a></div>
<div class="wp-block-button is-style-outline-on-dark"><a class="wp-block-button__link wp-element-button" href="/patterns"><?php _e( 'Browse block patterns', 'wporg' ); ?></a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:group --></div>
Expand Down Expand Up @@ -208,7 +208,7 @@

<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"className":"is-style-fill"} -->
<div class="wp-block-button is-style-fill"><a class="wp-block-button__link wp-element-button" href="<?php _e( '/gutenberg', 'wporg' ); ?>"><?php _e( 'Try blocks live', 'wporg' ); ?></a></div>
<div class="wp-block-button is-style-fill"><a class="wp-block-button__link wp-element-button" href="/gutenberg"><?php _e( 'Try blocks live', 'wporg' ); ?></a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:column --></div>
Expand Down
2 changes: 1 addition & 1 deletion source/wp-content/themes/wporg-main-2022/patterns/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="<?php _e( '/download/', 'wporg' ); ?>"><?php _e( 'Get WordPress', 'wporg' ); ?></a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="/download/"><?php _e( 'Get WordPress', 'wporg' ); ?></a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:group --></div>
Expand Down

0 comments on commit dd6c3bf

Please sign in to comment.