Problem with localize and translations in general
-
Hello, I have recently started using jsreport and came across a problem that despite trying to investigate how to work around it, I can't seem to find a way.
This is my structure (I have i18n and translations as I was trying to run some tests):
In my
contentRaw
, I have something like this, to access a specific translation, based on the@key
<c t="inlineStr" s="7"><is><t>{{localize @key "../../translations"}}</t></is></c>
Like this, it works, if I am accessing a key directly (in this case, tmax)
But what I wanted to do was something like
<c t="inlineStr" s="7"><is><t>{{localize historicalWateringChart.@key "../../translations"}}</t></is></c>
, so I could accesshistoricalWateringChart.tmax, historicalWateringChart.tmin
and so on...I also have in my StoockDataFetch/content.js (my script file), on my
beforeRender
function, the following:const i18n = await jsreport.localization.localize({ key:'t', folder: '../../i18n', language: 'en-GB' })
req.data.myData = [{}, { name: i18n.shared.datePeriod, text: formatDate(req.data.startDate) + " - " + formatDate(req.data.endDate) },]
Like this it works, but I was trying to understand if there is another way to get my i18n, as on my
json
file for my i18n translations, it needs to be set up like this:{"t": { "shared": { "datePeriod": "Date period", }}
And I wanted to get rid of the
t
key, but by accessingjsreport.localization.localize()
, thekey
is obligatory.Any sort of help would be highly appreciated, and if I wasn't clear or you need for me to provide more information please do tell.
-
But what I wanted to do was something like <c t="inlineStr" s="7"><is><t>{{localize historicalWateringChart.@key "../../translations"}}</t></is></c>, so I could access historicalWateringChart.tmax, historicalWateringChart.tmin and so on...
Unfortunately, the localization extension supports just the plain structure of the resource json. It doesn't support nested objects. I've submitted the feature request to backlog to add support for the nested objects as well.
https://github.com/jsreport/jsreport/issues/1103Like this it works, but I was trying to understand if there is another way to get my i18n, as on my json file for my i18n translations, it needs to be set up like this
Despite trying to understand what you mean here, I am lost. Would you be able to share a demo in the playground? I would check how to make localization work the best for you.
https://playground.jsreport.net/
-
That is unfortunate about the nested objects. Thank you for submitting a feature request.
About the other part, I created this demo:
https://playground.jsreport.net/w/anon/7ko~pFmZ
What I was trying to explain is that in my Sample > content.js, I have the following:
const i18n = await jsreport.localization.localize({ key:'t', folder: '../../i18n', language: 'en-GB' })
As in my i18n -> en-GB.json, I have this:
{"t": { "shared": { "datePeriod": { "title": "Date period" }, "fieldName": "Field name" } }}
I was hoping that there was a way to dynamically localize my translations, without having to declare a
key
, so I could do something more straightforwards such as:{ "shared": { "datePeriod": { "title": "Date period" }, "fieldName": "Field name" } }
And again, thank you for your time
-
Ah ok. You want to get the whole content of the JSON.
In this case, just get the desired entity and don't use the
localize
.
https://playground.jsreport.net/w/anon/3j4pd5Kcconst { entity: localizedEntity } = await jsreport.folders.resolveEntityFromPath( `../../i18n/${req.options.localization?.language || 'en-GB'}.json`, 'assets', req) const i18n = JSON.parse(localizedEntity.content.toString())
-
That was exactly it, thank you!