cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to use the i18n resource bundle inside a NodeJS service implementation?

iashishsingh
Participant

Hello,

I was wondering if there is any mechanism to make use of the i18n resource bundle defined in a CAP service, from inside a NodeJS based service implementation? Its usage would be similar to how resource bundles are used in SAPUI5 Javascript controllers and Components.

Thanks,
Ashish

johannesvogel
Advisor
Advisor
0 Kudos

Hi Ashish,

could you please provide a code snippet on how you imagine to access the i18n resource bundle form within a CAP service?

Best regards,

Johannes

gregorw
Active Contributor
0 Kudos

Maybe something like the UI5 ResourceModel?

iashishsingh
Participant
0 Kudos

Hi Johannes,

The use case is similar to what Gregor has mentioned i.e. how we use UI5 ResourceModel. Apart from the current usage inside the model definitions, another usage could be in the custom handlers of service definitions where the output may contain locale based text for e.g. error messages. But I am still wondering and looking for more compelling use cases. Just wanted to know if this was currently supported.

Thanks.

johannesvogel
Advisor
Advisor

Hi Ashish,

currently there is no convenient API to do this. We might add something in the future.

Best regards,

Johannes

iashishsingh
Participant
0 Kudos

That sounds great. Thanks a lot, Johannes!

Accepted Solutions (1)

Accepted Solutions (1)

sergei-u-niq
Active Contributor

well, it kinda works in CAP (v 3.33.1) in NodeJS

const i18n = require('@sap/cds/lib/models/i18n')
var sBlubb = cds.localize({_i18nfolders:[__dirname + "/../_i18n"]}, "de", "\"{i18n>blubb}\"") 

Note that the string MUST contain the double quotes around the binding {...} and the result also has the double quotes inside of the string

iashishsingh
Participant
0 Kudos

Thanks Sergei! This looks like an interesting find and should work until a convenient api is officially provided.

gregorw
Active Contributor
0 Kudos

Maybe the CAP Community should think about using the @sap/textbundle package.

CC: christian.georgi vansyckel daniel.hutzel

mvoros
Active Contributor
0 Kudos

I just want to add that I am using @sap/textbundle in my app and it works nicely.

const i18Service = require('@sap/textbundle');
const bundle = new i18Service.TextBundle({path: '../_i18n/i18n'});

and then you can simply call getText on bundle.

sergei-u-niq
Active Contributor
0 Kudos

nice. I did a search but only ever found other localization packages based on different other formats for bundle files.

I support the Idea that CAP should be using it internally. And definitely will give it a try myself (currently using a self-made wrapper including parametrizing messages a la formatMessage("hello {0}", ['world']))

Answers (1)

Answers (1)

WouterLemaire
Active Contributor

You could try to use this npm package in your custom hook functions:

https://www.npmjs.com/package/i18n

iashishsingh
Participant
0 Kudos

Thanks for sharing this info, Wouter! The package looks interesting and could offer an alternative solution. I was more interested in knowing about the possibilities of achieving the i18n usage using the existing locale setup offered in CAP.