Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
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
18 changes: 4 additions & 14 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
source 'https://rubygems.org'



################################################################################
#
# Gemspec
Expand All @@ -13,10 +11,6 @@ source 'https://rubygems.org'
gemspec






################################################################################
#
# Default
Expand All @@ -26,10 +20,6 @@ gemspec
#gem "jekyll", "~> 2.0" # Uncoment to use Jekyll 2






################################################################################
#
# Groups
Expand All @@ -40,15 +30,15 @@ gemspec
# development
#######################################
#group :development do
#
#
#end



#######################################
# test
#######################################
group :test do
gem "jekyll-paginate"
gem "redcarpet"
gem 'jekyll-paginate'
gem 'redcarpet'
gem 'minitest'
end
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Or install it yourself as: `$ gem install jekyll-multiple-languages-plugin`
To activate the plugin add it to the Jekyll `_config.yml` file, under the `gems` option:

```ruby
gems:
gems:
- jekyll-multiple-languages-plugin
```
See the [Jekyll configuration documentation](http://jekyllrb.com/docs/configuration) for details.
Expand Down Expand Up @@ -115,8 +115,9 @@ $ git pull origin master


## 4. Configuration
### 4.1. _config.yml
Add the languages available in your website into your _config.yml (obligatory):
### 4.1. \_config.yml

Add the languages available in your website into your \_config.yml (obligatory):

```yaml
languages: ["sv", "en", "de", "fr"]
Expand All @@ -129,23 +130,24 @@ To avoid redundancy, it is possible to exclude files and folders from being copi
```yaml
exclude_from_localizations: ["javascript", "images", "css"]
```

In code, these specific files should be referenced via `baseurl_root`. E.g.

```
```html
<link rel="stylesheet" href="{{ "/css/bootstrap.css" | prepend: site.baseurl_root }}"/>
```

### 4.2. Folder structure
Create a folder called `_i18n` and add sub-folders for each language, using the same names used on the `languages` setting on the `_config.yml`:

- /_i18n/sv.yml
- /_i18n/en.yml
- /_i18n/de.yml
- /_i18n/fr.yml
- /_i18n/sv/pagename/blockname.md
- /_i18n/en/pagename/blockname.md
- /_i18n/de/pagename/blockname.md
- /_i18n/fr/pagename/blockname.md
- /\_i18n/sv.yml
- /\_i18n/en.yml
- /\_i18n/de.yml
- /\_i18n/fr.yml
- /\_i18n/sv/pagename/blockname.md
- /\_i18n/en/pagename/blockname.md
- /\_i18n/de/pagename/blockname.md
- /\_i18n/fr/pagename/blockname.md



Expand Down Expand Up @@ -352,11 +354,11 @@ permalink: /about/
{% translate_file about/about.md %}
```

Then, create a file named `about.md` under `_i18n/en` with the English content. Repeat this for the other languages (_i18n/es/about.md ...). When running the website, visit the address `http://localhost:4000/about` to see the English version, `http://localhost:4000/es/about` for the Spanish one, etc.

Then, create a file named `about.md` under `_i18n/en` with the English content. Repeat this for the other languages (\_i18n/es/about.md ...). When running the website, visit the address `http://localhost:4000/about` to see the English version, `http://localhost:4000/es/about` for the Spanish one, etc.


## 8. Changelog

* 1.5.1
* Fix a bug (#70) where `site.static_files` would be empty on subsites if `exclude_from_localizations` is used.
* Some overall project enhancements and minor fixes.
Expand Down Expand Up @@ -419,6 +421,16 @@ Then, create a file named `about.md` under `_i18n/en` with the English content.
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

### Test

For testing purpose:

rake test

For test the building:

rake build

### Contributors
| User | Contribution |
| :----------------------------------------------- | :----------------------------------- |
Expand Down
18 changes: 6 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
require "bundler/gem_tasks"



#######################################
# default
#######################################
task :default => [:test]



#######################################
# test
#######################################
# A simple test which buils the example site.
desc "Run tests"
task :test do

task :build do
cd "example" do
sh "bundle exec jekyll build"
end
end

task :test do
sh "ruby test/base_test.rb"
end
4 changes: 2 additions & 2 deletions example/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ paginate_path: "/pagination/page:num/"


# Plugins
gems:
plugins:
- jekyll-multiple-languages-plugin
- jekyll-paginate


# jekyll-multiple-languages-plugin settings:
languages: ["it", "en", "es"]

exclude_from_localizations: ["javascript", "images", "css", "scripts", "favicon.ico"]
exclude_from_localizations: ["javascript", "images", "css", "scripts", "favicon.ico", "robots.txt"]
4 changes: 4 additions & 0 deletions example/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
permalink: robots.txt
---
Test for dynamic content that have to be excluded from localization folders.
164 changes: 164 additions & 0 deletions lib/custom_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
module Jekyll

# Implements the plugin Liquid Tags and/or Filters

##############################################################################
# class LocalizeTag
#
# Localization by getting localized text from YAML files.
# User must use the "t" or "translate" liquid tags.
##############################################################################
class LocalizeTag < Liquid::Tag

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

def render(context)
if "#{context[@key]}" != "" # Check for page variable
key = "#{context[@key]}"
else
key = @key
end

key = Liquid::Template.parse(key).render(context) # Parses and renders some Liquid syntax on arguments (allows expansions)

site = context.registers[:site] # Jekyll site object

lang = site.config['lang']

unless site.parsed_translations.has_key?(lang)
puts "Loading translation from file #{site.source}/_i18n/#{lang}.yml"
site.parsed_translations[lang] = YAML.load_file("#{site.source}/_i18n/#{lang}.yml")
end

translation = site.parsed_translations[lang].access(key) if key.is_a?(String)

if translation.nil? or translation.empty?
translation = site.parsed_translations[site.config['default_lang']].access(key)

puts "Missing i18n key: #{lang}:#{key}"
puts "Using translation '%s' from default language: %s" %[translation, site.config['default_lang']]
end

translation
end
end


##############################################################################
# class LocalizeInclude
#
# Localization by including whole files that contain the localization text.
# User must use the "tf" or "translate_file" liquid tags.
##############################################################################
module Tags
class LocalizeInclude < IncludeTag

def render(context)
if "#{context[@file]}" != "" # Check for page variable
file = "#{context[@file]}"
else
file = @file
end

file = Liquid::Template.parse(file).render(context) # Parses and renders some Liquid syntax on arguments (allows expansions)

site = context.registers[:site] # Jekyll site object

includes_dir = File.join(site.source, '_i18n/' + site.config['lang'])

validate_file_name(file)

Dir.chdir(includes_dir) do
choices = Dir['**/*'].reject { |x| File.symlink?(x) }

if choices.include?( file)
source = File.read(file)
partial = Liquid::Template.parse(source)

context.stack do
context['include'] = parse_params( context) if @params
contents = partial.render(context)
ext = File.extname(file)

converter = site.converters.find { |c| c.matches(ext) }
contents = converter.convert(contents) unless converter.nil?

contents
end
else
raise IOError.new "Included file '#{file}' not found in #{includes_dir} directory"
end

end
end
end
end



##############################################################################
# class LocalizeLink
#
# Creates links or permalinks for translated pages.
# User must use the "tl" or "translate_link" liquid tags.
##############################################################################
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 = Liquid::Template.parse(key).render(context) # Parses and renders some Liquid syntax on arguments (allows expansions)

site = context.registers[:site] # Jekyll site object

key = key.split
namespace = key[0]
lang = key[1] || site.config[ 'lang']
default_lang = site.config['default_lang']
baseurl = site.baseurl
pages = 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



################################################################################
# Liquid tags definitions

Liquid::Template.register_tag('t', Jekyll::LocalizeTag )
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 )
Loading