protected $_translationsForCompilation = [];
+ protected $_urlMaps = null;
+
public function setFields()
{
parent::setFields();
{
$code = '$(".w-webflow-badge").remove();' . "\n";
foreach ($this->getLocales() as $locale) {
- $code .= '$("a#switch-' . $locale['locale'] . '").attr("href","https://' . $locale['url'] . '");' . "\n";
+ $code .= 'if($("html").attr("lang")=="' . $locale['locale'] . '"){$("a#switch-' . $locale['locale'] . '").attr("href",null).attr("nohref","");}else{$("a#switch-' . $locale['locale'] . '").attr("href",SWITCH_URL_' . $locale['locale'] . ');}' . "\n";
}
$js = '$(function(){' . $code . '});';
* @param $locale string
* @return void
*/
- protected function compileHTMLFile($f, $relative, $dest, $isMainLocale, $locale, $texts, $images, $seo, $urlmap)
+ protected function compileHTMLFile($f, $relative, $dest, $isMainLocale, $locale, $texts, $images, $seo)
{
+ $urlmaps = $this->getURLMaps();
+ $urlmap = $urlmaps[$locale];
+ $origRelative = $relative;
+
$html = file_get_contents($f->getPathname());
$regex = '/("https:\/\/' . $this->webflow . '.webflow.io\/\\\\")(.*)(\\\\"")/';
$html = preg_replace($regex, '\"$2\"', $html);
}
}
+ // Replace URL
+ foreach ($urlmap as $k => $v) {
+ $html = str_replace(ltrim($k, '/'), ltrim($v, '/'), $html);
+ }
+
+
+ $localeURL = "<script>";
+ foreach ($this->getLocales() as $l) {
+ $code = $l['locale'];
+ if ($code === $locale) {
+ continue;
+ }
+ $u = $urlmaps[$code][$origRelative] ?? $origRelative;
+ $localeURL .= 'var SWITCH_URL_' . $code . '="https://' . $l['url'] . $u . '";' . "\n";
+ }
+ $localeURL .= "</script>";
+
$html = str_replace('</head>', '<link href="/css/custom.css" rel="stylesheet">' . "\n" . '</head>', $html);
- $html = str_replace('</body>', '<script src="/js/custom.js"></script>' . "\n" . '</body>', $html);
+ $html = str_replace('</body>', $localeURL . '<script src="/js/custom.js"></script>' . "\n" . '</body>', $html);
$html = preg_replace('/\s+lang=\"[a-zA-Z\-_]{2,6}\"/', ' lang="' . $locale . '"', $html);
// Images
- // Replace URL
- foreach ($urlmap as $k => $v) {
- $html = str_replace(ltrim($k, '/'), ltrim($v, '/'), $html);
- }
-
// SEO
if (!$isMainLocale || $htmlPage) {
foreach ($seo as $s) {
file_put_contents($dest . ($urlmap[$relative] ?? $relative), $html);
}
+ public function getURLMaps()
+ {
+
+ if (null === $this->_urlMaps) {
+ $this->_urlMaps = [];
+ foreach ($this->getLocalesCodes() as $locale) {
+ $this->_urlMaps[$locale] = $this->_getURLMap($locale);
+ }
+ }
+ return $this->_urlMaps;
+ }
+
public function getURLMap($locale)
+ {
+ return $this->getURLMaps()[$locale];
+ }
+
+ protected function _getURLMap($locale)
{
if ($locale === $this->getMainLocale()) {
return [];