mangstadt/ez-vcard

Adding charset encoding to elements in VCard

Closed this issue · 5 comments

In order to support special characters on iPhone we had to (with VCard v2.1) add Charset=UTF-8 to each elements.
WIthout it tthe special characters did not display properly on iPhone. It works fine on Android with and without the Charset.

N;CHARSET=UTF-8:Solskjær;Åge
ADR;WORK;CHARSET=UTF-8:Address with ÆØÅ Special Characters;1234;Norway

I could not find any way to add this with ez-vcard.

I found out how.
Setting parameter
address.setProperty("Chartset", "UTF-8");

To just add a CHARSET parameter to a property:

vcard.getStructuredName().getParameters().setCharset("UTF-8");

If you are still having trouble, you can try encoding the property value in quoted-printable encoding. My understanding is that this is the most widely supported technique amongst vCard readers.

Quoted-printable encoding allows for non-ASCII characters to be included in a property value, no matter what character set the vCard as a whole uses. By default, ez-vcard uses UTF-8 when encoding a value in quoted-printable encoding (the CHARSET parameter is automatically added when quoted-printable encoding is used).

StructuredName n = new StructuredName();
n.setFamily("Solskjær");
n.setGiven("Åge");
n.getParameters().setEncoding(Encoding.QUOTED_PRINTABLE);

VCard vcard = new VCard(VCardVersion.V2_1);
vcard.setStructuredName(n);
vcard.write(System.out);

//outputs: N;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:Solskj=C3=A6r;=C3=85ge

Thanks. A much better approach than what I just found.

On VCard V3.0 and V4.0 this produces an warning on validation
n.getParameters().setEncoding(Encoding.QUOTED_PRINTABLE);

W04 ENCODING parameter value ("QUOTED_PRINTABLE") is not supported by this vCard version.

It didn't like my Charset parameter either.

W06 Charset parameter is not supported by this vCard version.

Though I could just stick with V2.1

That is correct. According to the specifications, only 2.1 supports quoted-printable encoding and the CHARSET parameter.