<commit_before>Translatable Routing
=================

Extends SilverStripe Translatable module and replaces routing to enable multi-lingual urls

## Maintainer Contact
* <NAME> ([UndefinedOffset](https://github.com/UndefinedOffset))

## Requirements
* SilverStripe CMS 3.0.x
* SilverStripe Translatable 1.0.x
* SilverStripe Google Sitemaps for 3.0


## Installation
* Download the module from here https://github.com/webbuilders-group/silverstripe-translatablerouting/archive/master.zip
* Extract the downloaded archive into your site root so that the destination folder is called translatablerouting, opening the extracted folder should contain _config.php in the root along with other files/folders
* Run dev/build?flush=all to regenerate the manifest


## Usage
You must add the following methods to your Page.php for this module to function properly.
```php
/**
 * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included.
 * @param {string} $action Optional controller action (method). Note: URI encoding of this parameter is applied automatically through template casting, don't encode the passed parameter. Please use {@link Controller::join_links()} instead to append GET parameters.
 * @return {string}
 */
public function Link($action=null) {
    return Controller::join_links(Director::baseURL(), (MultilingualRootURLController::get_use_locale_url() ? $this->Locale:i18n::get_lang_from_locale($this->Locale)), $this->RelativeLink($action));
}

/**
 * Return the link for this {@link SiteTree} object relative to the SilverStripe root. By default, it this page is the current home page, and there is no action specified then this will return a link to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten
 * and returned in its full form.
 * @param {string} $action See {@link Link()}
 * @return {string}
 * 
 * @uses Translatable::get_homepage_link_by_locale()
 */
public function RelativeLink($action=null) {
    if($this->ParentID && self::nested_urls()) {
        $base=$this->Parent()->RelativeLink($this->URLSegment);
    } else {
        $base=$this->URLSegment;
    }
    
    //Unset base for homepage URLSegments in their default language. Homepages with action parameters or in different languages need to retain their URLSegment. We can only do this if the homepage is on the root level.
    if(!$action && $base==Translatable::get_homepage_link_by_locale($this->Locale) && !$this->ParentID) {
        $base = null;
    }

    return Controller::join_links($base, '/', $action);
}
```

#### Locale URL's
If you want to use locale's (for example en_US) as your url pattern instead of just languages you need to add this to your mysite/_config.php
```php
MultilingualRootURLController::set_use_locale_url(true);
```<commit_msg>Added and updated the notes about changes to Page.php<commit_after>Translatable Routing
=================

Extends SilverStripe Translatable module and replaces routing to enable multi-lingual urls

## Maintainer Contact
* <NAME> ([UndefinedOffset](https://github.com/UndefinedOffset))

## Requirements
* SilverStripe CMS 3.0.x
* SilverStripe Translatable 1.0.x
* SilverStripe Google Sitemaps for 3.0


## Installation
* Download the module from here https://github.com/webbuilders-group/silverstripe-translatablerouting/archive/master.zip
* Extract the downloaded archive into your site root so that the destination folder is called translatablerouting, opening the extracted folder should contain _config.php in the root along with other files/folders
* Run dev/build?flush=all to regenerate the manifest


## Usage
You must add the following methods to your Page class for this module to function properly.
```php
/**
 * Return the link for this {@link SiteTree} object, with the {@link Director::baseURL()} included.
 * @param {string} $action Optional controller action (method). Note: URI encoding of this parameter is applied automatically through template casting, don't encode the passed parameter. Please use {@link Controller::join_links()} instead to append GET parameters.
 * @return {string}
 */
public function Link($action=null) {
    return Controller::join_links(Director::baseURL(), (MultilingualRootURLController::get_use_locale_url() ? $this->Locale:i18n::get_lang_from_locale($this->Locale)), $this->RelativeLink($action));
}

/**
 * Return the link for this {@link SiteTree} object relative to the SilverStripe root. By default, it this page is the current home page, and there is no action specified then this will return a link to the root of the site. However, if you set the $action parameter to TRUE then the link will not be rewritten
 * and returned in its full form.
 * @param {string} $action See {@link Link()}
 * @return {string}
 * 
 * @uses Translatable::get_homepage_link_by_locale()
 */
public function RelativeLink($action=null) {
    if($this->ParentID && self::nested_urls()) {
        $base=$this->Parent()->RelativeLink($this->URLSegment);
    } else {
        $base=$this->URLSegment;
    }
    
    //Unset base for homepage URLSegments in their default language. Homepages with action parameters or in different languages need to retain their URLSegment. We can only do this if the homepage is on the root level.
    if(!$action && $base==Translatable::get_homepage_link_by_locale($this->Locale) && !$this->ParentID) {
        $base = null;
    }

    return Controller::join_links($base, '/', $action);
}
```

As well for your Page_Controller class you must add this for this module to function properly.
``php
public function init() {
    parent::init();
    
    
    // If we've accessed the homepage as /home/, then we should redirect to /.
    if($this->dataRecord && $this->dataRecord instanceof SiteTree && MultilingualRootURLController::should_be_on_root($this->dataRecord) && (!isset($this->urlParams['Action']) || !$this->urlParams['Action']) && !$_POST && !$_FILES && !$this->redirectedTo()) {
        $getVars=$_GET;
        unset($getVars['url']);
        
        if($getVars) {
            $url=i18n::get_lang_from_locale($this->Locale).'/?'.http_build_query($getVars);
        }else {
            $url=i18n::get_lang_from_locale($this->Locale).'/';
        }
        
        $this->redirect($url, 301);
        return;
    }
}
```

#### Locale URL's
If you want to use locale's (for example en_US) as your url pattern instead of just languages you need to add this to your mysite/_config.php
```php
MultilingualRootURLController::set_use_locale_url(true);
```
{'fraction_non_alphanumeric': 0.11561309977151561, 'fraction_numerical': 0.0035034272658035033, 'mean_word_length': 4.55969517358171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3123158', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1810, 'n_words': 657}