hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.3k stars 4.83k forks source link

Multi-language support #474

Closed sergiolepore closed 10 years ago

sergiolepore commented 10 years ago

My first language is Spanish. I've been writing in Spanish since I remember, but now I want to share some thoughts in English. I remembered that Wordpress have a plugin called WPML, so I started to write a Hexo Plugin that mimics its functionality. I didn't get far :cry:

Do you have any plans for this feature? I've been looking at some core files and I think that's not so hard to bring multi-language support by rewriting a few lines.

I'll be waiting until v2.5 comes out :smile:

sergiolepore commented 10 years ago

Some ideas:

_config.yml

language: es #main language
secondary_languages:
  - en
  - fr
  - de

scaffolds

title:
date:
tags:
language: en
---

Post content

The public/index.html file will have the posts with language = main language. Posts, pages, archives will be rendered on /{language}/{slug/permalink}. Inside public/{language}/ folder will reside the main index.html with the posts filtered by language.

kassner commented 10 years ago

If someone is interested, I'm already working on it: https://github.com/kassner/hexo

FYI, I never worked with NodeJS before, so probably I'll make a lot of mistakes. When I'll be done, I'll make a pull request.

sergiolepore commented 10 years ago

Awesome! :)

tommy351 commented 10 years ago

The new i18n module is ready for this feature. When you call get function, the module will iterate over the language property and find the available translation or return the original string.

https://github.com/tommy351/hexo/blob/1c2b23725ea983db9949e5a169207c2f0772b0dc/lib/core/i18n.js

kassner commented 10 years ago

@tommy351 this isn't just for translate template strings?

tommy351 commented 10 years ago

This module is used in the templates. But it can be used in other purposes, too.

kassner commented 10 years ago

@tommy351 Do you have any example how to do multilanguage posts with it? I've been working on a multilanguage hexo on this fork, but this way seems MUCH way easier than the one I've been working on.

tommy351 commented 10 years ago
  1. Create an i18n instance.

    // You can use an array
    var locale = new i18n(['zh-TW', 'zh', 'default']);
    // => language: zh-TW, zh, default
    
    // or a string as the argument.
    var locale = new i18n('zh-TW');
    // => language: zh-TW, zh, default
    
    // If the language isn't defined, it'll be `default`.
    var locale = new i18n();
    // => language: default
  2. Add language resources.

    locale.add('default', {hello: 'Hello'});
    locale.add('zh-TW', {hello: '你好'});
    // ...
  3. Use the get function.

    var _tw = locale.get('zh-TW');
    
    _tw('hello');
    // => 你好
    
    _tw('what');
    // => what
sergiolepore commented 10 years ago

But your example is for template translation or "isolated" strings... I want this instead:

Files:

source/_posts/2014-02-07-mi-post-en-espanol.md

Contents:
---
layout: post
title: "Mi post en español"
date: 2014-02-07 18:27
comments: true
categories:
tags:
language: es
---

Este es mi post escrito en español.
source/_posts/2014-02-07-my-post-in-english.md

Contents:
---
layout: post
title: "My post in english"
date: 2014-02-07 18:27
comments: true
categories:
tags:
language: en
---

This is my post written in english.
tommy351 commented 10 years ago

@sergiolepore I'm working on the similar problem for my boss. I'll post the solution when I'm done.

sergiolepore commented 10 years ago

Nice! Thank you soooo much! :smile:

tommy351 commented 10 years ago

Here's my solution:

  1. Disable home, post generators.
  2. Move your posts to _posts_:lang folders.
  3. Use these scripts

There're still many things to do. Categories, tag and archive pages are not supported so far.

sergiolepore commented 10 years ago

I'll take a look as soon as I can. Thank you! :)

IsaacRemuant commented 9 years ago

@tommy351: Is there any work being done about this or is it this still the way you suggest doing multiple manual translations of the same post?

ShadowDanceRunner commented 9 years ago

Hi, I made an alternative to multi language support just doing some changes in the theme layout files, like this: http://www.shadowdr.com/. It's a bit annoying manage but if this is what you looking for, I would be glad to help.

IsaacRemuant commented 9 years ago

@ShadowDanceRunner: That looks great! What would I need to do in order to replicate something like that?

When I was considering alternatives I thought about doing many posts with separators /LANG/YYYY/MM/DD (e.g.: /en/2014/03/23) but I realized that It would mean duplicate entries in lists of posts like "recent".

I see that you dealt with it nicely but do not show list of posts. Is it due to that problem or is it just a personal preference?

ShadowDanceRunner commented 9 years ago

@IsaacRemuant : In fact I turned off the list, after adjusting I had not checked if it is doubling. As it is now, I know it is doubling the statistics of posts, but I did not care about this.

The official theme: https://github.com/ppoffice/hexo-theme-icarus Modified theme: https://github.com/ShadowDanceRunner/icarus-multilanguage-sample

I removed the duplicate listing on the index creating a condition in /layout/_partial/archive.ejs

Added the flags with the links to change the language in .//layout/_partial/article.ejs

Basically we create 2 posts, one as "mynamepost-en" and other "mynamepost-pt-BR", add in the Front matter of each post the "filename" attribute with the value: "mynamepost".

The trick is to create a link in article.ejs like this:

<a href="<%-url_for(post.path) %> ../<%= post.filename%> - en ">

Results in: blog.com/2015-03-31/mynamepost-pt-BR/../mynamepost-en

If you need more details I'll be glad to help. (sorry my bad english)

IsaacRemuant commented 9 years ago

@ShadowDanceRunner. It worked wonders. Thanks. I had no issue with the translations.

However, I had a problem with both the duplicated posts in the "recents" widget (both appear) and its position (the css somehow positioning somehow got broken in the process?)

see: http://isaacremuant.github.io/ (the sidebar is at the bottom).