Skip to content

Commit ce59595

Browse files
committed
adds link translation helper
1 parent be967a7 commit ce59595

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

load.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ function tsdk_utmify( $url, $area, $location = null ) {
121121
$url
122122
)
123123
);
124+
124125
return apply_filters( 'tsdk_utmify_url_' . $filter_key, $utmify_url, $url );
125126
}
126127

@@ -177,6 +178,46 @@ function tsdk_lkey( $file ) {
177178
return \ThemeisleSDK\Modules\Licenser::key( $file );
178179
}
179180
}
181+
182+
if ( ! function_exists( 'tsdk_translate_link' ) ) {
183+
184+
/**
185+
* Function to translate a link based on the current language.
186+
*
187+
* @param string $url URL to translate.
188+
* @param string{'path'|'query'|'domain'} $type Type of localization. Supports path, query and domain.
189+
* @param array $available_languages Available language to choose from.
190+
*
191+
* @return string
192+
*/
193+
function tsdk_translate_link(
194+
$url, $type = 'path', $available_languages = [
195+
'de_DE' => 'de',
196+
'de_DE_formal' => 'de',
197+
]
198+
) {
199+
$language = get_user_locale();
200+
if ( ! isset( $available_languages[ $language ] ) ) {
201+
return $url;
202+
}
203+
$code = $available_languages[ $language ];
204+
// We asume that false is based on query and add the code via query arg.
205+
if ( $type === 'query' ) {
206+
return add_query_arg( 'lang', $code, $url );
207+
}
208+
209+
$parsed_url = wp_parse_url( $url );
210+
// we replace the domain here based on the localized one.
211+
if ( $type === 'domain' ) {
212+
return $parsed_url['scheme'] . '://' . $code . $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
213+
}
214+
// default is the path based approach.
215+
$new_path = isset( $parsed_url['path'] ) ? "/$code" . $parsed_url['path'] : "/$code";
216+
217+
return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $new_path . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' ) . ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
218+
219+
}
220+
}
180221
if ( ! function_exists( 'tsdk_support_link' ) ) {
181222

182223
/**

0 commit comments

Comments
 (0)