question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to foreach list languages supports in express-handlebars

See original GitHub issue

am use i18n for support multi languages.

  1. I can’t find documents for list languages pass to views.
{{#each languages}}
<li><button onclick="window.location.href='/{{this.langcode}}';" class="btn btn-link btn-block language-select" type="button" name="VN"><img src="/images/{{this.langcode}}.png" alt="{{this.langname}}" title="{{this.langname}}" /> {{this.langname}}</button></li>
{{/each}}
  1. how to get default data

Current i do static html:

<div id="language" class="btn-group">

                        <button class="btn-link dropdown-toggle" data-toggle="dropdown"> <span> <img src="/images/{{language}}.png" alt="Việt Nam" title="Việt Nam">{{langname}} <i class="fa fa-caret-down"></i></span></button>
                      

                        <ul class="dropdown-menu">
                            <li>
                                <button onclick="window.location.href='/vi';" class="btn btn-link btn-block language-select" type="button" name="language"><img src="/images/vi.png" alt="Việt Nam" title="Việt Nam" /> Việt Nam</button>
                            </li>
                            <li>
                                <button onclick="window.location.href='/en';" class="btn btn-link btn-block language-select" type="button" name="GB"><img src="/images/en.png" alt="English" title="English" /> English</button>
                            </li>
                        </ul>
                    </div>

thankyou any solution

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, May 9, 2017

erm, I think the easiest way is adding those to your translations like

en.json

{
    "en": "english",
    "de": "german"
}

de.json

{
    "en": "Englisch",
    "de": "Deutsch"
}

and using i18n helper to display (example in hbs)

{{#each languages}}
    <a href="/{{this}}">{{@root.__ this}}</a>
{{/each}}
1reaction
mashpiecommented, May 9, 2017

use i18n.getLocales() https://github.com/mashpie/i18n-node#i18ngetlocales and pass the result to locals of your view.

for one view only

app.get('/setlanguage', (req, res) => {
  res.render('languagesetting', {
    languages: i18n.getLocales()
  });
});

or more generally for all view by middleware

app.use((req, res, next) => {
  res.locals.languages: i18n.getLocales();
  next();
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js express, handlebars - get elements of an array
First of all, lets assume that your files:items is a handlebar'd array looking kinda like this. { files:['filenameOne.ext', 'filenameTwo.ext', ...
Read more >
Built-in Helpers - Handlebars
You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being...
Read more >
Express i18n with handlebars and maintainable links - Medium
i18n consists of making every text in your site a variable and every time the page is requested in certain language, every variable...
Read more >
Using Handlebars with Express | Packt Hub
Node.js and Express, support several templating languages that allow you to separate layout and content, and which have the template system ...
Read more >
foundersandcoders/express-handlebars-workshop - GitHub
Contribute to foundersandcoders/express-handlebars-workshop development by ... over the fruits array (similarly to javascript's forEach array method).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found