Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there some way to use the Spring MessageSource for column header? #18

Open
temp-impl opened this issue Mar 27, 2017 · 5 comments
Open
Labels

Comments

@temp-impl
Copy link

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

@cowtowncoder
Copy link
Member

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.

@temp-impl
Copy link
Author

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.

@cowtowncoder
Copy link
Member

@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.

@tschlegel
Copy link

tschlegel commented Jun 18, 2021

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

@cowtowncoder
Copy link
Member

Hmmh. I would have thought that it should be possible to simply create ObjectWriter with desired schema, and that should... work?

@cowtowncoder cowtowncoder changed the title (csv) Is there some way to use the Spring MessageSource for column header? Is there some way to use the Spring MessageSource for column header? Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants