Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
fix(i18n): improvements on plugin multi-language support
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoffice committed Apr 18, 2018
1 parent 259592d commit 3e9a9a2
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 101 deletions.
1 change: 1 addition & 0 deletions languages/en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: 'English'
common:
archives: 'Archives'
category: 'Category'
Expand Down
1 change: 1 addition & 0 deletions languages/ru.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: 'RU'
nav:
next: 'Далее'
prev: 'Назад'
Expand Down
1 change: 1 addition & 0 deletions languages/zh-cn.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: '简体中文'
common:
archives: '归档'
category: '分类'
Expand Down
61 changes: 58 additions & 3 deletions layout/comment/disqus.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
<% if (has_config('comment.shortname')) { %>
<% if (has_config('comment.shortname')) {
// Disqus supported languages.
// Last update at 4/18/2018
const SUPPORTED_LANGS = {
'sq': 'Albanian',
'ar': 'Arabic',
'az': 'Azerbaijani',
'eu': 'Basque',
'ca': 'Catalan',
'zh': 'Chinese',
'cs': 'Czech',
'da': 'Danish',
'nl': 'Dutch',
'en': 'English',
'fi': 'Finnish',
'fr': 'French',
'de': 'German',
'el': 'Greek',
'he': 'Hebrew',
'hu': 'Hungarian',
'id': 'Indonesian',
'it': 'Italian',
'ja': 'Japanese',
'ko': 'Korean',
'lv': 'Latvian',
'nb': 'Norwegian Bokmål',
'fa': 'Persian',
'pl': 'Polish',
'pt': 'Portuguese',
'pt_BR': 'Portuguese (Brazil)',
'ro': 'Romanian',
'ru': 'Russian',
'sr': 'Serbian',
'sr@latin': 'Serbian (Latin)',
'sk': 'Slovak',
'sl': 'Slovenian',
'es_AR': 'Spanish (Argentina)',
'es_MX': 'Spanish (Mexico)',
'es_ES': 'Spanish (Spain)',
'sv_SE': 'Swedish (Sweden)',
'tr': 'Turkish',
'uk': 'Ukrainian',
'vec': 'Venetian'
};
function getDisqusLanguage(language) {
const variant = language.split(/[-_]/).map((l, i) => i === 0 ? l.toLowerCase() : l.toUpperCase()).join('_');
if (SUPPORTED_LANGS.hasOwnProperty(variant)) {
return variant;
}
if (SUPPORTED_LANGS.hasOwnProperty(variant.split('_')[0])) {
return variant.split('_')[0];
}
return 'en';
}
%>
<script>
var disqus_config = function () {
this.page.url = '<%= page.permalink %>';
this.page.identifier = '<%= page.disqusId || page.path %>';
<% if (lang()) { %>
this.language = '<%= lang().split('-')[0] %>';
<% if (page_language()) { %>
this.language = '<%= getDisqusLanguage(page_language()) %>';
<% } %>
};
(function() {
Expand Down
2 changes: 1 addition & 1 deletion layout/comment/facebook.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/<%= lang() ? lang().split('-').join('_') : 'en' %>/sdk.js#xfbml=1&version=v2.8";
js.src = "//connect.facebook.net/<%= closest_rfc5646_with_country_code(page_language()) %>/sdk.js#xfbml=1&version=v2.8";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-comments" data-width="100%" data-href="<%= page.permalink %>" data-num-posts="5"></div>
2 changes: 1 addition & 1 deletion layout/common/article.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<% if (post.categories && post.categories.length){ %>
<span class="column is-narrow article-category">
<i class="far fa-folder"></i>
<%- post._categories.map(category =>
<%- (post._categories || post.categories).map(category =>
`<a class="article-category-link" href="${url_for(category.path)}">${category.name}</a>`)
.join('<span>></span>') %>
</span>
Expand Down
2 changes: 1 addition & 1 deletion layout/common/footer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<% } %>
</div>
</div>
<%- partial('common/languages') %>
<% } %>
<%- partial('common/languages') %>
</div>
</div>
</footer>
17 changes: 9 additions & 8 deletions layout/common/languages.ejs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<% const languages = get_config('language', [], true);
if (Array.isArray(languages) && languages.length > 1) {%>
<% const languages = display_languages();
if (languages.length > 1) {%>
<div class="column is-narrow has-text-centered">
<div class="dropdown is-up is-right is-hoverable" style="margin-top: -0.2em;">
<div class="dropdown-trigger">
<button class="button is-small" aria-haspopup="true" aria-controls="dropdown-menu7">
<span class="icon">
<i class="fas fa-globe"></i>
</span>
<span><%= lang() ? lang() : languages[0] %></span>
<span><%= language_name(page_language()) %></span>
<span class="icon is-small">
<i class="fas fa-angle-down" aria-hidden="true"></i>
</span>
</button>
</div>
<div class="dropdown-menu has-text-left" role="menu">
<div class="dropdown-content">
<% languages.forEach((language, i) => {
if (language !== 'default') { %>
<a href="<%= url_for(i18n_path(i > 0 ? language : null)) %>" class="dropdown-item"><%= language %></a>
<% }
}) %>
<% languages.forEach((language) => { %>
<a href="<%= url_for(i18n_path(!is_default_language(language) ? language : null)) %>" class="dropdown-item">
<%= language_name(language) %>
</a>
<% }) %>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion layout/common/navbar.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<nav class="navbar is-transparent is-fixed-top navbar-main" role="navigation" aria-label="main navigation">
<div class="container">
<div class="navbar-brand">
<a class="navbar-item navbar-logo" href="<%- url_for('/' + (lang() ? lang() : '')) %>">
<a class="navbar-item navbar-logo" href="<%- url_for('/' + (!is_default_language(page_language()) ? page_language() : '')) %>">
<% if (has_config('logo') && get_config('logo')) { %>
<% if (has_config('logo.text') && get_config('logo.text')) { %>
<%= get_config('logo.text') %>
Expand Down
2 changes: 1 addition & 1 deletion layout/search/insight.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
TAGS: '<%= __("insight.tags") %>',
UNTITLED: '<%= __("insight.untitled") %>',
},
CONTENT_URL: '<%- url_for("/content" + (lang() ? '.' + lang() : '') + ".json")%>',
CONTENT_URL: '<%- url_for("/content" + (!is_default_language(page_language()) ? '.' + page_language() : '') + ".json")%>',
};
window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
Expand Down
15 changes: 14 additions & 1 deletion scripts/01_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const fs = require('fs');
const path = require('path');
const logger = require('hexo-log')();

const { getUsedLanguages, getDisplayLanguages, isLanguageValid } = require('./10_i18n');

logger.info(`=======================================
███╗ ███╗ ██╗ ███╗ ██╗ ██████╗ ███████╗
████╗ ████║ ██║ ████╗ ██║ ██╔═══██╗ ██╔════╝
Expand Down Expand Up @@ -45,6 +47,17 @@ if (missingDeps) {
const themeRoot = path.join(__dirname, '..');
const mainConfigPath = path.join(themeRoot, '_config.yml');

logger.info('Checking if the configuration file exists');
if (!fs.existsSync(mainConfigPath)) {
logger.warn(`${mainConfigPath} is not found. Please create one from the template _config.yml.example.`)
}
}

logger.info('Checking language names against RFC5646 specs');
const invalidLanguages = getUsedLanguages().filter(language => !isLanguageValid(language));
if (invalidLanguages.length > 0) {
logger.warn(`Language ${invalidLanguages} indicated by some posts is not a valid RFC5646 language.`)
}
const invalidDisplayLanguages = getDisplayLanguages().filter(language => !isLanguageValid(language));
if (invalidDisplayLanguages.length > 0) {
logger.warn(`Language ${invalidDisplayLanguages} set in the configuration file is not a valid RFC5646 language.`)
}
Loading

0 comments on commit 3e9a9a2

Please sign in to comment.