koa-modules / i18n Goto Github PK
View Code? Open in Web Editor NEWLightweight simple translation middleware for koa, based on i18n-2
License: MIT License
Lightweight simple translation middleware for koa, based on i18n-2
License: MIT License
Somewhere like service couldn't get a context, and how to get i18n instance?
"koa-i18n": "2.1.0",
browse:
http://localhost:3000/download/view?filePath=20220406/3_mP8fZGCY.jpg
TypeError: ctx.i18n[(SET_PREFIX + key)] is not a function
at D:\workspace\nodejs\project\project-backend\node_modules\koa-i18n\index.js:107:59
at Array.some (<anonymous>)
at i18nMiddleware (D:\workspace\nodejs\project\project-backend\node_modules\koa-i18n\index.js:104:24)
at dispatch (D:\workspace\nodejs\project\project-backend\node_modules\koa-compose\index.js:42:32)
at session (D:\workspace\nodejs\project\project-backend\node_modules\koa-session\index.js:41:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async bodyParser (D:\workspace\nodejs\project\project-backend\node_modules\koa-bodyparser\index.js:91:5)
at async cors (D:\workspace\nodejs\project\project-backend\node_modules\@koa\cors\index.js:108:16)
at async compressMiddleware (D:\workspace\nodejs\project\project-backend\node_modules\koa-compress\lib\index.js:52:5)
How to define directory option dynamically?
i18n(app, {
directory: path.join(__dirname, 'config', 'locales'),
locales: ['en', 'pt-BR'],
modes: ['header']
})
I'm defined my locale in english as:
{
"BAD_PARAMETERS": 'BAD_PARAMETERS',
"USER_NOT_FOUND": 'USER_NOT_FOUND',
"MISSING_AUTH_HEADER": 'MISSING_AUTH_HEADER',
"USER_EXISTS": 'USER_EXISTS',
"SCAN_REFERENCE_EXISTS": 'Reference identifier exists to this provider',
"TOO_MANY_RETRIES": 'TOO_MANY_RETRIES',
"WRONG_AUTH_TYPE": 'WRONG_AUTH_TYPE',
"WRONG_ISSUER": 'WRONG_ISSUER',
"WRONG_AUDIENCE": 'WRONG_AUDIENCE',
"WRONG_SCOPE": 'WRONG_SCOPE',
"WRONG_IP": "WRONG_IP",
"INVALID_TOKEN": "INVALID_TOKEN",
"UNKNOW_PROVIDER": "Provider doesn't found or invalid",
}
When I try to use ctx.i18n.__(key)
the en
file was overwrited. Why? Thanks in advance.
{
"UNKNOW_PROVIDER": "Provider doesn't found or invalid"
}
As asked in #11, then my question is pretty much the same, I use Nunjucks to have multiple themes on the same instance, that be, a frontend theme and a backend theme.
The language files should be a part of the theme directory and not something you store globally outside the theme itself.
As per now, the theme is defined in a middleware and set in a state, and used by all other elements.
app.use( function( ctx, next ) {
ctx.state.theme = 'default';
await next();
} );
How would I go about accessing ctx.state.theme
when initializing koa-i18n
inside the middleware, pseudo example code
app.use( i18n( app, {
directory: `./themes/${ctx.state.theme}/languages`,
} ) );
How would i go about the following,
I have the following locales defined (en.json, nl.json, fr.json), If a user pops up using Accept-Language nl-BE it will fallback to en due to it being the default language.
How do i make it so that nl-BE (iso 639-4) falls back to the language code nl ? Do i use the modes array for this ?
the exemple is missing a caracter.. !
app.use(i18n(app, {
...
no
app.use(18n(app, {
I think it's possible to decouple this module from koa-locale
by dynamically detecting getLocaleFrom...
and setLocaleFrom...
. This way, people can add more ways of determining locales in their middleware instead of having to rely on this module to keep a close watch on koa-locale
.
What do you think? I can send in a pull request tonight if you think this is a good idea.
Here's a quick sample code:
var getLocaleMethods = function (request) {
var methods = [];
assert(typeof request === 'object', 'request param is not a valid request object');
request.forEach(function (method) {
if (method.indexOf('getLocaleFrom') === 0) {
methods.push(method.replace('getLocaleFrom', ''));
return;
}
});
return methods;
};
There is a strange issue, locale doesn't detect which is passing in header and it always translates according to default locale. Please help.
Thanks in advance.
Hello,
I was reading https://support.google.com/webmasters/answer/189077?hl=en and thought it would be pretty nice if i18n had an option to automatically generate alternate language headers, it would definitely help with SEO. Would such thing be a possibility?
Great library, by the way.
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.