Skip to content

Commit

Permalink
adds link translation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Apr 10, 2024
1 parent be967a7 commit ce59595
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function tsdk_utmify( $url, $area, $location = null ) {
$url
)
);

return apply_filters( 'tsdk_utmify_url_' . $filter_key, $utmify_url, $url );
}

Expand Down Expand Up @@ -177,6 +178,46 @@ function tsdk_lkey( $file ) {
return \ThemeisleSDK\Modules\Licenser::key( $file );
}
}

if ( ! function_exists( 'tsdk_translate_link' ) ) {

/**
* Function to translate a link based on the current language.
*
* @param string $url URL to translate.
* @param string{'path'|'query'|'domain'} $type Type of localization. Supports path, query and domain.
* @param array $available_languages Available language to choose from.
*
* @return string
*/
function tsdk_translate_link(
$url, $type = 'path', $available_languages = [
'de_DE' => 'de',
'de_DE_formal' => 'de',
]
) {
$language = get_user_locale();
if ( ! isset( $available_languages[ $language ] ) ) {
return $url;
}
$code = $available_languages[ $language ];
// We asume that false is based on query and add the code via query arg.
if ( $type === 'query' ) {
return add_query_arg( 'lang', $code, $url );
}

$parsed_url = wp_parse_url( $url );
// we replace the domain here based on the localized one.
if ( $type === 'domain' ) {
return $parsed_url['scheme'] . '://' . $code . $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
}
// default is the path based approach.
$new_path = isset( $parsed_url['path'] ) ? "/$code" . $parsed_url['path'] : "/$code";

return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $new_path . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );

}
}
if ( ! function_exists( 'tsdk_support_link' ) ) {

/**
Expand Down

0 comments on commit ce59595

Please sign in to comment.