Skip to content

Commit

Permalink
Added custom language support feature. (theme-next#253)
Browse files Browse the repository at this point in the history
* Added custom language support feature.

* Fix `hexo` variables in linter by adding `global hexo` comment.

* Check Stickler CI.

* Error: lines-around-comment

* Fix error: lines-around-comment
  • Loading branch information
ivan-nginx authored and Alex LEE committed May 14, 2018
1 parent 725d13d commit 3b2f980
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 35 deletions.
6 changes: 6 additions & 0 deletions layout/_layout.swig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
{# NexT version #}
{% set version = next_env('version') %}

{# Language & Config #}
{% set title = __('title') !== 'title' && __('title') || config.title %}
{% set subtitle = __('subtitle') !== 'subtitle' && __('subtitle') || config.subtitle %}
{% set author = __('author') !== 'author' && __('author') || config.author %}
{% set description = __('description') !== 'description' && __('description') || config.description %}

{% set html_class = 'theme-next ' + theme.scheme %}
{% if theme.motion.enable %}
{% set html_class = html_class + ' use-motion' %}
Expand Down
2 changes: 1 addition & 1 deletion layout/_macro/post-copyright.swig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ul class="post-copyright">
<li class="post-copyright-author">
<strong>{{ __('post.copyright.author') + __('symbol.colon') }} </strong>{#
#}{{ post.author | default(config.author) }}{#
#}{{ post.author | default(author) }}{#
#}</li>
<li class="post-copyright-link">
<strong>{{ __('post.copyright.link') + __('symbol.colon') }}</strong>
Expand Down
9 changes: 6 additions & 3 deletions layout/_macro/post.swig
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
<link itemprop="mainEntityOfPage" href="{{ config.url }}{{ url_for(post.path) }}">

<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="{{ theme.author }}">
<meta itemprop="description" content="{{ theme.signature }}">
<meta itemprop="name" content="{{ author }}">
<meta itemprop="description" content="{{ description }}">
<meta itemprop="image" content="{{ url_for( theme.avatar.url | default(theme.images + '/avatar.gif') ) }}">
</span>

<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="{{ config.title }}">
<meta itemprop="name" content="{{ title }}">
</span>

{% if not headlessPost %}
Expand Down Expand Up @@ -166,6 +166,9 @@
<i class="fa fa-comment-o"></i>
</span>
<a href="{{ url_for(post.path) }}#comments" itemprop="discussionUrl">
{% if __('disqus') !== 'disqus' -%}
<span class="post-meta-item-text">{{ __('post.comments_count') + __('symbol.colon') }}</span>
{% endif %}
<span class="post-comments-count disqus-comment-count"
data-disqus-identifier="{{ post.path }}" itemprop="commentCount"></span>
</a>
Expand Down
6 changes: 3 additions & 3 deletions layout/_macro/reward.swig
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

{% if theme.wechatpay %}
<div id="wechat" style="display: inline-block">
<img id="wechat_qr" src="{{ url_for(theme.wechatpay) }}" alt="{{ theme.author }} {{ __('reward.wechatpay') }}"/>
<img id="wechat_qr" src="{{ url_for(theme.wechatpay) }}" alt="{{ author }} {{ __('reward.wechatpay') }}"/>
<p>{{ __('reward.wechatpay') }}</p>
</div>
{% endif %}

{% if theme.alipay %}
<div id="alipay" style="display: inline-block">
<img id="alipay_qr" src="{{ url_for(theme.alipay) }}" alt="{{ theme.author }} {{ __('reward.alipay') }}"/>
<img id="alipay_qr" src="{{ url_for(theme.alipay) }}" alt="{{ author }} {{ __('reward.alipay') }}"/>
<p>{{ __('reward.alipay') }}</p>
</div>
{% endif %}

{% if theme.bitcoin %}
<div id="bitcoin" style="display: inline-block">
<img id="bitcoin_qr" src="{{ url_for(theme.bitcoin) }}" alt="{{ theme.author }} {{ __('reward.bitcoin') }}"/>
<img id="bitcoin_qr" src="{{ url_for(theme.bitcoin) }}" alt="{{ author }} {{ __('reward.bitcoin') }}"/>
<p>{{ __('reward.bitcoin') }}</p>
</div>
{% endif %}
Expand Down
6 changes: 3 additions & 3 deletions layout/_macro/sidebar.swig
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
{% if theme.avatar.url %}
<img class="site-author-image" itemprop="image"
src="{{ url_for( theme.avatar.url | default(theme.images + '/avatar.gif') ) }}"
alt="{{ theme.author }}" />
alt="{{ author }}" />
{% endif %}
<p class="site-author-name" itemprop="name">{{ theme.author }}</p>
<p class="site-author-name" itemprop="name">{{ author }}</p>
<p class="site-description motion-element" itemprop="description">{#
#}{{ theme.description }}{#
#}{{ description }}{#
#}</p>
</div>

Expand Down
2 changes: 1 addition & 1 deletion layout/_macro/wechat-subscriber.swig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
<img id="wechat_subscriber_qcode" src="{{ url_for(theme.wechat_subscriber.qcode) }}" alt="{{ theme.author }} wechat" style="width: 200px; max-width: 100%;"/>
<img id="wechat_subscriber_qcode" src="{{ url_for(theme.wechat_subscriber.qcode) }}" alt="{{ author }} wechat" style="width: 200px; max-width: 100%;"/>
<div>{{ theme.wechat_subscriber.description }}</div>
</div>
2 changes: 1 addition & 1 deletion layout/_partials/footer.swig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<span class="with-love" id="animate">
<i class="fa fa-{{ theme.footer.icon.name }}"></i>
</span>
<span class="author" itemprop="copyrightHolder">{{ theme.footer.copyright || config.author }}</span>
<span class="author" itemprop="copyrightHolder">{{ theme.footer.copyright | default(author) }}</span>

{% if config.symbols_count_time.total_symbols %}
<span class="post-meta-divider">|</span>
Expand Down
2 changes: 1 addition & 1 deletion layout/_partials/head/head-unique.swig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% set theme.rss = config.feed.path %}
{% endif %}
{% if theme.rss %}
<link rel="alternate" href="{{ url_for(theme.rss) }}" title="{{ config.title }}" type="application/atom+xml" />
<link rel="alternate" href="{{ url_for(theme.rss) }}" title="{{ title }}" type="application/atom+xml" />
{% endif %}

{# Canonical, good for google search engine (SEO) : https://support.google.com/webmasters/answer/139066 #}
Expand Down
10 changes: 6 additions & 4 deletions layout/_partials/header/brand.swig
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
<div class="site-meta-headline">
<a>
<img class="custom-logo-image" src="{{ theme.custom_logo.image }}"
alt="{{ config.title }}"/>
alt="{{ title }}"/>
</a>
</div>
{% endif %}

<div class="custom-logo-site-title">
<a href="{{ config.root }}" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">{{ config.title }}</span>
<span class="site-title">{{ title }}</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
{% if subtitle %}
{% if theme.seo %}
<h1 class="site-subtitle" itemprop="description">{{ config.subtitle }}</h1>
<h1 class="site-subtitle" itemprop="description">{{ subtitle }}</h1>
{% else %}
<p class="site-subtitle">{{ config.subtitle }}</p>
<p class="site-subtitle">{{ subtitle }}</p>
{% endif %}
{% endif %}
</div>

<div class="site-nav-toggle">
Expand Down
1 change: 0 additions & 1 deletion layout/_partials/header/index.swig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{{ partial('_partials/header/brand.swig', {}, {cache: theme.cache.enable}) }}
{#{% include 'brand.swig' %}#}
{% include 'menu.swig' %}

{% if theme.custom_file_path.header %}
Expand Down
5 changes: 4 additions & 1 deletion layout/_third-party/comments/disqus.swig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if theme.disqus.enable %}

{% if theme.disqus.count %}
{% if theme.disqus.count %}
<script id="dsq-count-scr" src="https://{{theme.disqus.shortname}}.disqus.com/count.js" async></script>
{% endif %}

Expand All @@ -10,6 +10,9 @@
this.page.url = '{{ page.permalink }}';
this.page.identifier = '{{ page.path }}';
this.page.title = '{{ page.title| addslashes }}';
{% if __('disqus') !== 'disqus' -%}
this.language = '{{ __('disqus') }}';
{% endif -%}
};
function loadComments () {
var d = document, s = d.createElement('script');
Expand Down
2 changes: 1 addition & 1 deletion layout/archive.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% import '_macro/post-collapse.swig' as post_template %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{{ __('title.archive') }} | {{ config.title }}{% endblock %}
{% block title %}{{ __('title.archive') }} | {{ title }}{% endblock %}

{% block page_class %}page-archive{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion layout/category.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% import '_macro/post-collapse.swig' as post_template %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{{ __('title.category') }}: {{ page.category }} | {{ config.title }}{% endblock %}
{% block title %}{{ __('title.category') }}: {{ page.category }} | {{ title }}{% endblock %}

{% block content %}

Expand Down
2 changes: 1 addition & 1 deletion layout/index.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% import '_macro/post.swig' as post_template %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{{ config.title }}{% if theme.index_with_subtitle and config.subtitle %} - {{config.subtitle }}{% endif %}{% endblock %}
{% block title %}{{ title }}{% if theme.index_with_subtitle and subtitle %} &mdash; {{ subtitle }}{% endif %}{% endblock %}

{% block page_class %}
{% if is_home() %}page-home{% endif -%}
Expand Down
2 changes: 1 addition & 1 deletion layout/page.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{#
#}{% set page_title_suffix = ' | ' + config.title %}{#
#}{% set page_title_suffix = ' | ' + title %}{#

#}{% if page.type === "categories" and not page.title %}{#
#}{{ __('title.category') + page_title_suffix }}{#
Expand Down
2 changes: 1 addition & 1 deletion layout/post.swig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% import '_macro/sidebar.swig' as sidebar_template %}


{% block title %}{{ page.title }} | {{ config.title }}{% endblock %}
{% block title %}{{ page.title }} | {{ title }}{% endblock %}

{% block page_class %}page-post-detail{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion layout/schedule.swig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends '_layout.swig' %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{{ __('title.schedule') }} | {{ config.title }}{% endblock %}
{% block title %}{{ __('title.schedule') }} | {{ title }}{% endblock %}

{% block page_class %}page-post-detail page-calendar{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion layout/tag.swig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% import '_macro/post-collapse.swig' as post_template %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{{ __('title.tag') }}: {{ page.tag }} | {{ config.title }}{% endblock %}
{% block title %}{{ __('title.tag') }}: {{ page.tag }} | {{ title }}{% endblock %}

{% block content %}

Expand Down
36 changes: 27 additions & 9 deletions scripts/merge-configs.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
/* global hexo */

'use strict';

var merge = require('./merge');

/**
* Merge configs from _data/next.yml into hexo.theme.config.
* Note: configs in _data/next.yml will rewrite or override configs in hexo.theme.config.
*/
hexo.on('generateBefore', function () {
hexo.on('generateBefore', function() {
if (hexo.locals.get) {
var data = hexo.locals.get('data');

/**
* Merge configs from _data/next.yml into hexo.theme.config.
* If `override`, configs in next.yml will rewrite configs in hexo.theme.config.
* If next.yml not exists, merge all `theme_config.*` into hexo.theme.config.
*/
if (data && data.next) {
if (data.next.override) {
hexo.theme.config = data.next;
} else {
merge(hexo.config, data.next);
merge(hexo.theme.config, data.next);
}
/**
* If next.yml not exists, then merge all `theme_config.*`
* options from main Hexo config into hexo.theme.config.
*/
} else {
merge(hexo.theme.config, hexo.config.theme_config);
}

// Custom languages support. Introduced in NexT v6.3.0.
if (data && data.languages) {
var lang = this.config.language;
var i18n = this.theme.i18n;

var mergeLang = function(lang) {
i18n.set(lang, merge(i18n.get([lang]), data.languages[lang]));
};

if (Array.isArray(lang)) {
for (var i = 0; i < lang.length; i++) {
mergeLang(lang[i]);
}
} else {
mergeLang(lang);
}
}
}
});

0 comments on commit 3b2f980

Please sign in to comment.