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

Handling localization for data? #614

Open
HiImJulien opened this issue Oct 13, 2023 · 4 comments
Open

Handling localization for data? #614

HiImJulien opened this issue Oct 13, 2023 · 4 comments
Labels

Comments

@HiImJulien
Copy link

Hey there!

I have two email templates A and B. A is indented for an international audience whereas B is targeted at a German audience.

Both templates provide a placeholder to set the date of the generated email. The backend represents the timestamp using chrono::DateTime<Utc>. This results in strings such as 2023-10-11T22:11:27.902255300Z whereas strings such as 11.10.2023 or 10/11/2023 are desired. Strings such as 11th March 23 would also be a possibility.

My current workaround is to pass a localized string, which requires the backend to have knowledge of the preferred format.

Is it possible to control the serialization process (with
handlebars or the rhai extensions) to achieve a desired outcome?

Thanks in advance!

@sunng87
Copy link
Owner

sunng87 commented Oct 13, 2023

Hi @HiImJulien , I would recommend your current solution too. Because template engine has no context about user's preference, it's not possible to format the date in helper unless you give user's preference as data to template. It will be identical to do it in backend. (Note that template rendering also happens in backend).

@HiImJulien
Copy link
Author

HiImJulien commented Oct 13, 2023

My bad, it's less about the user preference than the "template preference". Would it be possible to access the underlying value using a helper e.g.:

{{ formatDate varDate 'de' }}

or

{{ formatDate varDate 'en' }}

or even

{{ formatDate varDate '%d.%m.%y' }}?

Or is the data serialized before the helper can access them?

@sunng87
Copy link
Owner

sunng87 commented Oct 13, 2023

I see. Thank you for clarification. But before we send data into template's rendering process, it has to be converted to serde_json::Value types so it's impossible to retain chrono::DateTime and you still need to do it in backend code.

@HiImJulien
Copy link
Author

HiImJulien commented Oct 22, 2023

you still need to do it in backend code.

And I again, I failed to express my intent clearly 😅.
I understand, that the procedure still runs in the "backend"; it doesn't matter to me. However, it's important for me to control form the template how it's being formatted in the end.

I think I have an idea for an workaround:


The basic idea is to use the serde's serialize_with attribute to transform the date to a numeric timestamp or an object (either Value::Number or Value::Object).

Then create a custom helper to_date_str with two parameters timestamp and format. I.e. the following pseudo code:

fn to_date_str(timestamp: i64, fmt: String) -> String {
    let datetime = DateTime::from_timestamp(timestamp);
    return datetime.format(fmt);
}

This would allow me to do something like this

{{ to_date_str email.date "DD.MM.YYYY" }}

What confuses me, however, is when you are taking a look at the example. On line 10, there is this line:

handlebars_helper!(date: |dt: OffsetDateTime| dt.format(&parse("[year]-[month]-[day]").unwrap()).unwrap());

How can the helper access a value of type OffsetDateTime? Or can I use any type as long as it's deserializable?

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

2 participants