koalazak / i18n-express Goto Github PK
View Code? Open in Web Editor NEWA simple i18n middleware for Express.js
A simple i18n middleware for Express.js
Cool work man, but ... Is me or there ir no way to use language variables on method like i18n documentacion point like this:
app.get('/de', function(req, res){
var greeting = res.__('Hello');
});
Cannot find a way to use in model or controllers, I end it up using both (i18n and i18n-express).
Please let me know if is just me that cannot find it.
I configure my express application as laid out in the README using i18n-express 1.1.3 and the application starts without a problem.
However, when I test my application using Jest, I receive the following error:
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
โ FSEVENTWRAP
23 |
24 | app.use(
> 25 | i18n({
| ^
Looks like the constructor activates a file system watch on the translationsPath directory, which I think doesn't get closed in the scope of my tests. It looks like there's an option to allow closing the watcher with an abort signal https://nodejs.org/docs/latest-v18.x/api/fs.html#fswatchfilename-options-listener, but the option isn't set in the call to fs.watch.
In the meantime, since I know roughly where the issue originates, I can --forceExit
my jest tests in the meantime.
Hi there. I am understood, when I choose a language (for example - mywebsite.com/?clang=en) and it save language in cookie for all url at my website. Can u help me, pls. How can I add /?clang=(en or ru) to the end of my routes(for example - mywebsite.com/about/?clang=(en or ru) depending on what button with language we are click before? Thank's in advance.
If we put lots of words into json files, how does performance affect from this?
I have the following error when I try to use i18n-express in my application :
TypeError: Cannot use 'in' operator to search for 'ulang' in undefined
at getLangFromCookie (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\i18n-express\index.js:20:26)
at i18n (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\i18n-express\index.js:85:21)
at Layer.handle [as handle_request] (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\index.js:312:13)
at C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\index.js:271:10)
at expressInit (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\middleware\init.js:33:5)
at Layer.handle [as handle_request] (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\router\index.js:312:13)
TypeError: C:\Users\JXPK7564\Documents\GitHub\olaf-portal\views\error.ejs:1
>> 1| <% include partials/header %>
2|
3|
4|
esc is not a function
at rethrow (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\ejs\lib\ejs.js:285:18)
at eval (eval at compile (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\ejs\lib\ejs.js:524:12), <anonymous>:30:9)
at eval (eval at compile (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\ejs\lib\ejs.js:524:12), <anonymous>:32:10)
at returnedFn (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\ejs\lib\ejs.js:555:17)
at tryHandleCache (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\ejs\lib\ejs.js:203:34)
at View.exports.renderFile [as engine] (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\ejs\lib\ejs.js:412:10)
at View.render (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\view.js:126:8)
at tryRender (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (C:\Users\JXPK7564\Documents\GitHub\olaf-portal\node_modules\express\lib\response.js:960:7)
my code looks like this :
var i18n = require("i18n-express");
...
app.use(i18n({
translationsPath: __dirname + '/i18n',
siteLangs: ['en']
}));
best regards,
This plugin work nicely for me.
But I'd like to have support for a forme of pluralization rules
How can I standarize the error messages that shows my views if I send them from the routers usin flash?? If try to use the translation variable it does not work
how to set the i18n config options that allow access lang parameters inside a router.
what is a better way to get the lang, from cookie, variable, etc and how to do this?
Hi, can i use instead of site.com/?clang=es . -> site.com/es ??? without GET param?
Thanks.
I put this in my index.js but it doesnt' reach all the routes that I have in the project
var i18n=require("i18n-express");
var geolang=require("geolang-express");
app.use(geolang({
siteLangs: ["en","it"],
cookieLangName: 'ulang'
}));
app.use(i18n({
translationsPath: path.join(__dirname, 'i18n'),
siteLangs: ["en","it"],
cookieLangName: 'ulang',
textsVarName: 'translation',
paramLangName: 'clang'
}));
If I try to use <%=translation.ADD_APP%>
in my html it gives me this error:
ReferenceError: /Users/web/Desktop/node/pusha/views/apps/add_new_app.ejs:13
11| <div class="container">
12| <form action="/apps" method="post">
>> 13| <h3><%=translation.ADD_APP%></h3>
14| <p><strong>Name:</strong> <input type="text" name = "name"></p>
15| <br>
16| <!-- --------------------------------- -->
translation is not defined
at eval (eval at compile (/Users/web/Desktop/node/pusha/node_modules/ejs/lib/ejs.js:524:12), <anonymous>:43:25)
at returnedFn (/Users/web/Desktop/node/pusha/node_modules/ejs/lib/ejs.js:555:17)
at tryHandleCache (/Users/web/Desktop/node/pusha/node_modules/ejs/lib/ejs.js:203:34)
at View.exports.renderFile [as engine] (/Users/web/Desktop/node/pusha/node_modules/ejs/lib/ejs.js:412:10)
at View.render (/Users/web/Desktop/node/pusha/node_modules/express/lib/view.js:128:8)
at tryRender (/Users/web/Desktop/node/pusha/node_modules/express/lib/application.js:640:10)
at EventEmitter.render (/Users/web/Desktop/node/pusha/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/Users/web/Desktop/node/pusha/node_modules/express/lib/response.js:966:7)
at /Users/web/Desktop/node/pusha/routes/apps.js:124:9
at Layer.handle [as handle_request] (/Users/web/Desktop/node/pusha/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/web/Desktop/node/pusha/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/web/Desktop/node/pusha/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/web/Desktop/node/pusha/node_modules/express/lib/router/layer.js:95:5)
at /Users/web/Desktop/node/pusha/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/web/Desktop/node/pusha/node_modules/express/lib/router/index.js:335:12)
at next (/Users/web/Desktop/node/pusha/node_modules/express/lib/router/index.js:275:10)
It's very interesting because my file is there. And if I change it's name it will give me an error saying the same thing with the new name of the file.
Error: Cannot find module './public/i18n/en.json'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at loadLangJSONFiles (C:\Users\Adi\Desktop\7-nodejs-express-web-applications-m7-exercise-files\codeEnd\library\node_modules\i18n-express\index.js:49:66)
at module.exports (C:\Users\Adi\Desktop\7-nodejs-express-web-applications-m7-exercise-files\codeEnd\library\node_modules\i18n-express\index.js:79:19)
at Object. (C:\Users\Adi\Desktop\7-nodejs-express-web-applications-m7-exercise-files\codeEnd\library\app.js:61:9)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
Hi, Thanks for the i18n-express package, I wanna ask something, I have a code like this :
app.use(i18n({
translationsPath: path.join(__dirname, 'i18n'),
siteLangs: ["id","en"],
textsVarName: 'text',
defaultLang: 'id',
paramLangName : 'lang'
}));
Why defaultLang not working? When i open my web, defaultLang always to en.
Thanks
e.g. if i have 'en' and 'ru' in the page, but 'ru' does not have a full translation, can all missing texts be replaced by 'en' texts
Hello, my i18n is working fine on static content. When I render a page I do have the content changing according to the language.
But, i have some javascript scripts that need to apply some text transformation on certain events. For instance, I have a title that changes everytime you click a button.
The initial title is localized, but when the script acts, I don't have access to the i18n variable.
How do I do this?
so for example if instead of a view I want to output a json response I still can use it?
I have replicated the demo code as shown and confirmed I can access the lang
variable in my hbs templates, but lang's value is always en
.
But I can't get it to use any other language, even if I set defaultLang to something other than 'en'.
Any suggestions? I must be doing something foolish, surely.
Hello. Thank you for library.
I have question about changing current locale.
I trying to set cookie locale like this
app.param(':locale', function(req,res, next, locale) {
res.cookie('ulang', locale)
console.log('------- locale:', locale);
}
app.use('/:locale/order', order);
// Router order
router.get('/', function(req, res, next) {
console.log('=====', req.cookies['ulang']);
});
It works only after second call and logs looks like:
// first call to the /de/order
------- locale: de
===== en
// second call to the /de/order
------- locale: de
===== de
I found in the sources of current project req.session[cookieLangName]
is used. But for me req.session['ulang'] = locale
rase error - req.session
is undefined.
So the question is: How to change current locale?
Hi there, can u help me pls, I have a directories - templates and routes, i18n express is working only with first page(fronpage.ejs in templates directory). If I on the page about.ejs and click on language link, its redirect me on frontpage.ejs. What I am doing wrong, maybe problem with order of middleware ?
If I manual entries url, like this /about/?clang=en it's works.
In my template frontpage.ejs link to about.ejs is href="/about"
my app.js file:
var express = require('express');
var http = require('http');
var path = require('path');
var i18n = require("i18n-express");
var cookieParser = require('cookie-parser');
var config = require('config');
var log = require('libs/log') (module);
var app = express();
app.set('port', config.get('port'));
app.engine('ejs', require('ejs-locals'));
app.set('views', path.join(__dirname, '/templates'));
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(i18n({
translationsPath: path.join(__dirname, './i18n'),
siteLangs: ["en","es"],
textsVarName: 'translation'
}));
app.use(express.favicon());
if(app.get('env') == 'development') {
app.use(express.logger('dev'));
}else {
app.use(express.logger('default'))
}
app.use(express.json());
app.use(express.urlencoded());
app.use(express.session({ secret: 'your secret here' }));
app.use(app.router);
require('routes')(app);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function (err, req, res, next) {
if(app.get('env') == 'development'){
var errorHandler = express.errorHandler();
errorHandler(err, req, res, next);
}else {
res.send(500);
}
});
http.createServer(app).listen(app.get('port'), function(){
log.info('Express server listening on port ' + config.get('port'));
});
Thanks in advance...
Good day I'm not much fumbling in the code help
what is this mistake and how to fix it ?
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (/Users/aleksandrastahov/Desktop/fdg/utils/i18n.js:1:76)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (/Users/aleksandrastahov/Desktop/fdg/modules/checkAllGuilds.js:7:14)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (/Users/aleksandrastahov/Desktop/fdg/events/ready.js:2:24)
at Module._compile (internal/modules/cjs/loader.js:689:30)
Every time i click on a menu or link the /?clang that i choose before is change to bowser default language, how can i make it persistent?
We are interested in splitting up the files for any given language based on purpose. For example:
i18n/en
countries.json
errors.json
general.json
i18n/fr
countries.json
errors.json
general.json
Is this achievable with the current code base? If not, would this be a functionality that could be of interest if coded?
Congratulations on this module, it is helping me a lot, but I would like to know how to get translation inside a controller.js?
There will be an error in the method getLangFromCokie
you should replace
if (cookieName in req.cookies) {
with:
if (req.cookies && cookieName in req.cookies) {
Hi,
I would like to update the lang if you user browse /fr
or /en
. I added a middleware to dynamically update the defaultLang value but it's doesn't work.
var config = {
i18n: {
translationsPath: path.join(__dirname, 'locales'),
siteLangs: ["en","fr"],
textsVarName: 't',
defaultLang: 'fr',
}
}
var url_has_lang_regex = /^\/([a-z]{2})(\/|$)/;
app.use(function(req, res, next) {
var lang_matches = req.url.match( url_has_lang_regex );
if((config.i18n.siteLangs).indexOf(lang_matches[1]) >= 0) {
console.log(lang_matches[1])
config.i18n.defaultLang = lang_matches[1];
}
console.log(config)
next();
});
app.use(i18n(config.i18n));
Thanks.
Hi, I can't show my translations on my app. My template engine is Pug.
in app.js:
var cookieParser = require('cookie-parser');
var i18n=require("i18n-express"); // <-- require the module
var session = require('express-session')
app.use(i18n({
translationsPath: __dirname + '/i18n', //
siteLangs: ["en","tr"],
textsVarName: 'translations',
defaultLang: "tr"
}));
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
module.exports = app;
tr.js:
{
"WELCOME_MSG": "Hi! Welcome!",
"CONTACT_TEXT": "More bla"
}
index.pug:
h1 #{translations.WELCOME_MSG}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.