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

Fix hreflang in source alternate tag #270

Merged
merged 1 commit into from
Jun 14, 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
23 changes: 19 additions & 4 deletions includes/lib/transifex-live-integration-hreflang.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private function generate_languages_hreflang( $raw_url, $languages,
$language_map, $hreflang_map
) {
Plugin_Debug::logTrace();
$source = $this->settings['source_language'];
$url_map = Transifex_Live_Integration_Common::generate_language_url_map( $raw_url, $this->tokenized_url, $language_map );
$ret = [ ];
foreach ($languages as $language) {
Expand All @@ -106,10 +107,11 @@ private function generate_languages_hreflang( $raw_url, $languages,
$protocol = Transifex_Live_Integration_Util::get_http_requested_protocol();
$href_link_parts[0] = $protocol;
$arr['href'] = implode(':', $href_link_parts);
} else {
$arr['href'] = $url_map[$language];
} else {
$arr['href'] = $url_map[$language];
}
$arr['hreflang'] = $hreflang_map[$language];
$arr['is_source'] = ($language === $source);
array_push( $ret, $arr );
}
return $ret;
Expand All @@ -134,10 +136,23 @@ public function render_hreflang() {
$unslashed_source_url = $site_url . $source_url_path;
$source_url = rtrim( $unslashed_source_url, '/' ) . '/';
$hreflang_out = '';
$hreflangs = $this->generate_languages_hreflang( $source_url_path, $this->languages, $this->language_map, $this->hreflang_map );
$source_hreflang = '';
foteinigk marked this conversation as resolved.
Show resolved Hide resolved

foreach ($hreflangs as $hreflang) {
if ($hreflang['is_source']) {
$source_hreflang = $hreflang['hreflang'];
break;
}
}
// If source_hreflang is not found,
// use the source language as default
if (empty($source_hreflang)) {
$source_hreflang = $source;
}
$hreflang_out .= <<<SOURCE
<link rel="alternate" href="$source_url" hreflang="$source"/>\n
<link rel="alternate" href="$source_url" hreflang="$source_hreflang"/>\n
SOURCE;
$hreflangs = $this->generate_languages_hreflang( $source_url_path, $this->languages, $this->language_map, $this->hreflang_map );
foreach ($hreflangs as $hreflang) {
$href_attr = $hreflang['href'];
$hreflang_attr = $hreflang['hreflang'];
Expand Down
68 changes: 32 additions & 36 deletions javascript/transifex-live-integration-settings-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,56 +71,53 @@ function build_cdn_manifest_url(apikey) {
return "https://cdn.transifex.com/" + apikey + "/latest/manifest.jsonp";
}




function transifex_live_integration_convert(l) {
var r = {"type": "div",
function transifex_live_integration_convert(data) {
var result = {"type": "div",
"id": "transifex-languages"};
var t = l['translation'];
var k = l['source'];
var h = [];
var translation = data['translation'];
var source = data['source'];
var html = [];
transifex_languages = [];
language_lookup = [];
language_map = [];
hreflang_map = [];
var arr = {};
var arrr = {};
var arrrr = {};

h.push(transifex_live_integration_mapper(k));
transifex_languages.push(k['code']);
arr['tx_name'] = k['tx_name'];
arr['code'] = k['code'];
language_lookup.push(arr);
arrr[k['code']] = k['code'];
arrrr[k['code']] = k['code'].toLowerCase().replace('_', '-');
var language_data = {};
var language = {};
var hreflang = {};

html.push(transifex_live_integration_mapper(source));
transifex_languages.push(source['code']);
language_data['tx_name'] = source['tx_name'];
language_data['code'] = source['code'];
language_lookup.push(language_data);
language[source['code']] = source['code'];
hreflang[source['code']] = source['code'].toLowerCase().replace('_', '-');

jQuery.each(
t, function (i, o) {
h.push(transifex_live_integration_mapper(o));
translation, function (i, o) {
html.push(transifex_live_integration_mapper(o));
transifex_languages.push(o['code']);
var arr = {};
arr['tx_name'] = o['tx_name'];
arr['code'] = o['code'];
language_lookup.push(arr);
arrr[o['code']] = o['code'];
arrrr[o['code']] = o['code'].toLowerCase().replace('_', '-');
var language_data = {};
language_data['tx_name'] = o['tx_name'];
language_data['code'] = o['code'];
language_lookup.push(language_data);
language[o['code']] = o['code'];
hreflang[o['code']] = o['code'].toLowerCase().replace('_', '-');
}
);
language_map.push(arrr);
hreflang_map.push(arrrr);
language_map.push(language);
hreflang_map.push(hreflang);
var s = {
caption: 'Source:' + l['source']['tx_name'],
caption: 'Source:' + source['tx_name'],
name: "transifex-integration-live-source-language",
id: "transifex-integration-live-[source-language]",
type: "text",
value: l['source']['code']
value: source['code']
};
source_language = l['source']['code'];
r['source'] = s;
r['html'] = h;
return r;
source_language = source['code'];
result['source'] = s;
result['html'] = html;
return result;
}


Expand Down Expand Up @@ -206,7 +203,6 @@ function addTransifexLanguages(obj) {
}
);


} else {
var tlslm = JSON.parse(jQuery('#transifex_live_settings_language_map').val());
var tlshm = JSON.parse(jQuery('#transifex_live_settings_hreflang_map').val());
Expand Down
Loading