Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Add v2 port, fix segment issue, update readme #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Statamic Language Switcher
# Statamic Language Switcher (for v1.x and v2.x)
> Create links to corresponding languages on your site

If you have your multi-lingual Statamic site set up in language folders with identical sub-structures, you can use this plugin to swap out the language segment.
Expand All @@ -15,3 +15,9 @@ For example, on the English 'About the team' page named `/en/about/team`, you mi
* `url`: Defaults to the current page.
* `segment`: If your language segment isn't the first one, enter the number.
* `full_url`: Specify `yes` if you want to output the full http URL.

### Installation (for v2.x)
Open `v2` folder, copy `LangSwitch` to your `site/addons` folder on your v2.x site

### Installation (for v1.x)
Copy `_add-ons/lang_switch` to your `_add-ons` folder on your v1.x site
45 changes: 45 additions & 0 deletions v2/LangSwitch/LangSwitchTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Statamic\Addons\LangSwitch;

use Statamic\Extend\Tags;
use Statamic\API\URL;

class LangSwitchTags extends Tags
{
/**
* The {{ lang_switch }} tag
*
* @return string|array
*/
public function index()
{
// Get parameters
$url = $this->get('url', URL::getCurrent());
$lang = $this->get('lang');
$segment = $this->getInt('segment', 1);
$full_url = $this->get('full_url');

// Don't do anything if there's no language specified
if (!$lang) {
return $url;
}

// Remove the site URL
$site_url = URL::getSiteUrl();
$url = str_replace($site_url, '', $url);


// Swap out the segment
$segments = explode('/', $url);
array_splice($segments, $segment, 0, $lang);
$url = join($segments, '/');

// Make it a full URL if required
if ($full_url) {
$url = $site_url . $url;
}

return $url;
}
}
6 changes: 6 additions & 0 deletions v2/LangSwitch/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: LangSwitch
version: 1.0.0
description: Create links to corresponding languages on your site
url: https://github.com/lesaff/statamicv2-lang_switch
developer: 'Jason Varga & Rudy Affandi (v2 conversion)'
developer_url: https://github.com/lesaff