Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About I18n #39

Open
genlinjiao opened this issue Apr 25, 2019 · 1 comment
Open

About I18n #39

genlinjiao opened this issue Apr 25, 2019 · 1 comment

Comments

@genlinjiao
Copy link

How to use i18n in a wq project? The i18n approach is not working properly in wq project, especially with mustache template engine.

@sheppard
Copy link
Member

sheppard commented May 9, 2019

Yes, this is not very well supported currently, and would be a good candidate for inclusion 2.0. In the meantime, you can use Mustache's function rendering with literal template values. Perhaps something like this:

<!-- template.html -->
<html>
  <body>
     <h1>{{#t}}Hello{{/t}}</h1>
  </body>
</html>

Then, define a context plugin like this:

// myapp/i18n.js
define({
    'name': 'i18n',
    'init': function(config) {
        this.language = navigator.language.split('-')[0];
        this.translations = config.translations[this.language];
    },
    'context': function(context, routeInfo) {
        var self = this;
        return {
            't': function() {
                return function(text, render) {
                    return self.translations[text] || text;
                };
            }
        }
    }
});

// myapp/config.js
define(['data/config', ...],
function(config, ...) {
    ...

    // TODO: use full language codes
    config.i18n = {
        'translations': {
            'en': {
                'Hello': 'Hello',
            },
            'zh': {
                'Hello': '你好'
            }
        }
    };

    return config;
});

// myapp/main.js
define(['wq/app', './i18n', './config', ...],
function(app, i18n, config, ...) {
    app.use(i18n);
    app.init(config) ...
});

This assumes a hardcoded list of a few translations, but you should be able to integrate an existing i18n library using a similar technique.

@sheppard sheppard added this to the 2.0 milestone Jul 11, 2019
@sheppard sheppard added this to To do in wq 2.0 Aug 18, 2021
@sheppard sheppard modified the milestones: 2.0, 3.0 - Data Model Feb 20, 2023
@sheppard sheppard removed this from To do in wq 2.0 Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants