Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Add LocalizeLinks and custom permalinks for each lang #53

Merged
merged 2 commits into from
Nov 19, 2015
Merged
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
52 changes: 52 additions & 0 deletions lib/jekyll/multiple/languages/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def read_posts(dir)
end
end

class Page
def permalink
return nil if data.nil? || data['permalink'].nil?
if site.config['relative_permalinks']
File.join(@dir, data['permalink'])
else
# Look if there's a permalink overwrite specified for this lang
data['permalink_'+site.config['lang']] || data['permalink']
end
end
end

class LocalizeTag < Liquid::Tag

def initialize(tag_name, key, tokens)
Expand Down Expand Up @@ -130,6 +142,44 @@ def render(context)
end
end
end

class LocalizeLink < Liquid::Tag

def initialize(tag_name, key, tokens)
super
@key = key
end

def render(context)
if "#{context[@key]}" != "" #Check for page variable
key = "#{context[@key]}"
else
key = @key
end
key = key.split
namespace = key[0]
lang = key[1] || context.registers[:site].config['lang']
default_lang = context.registers[:site].config['default_lang']
baseurl = context.registers[:site].baseurl
pages = context.registers[:site].pages
url = "";

if default_lang != lang
baseurl = baseurl + "/" + lang
end

for p in pages
unless p['namespace'].nil?
page_namespace = p['namespace']
if namespace == page_namespace
permalink = p['permalink_'+lang] || p['permalink']
url = baseurl + permalink
end
end
end
url
end
end
end

unless Hash.method_defined? :access
Expand All @@ -153,3 +203,5 @@ def access(path)
Liquid::Template.register_tag('translate', Jekyll::LocalizeTag)
Liquid::Template.register_tag('tf', Jekyll::Tags::LocalizeInclude)
Liquid::Template.register_tag('translate_file', Jekyll::Tags::LocalizeInclude)
Liquid::Template.register_tag('tl', Jekyll::LocalizeLink)
Liquid::Template.register_tag('translate_link', Jekyll::LocalizeLink)