Skip to content

Commit bf50cca

Browse files
committed
[v1.7.2] experimenting multilingual ppost
1 parent d187e9f commit bf50cca

File tree

6 files changed

+550
-256
lines changed

6 files changed

+550
-256
lines changed

_includes/footer.html

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,26 @@
202202
{% endif %}
203203

204204

205-
206205
<!-- Side Catalog -->
207206
{% if page.catalog %}
208207
<script type="text/javascript">
209208
function generateCatalog (selector) {
210-
var P = $('div.post-container'),a,n,t,l,i,c;
209+
210+
// interop with multilangual
211+
if ({{ page.multilingual }}) {
212+
_containerSelector = 'div.post-container.active'
213+
} else {
214+
_containerSelector = 'div.post-container'
215+
}
216+
217+
// init
218+
var P = $(_containerSelector),a,n,t,l,i,c;
211219
a = P.find('h1,h2,h3,h4,h5,h6');
220+
221+
// clean
222+
$(selector).html('')
223+
224+
// appending
212225
a.each(function () {
213226
n = $(this).prop('tagName').toLowerCase();
214227
i = "#"+$(this).prop('id');
@@ -249,3 +262,56 @@
249262
});
250263
</script>
251264
{% endif %}
265+
266+
267+
<!-- Multi-Lingual -->
268+
{% if page.multilingual %}
269+
<!-- Handle Language Change -->
270+
<script type="text/javascript">
271+
// get nodes
272+
var $zh = document.querySelector(".zh");
273+
var $en = document.querySelector(".en");
274+
var $select = document.querySelector("select");
275+
276+
// bind hashchange event
277+
// Changes at v1.7.2: change the language flag from hash-based to in-memory based
278+
// due to its confliction with catalog anchors.
279+
// window.addEventListener('hashchange', _render);
280+
281+
// handle render
282+
function _render(_hash){
283+
var _hash = _hash || window.location.hash;
284+
// en
285+
if(_hash == "#en"){
286+
$select.selectedIndex = 1;
287+
$en.style.display = "block";
288+
$en.classList.add("active");
289+
$zh.style.display = "none";
290+
$zh.classList.remove("active");
291+
// zh by default
292+
}else{
293+
// not trigger onChange, otherwise cause a loop call.
294+
$select.selectedIndex = 0;
295+
$zh.style.display = "block";
296+
$zh.classList.add("active");
297+
$en.style.display = "none";
298+
$en.classList.remove("active");
299+
}
300+
// interop with catalog
301+
if ("{{ page.catalog }}") generateCatalog(".catalog-body");
302+
}
303+
304+
// handle select change
305+
function onLanChange(index){
306+
if(index == 0){
307+
_hash = "#zh"
308+
}else{
309+
_hash = "#en"
310+
}
311+
_render(_hash)
312+
}
313+
314+
// init
315+
_render();
316+
</script>
317+
{% endif %}

0 commit comments

Comments
 (0)