public function updateRelativeLink(&$base, &$action) { // Don't inject locale to subpages if ($this->owner->ParentID && SiteTree::config()->nested_urls) { return; } // For blank/temp pages such as Security controller fallback to querystring $locale = Fluent::current_locale(); if (!$this->owner->exists()) { $base = Controller::join_links($base, '?' . Fluent::config()->query_param . '=' . urlencode($locale)); return; } // Check if this locale is the default for its own domain $domain = Fluent::domain_for_locale($locale); if ($locale === Fluent::default_locale($domain)) { // For home page in the default locale, do not alter home url if ($base === null) { return; } // For all pages on a domain where there is only a single locale, // then the domain itself is sufficient to distinguish that domain // See https://github.com/tractorcow/silverstripe-fluent/issues/75 $domainLocales = Fluent::locales($domain); if (count($domainLocales) === 1) { return; } } // Simply join locale root with base relative URL $localeURL = Fluent::alias($locale); $base = Controller::join_links($localeURL, $base); }
/** * Determine the link to this object given the specified $locale. * Returns null for DataObjects that do not have a 'Link' function. * * @param string $locale * @return string */ public function LocaleLink($locale) { // Skip dataobjects that do not have the Link method if (!$this->owner->hasMethod('Link')) { return null; } // Return locale root url if unable to view this item in this locale if ($this->owner->hasMethod('canViewInLocale') && !$this->owner->canViewInLocale($locale)) { return $this->owner->BaseURLForLocale($locale); } // Mock locale and recalculate link $id = $this->owner->ID; $class = $this->owner->ClassName; return Fluent::with_locale($locale, function () use($id, $class, $locale) { $link = DataObject::get($class)->byID($id)->Link(); // Prefix with domain if in cross-domain mode if ($domain = Fluent::domain_for_locale($locale)) { $link = Controller::join_links(Director::protocol() . $domain, $link); } return $link; }); }
/** * Determine the baseurl within a specified $locale. * * @param string $locale Locale, or null to use current locale * @return string */ public static function locale_baseurl($locale = null) { if (empty($locale)) { $locale = Fluent::current_locale(); } // Build domain-specific base url $base = Director::baseURL(); if ($domain = Fluent::domain_for_locale($locale)) { $base = Controller::join_links(Director::protocol() . $domain, $base); } // Don't append locale to home page for default locale if ($locale === self::default_locale()) { return $base; } // Append locale otherwise return Controller::join_links($base, self::alias($locale), '/'); }
/** * Test output for helpers in domain mode */ public function testDomainsHelpers() { Config::inst()->update('Fluent', 'force_domain', true); // Test Fluent::domains $this->assertEquals(array('www.example.com', 'www.example.ca', 'www.example.co.nz'), array_keys(Fluent::domains())); // Test Fluent::default_locale $usDefault = $this->withURL('www.example.com', '/', '/', function ($test) { return Fluent::default_locale(true); }); $this->assertEquals('en_US', $usDefault); $this->assertEquals('en_US', Fluent::default_locale('www.example.com')); $this->assertEquals('fr_CA', Fluent::default_locale()); // Test Fluent::domain_for_locale $this->assertEquals(null, Fluent::domain_for_locale('nl_NL')); $this->assertEquals('www.example.com', Fluent::domain_for_locale('en_US')); $this->assertEquals('www.example.com', Fluent::domain_for_locale('es_ES')); $this->assertEquals('www.example.ca', Fluent::domain_for_locale('fr_CA')); $this->assertEquals('www.example.co.nz', Fluent::domain_for_locale('en_NZ')); // Test Fluent::locales $usLocales = $this->withURL('www.example.com', '/', '/', function ($test) { return Fluent::locales(true); }); $this->assertEquals(array('es_ES', 'en_US'), $usLocales); $this->assertEquals(array('es_ES', 'en_US'), Fluent::locales('www.example.com')); $this->assertEquals(array('fr_CA', 'en_NZ', 'en_US', 'es_ES'), Fluent::locales()); Config::inst()->update('Fluent', 'force_domain', false); }