cancel
Showing results for 
Search instead for 
Did you mean: 

Localized Strings on entities Cloud Application Programming Model

balbino_soaresferreirafil
Active Participant

Reading the blog https://blogs.sap.com/2018/08/24/top-5-time-saving-benefits-of-the-application-programming-model-for..., in first Item. I had a question about localized strings. I had expected a second table with the product descriptions and language as one of the keys, or something in the table to identify the text language.

for exemple:

entity ProductTypes { 
key code : Integer;
name  : localized String(40) @title:'{i18n>Name}';
}

I need to create a CSV file to import ProductTypes in different languages, how can I do that, since I don't have a field to choose the locale ou language?

Regards

Accepted Solutions (1)

Accepted Solutions (1)

qmacro
Developer Advocate
Developer Advocate

Hi

This is a good question; I've done a little digging as I think the docu on the public facing site is a little behind. The 'localized' modifier is indeed valid and the right thing to add, but the feature itself is still in beta. In order to use the feature, make sure you have a .cdsrc.json file in the root of your project (initialising a new project with 'cds init' will give you one of these files anyway) and add the following to it:

"features": {
  "localized": true
},
"cdsc": {
  "betaMode": true
}

When you then compile explicitly, or implicitly (via 'cds deploy'), you will get not only the table definition:

CREATE TABLE ProductTypes (
  code INTEGER,
name NVARCHAR(40),
PRIMARY KEY(code) );

but also a locale-specific "texts" table too:

CREATE TABLE ProductTypes_texts (
  locale NVARCHAR(5),
  code INTEGER,
  name NVARCHAR(40),
  PRIMARY KEY(locale, code)
);

In addition, for SQLite based persistence layers, you get some views that are locale-specific.

dj

former_member369039
Discoverer
0 Kudos

Hi.

do you have some documentation on how to use it in services, what cds statements are correct to have a localized service on master data ?

It's this working for HANA DB as well?

It's working with odata v2 as well odata v4?

Thanks a lot

Gianni

Answers (3)

Answers (3)

With the newest CDS version, the localized feature works without extra cdsrc.json settings.

It also works for HANA DB, and both Data versions.

Assuming that you have exposed the ProductTypes in your service, if you select from it, the name will be the localized name (if your have a translation in ProductTypes_texts).

Under the hood, the service runtime will first check whether there is a definition for `localized.<yourService>.ProductTypes` which is an automatically created view which select from ProductTypes_texts (and ProductTypes if there is no translation).

former_member617882
Discoverer
0 Kudos

thank you very much

former_member617882
Discoverer
0 Kudos

Hi, following your advice I had define a service like that

service TestService {

entity ProductTypes as projection on db.ProductTypes;

}

where db.ProductTypes is the localized entity.

When I test with postman the EntitySet I got this response:

{ "error": { "code": "Operation is not supported.", "message": "Operation is not supported." } }

What's wrong with that?

Any idea?

Gianni

0 Kudos

In your text table, I see 'de_DE' as LOCALE - you should use just 'de'.

For details, see https://cap.cloud.sap/docs/guides/i18n#user-locale.

mhitelli
Explorer
0 Kudos

Hey,

I have a CAP Node.JS service with an entity ValueHelps which is localized. Also, data is available in the database and req.user.locale is set to 'de' if I add sap-language as URL parameter. Nevertheless, always the default language is shown.

http://localhost:4004/import/ValueHelps('42458')?sap-language=de

{
    "@odata.context": "$metadata#ValueHelps/$entity",
    "ID": "42458",
    "name": "Organizational Management",
    "description": null,
}

help-service.cds

using{ de.org as my }from'../db/src/schema';
service HelpService @readonly{
  entity ValueHelps as projection on my.ValueHelps;...}

help-service.js

const cds = require('@sap/cds')module.exports = async function(){
    this.on('READ',(req,res,next)=>{
        console.log(req.user.locale)})...}

schema.cds

entity ValueHelps : managed {keyID:String;
  externalID :String;type:String;name: localized String;description: localized String;status:String;}
entity Users : managed {keyID:String;locale:String;}