Open temp-impl opened 7 years ago
I don't know what MessageSource
is, but no dependencies to Spring libraries should be added. So I am guessing answer would be "no".
But in general I don't think it is a good idea to integrate localization quite at this level: you can, however, programmatically construct CsvSchema
using translations.
Thanks for the answer.
Is there good way to do programmatically construct CsvSchema using translations? I tried the following, but... (My goal is to output a CSV file with a localized header from the list of POJO class)
CsvSchema schema = mapper.schemaFor(Pojo.class);
This one, there is no chance to localize header.
CsvSchema schema = CsvSchema.builder().addColumn("localized-firstName").build();
This one, POJO class need property name "localized-firstName".
"localized-firstName" is depending on the language, so I think that it is not a good idea to have "localized-firstName" property in the POJO class.
@temp-impl it is possible to just construct whole CsvSchema
programmatically, it need not come from a POJO. But I guess what you are asking is sort of renaming operation, which does not really exist at this point. It is of course possible to use different schema for reading and writing, but challenge still remains between naming used in POJOs (if used) and output.
I'll have to think about this: I don't really have a good idea how this could or should work. But I do think this is bit more general than just CSV: in theory one might want to rename properties of JSON or XML or Properties file as well.
Further, these would be somewhat dynamic translations, somewhat similar to @JsonFilter
in that different mapping could be used on per-call basis (on serialization). Existing renaming systems do not allow this; not even NamingStrategy
: assumption is there is just one mapping.
Interesting idea. Just not sure how to go about it.
I was looking for a solutions as well. I worked around this problem by writing header (first line) and content of the csv seperatly as follows:
CsvSchema schema = csvMapper.schemaFor(clazz).withHeader();
Builder builder = schema.rebuild();
for (Column column : schema) {
String localizedName= messageSource.getMessage(column.getName(), null, column.getName(), locale);
builder.renameColumn(column.getIndex(), localizedName);
}
CsvSchema renamedSchema = builder.build();
String header = csvMapper.writer(renamedSchema).writeValueAsString(List.of());
// write content
CsvSchema noheaderSchema = csvMapper.schemaFor(clazz).withoutHeader();
String csvData = csvMapper.writer(schemaRenamed).writeValueAsString(data);`
String localizedCSV = header + csvData
Hmmh. I would have thought that it should be possible to simply create ObjectWriter
with desired schema, and that should... work?
Or I need to join column header after writeValueAsString() ?
Below is similar problem: http://stackoverflow.com/questions/35295997/set-localized-column-title-in-csv-file-using-jackson