Skip to content

Commit b9c6865

Browse files
authored
Add documentation about localized transformers (#3125)
1 parent 00e1ff0 commit b9c6865

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

cucumber-java/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,57 @@ public class DataTableStepDefinitions {
329329
}
330330
```
331331

332+
### Localized Transformers
333+
334+
To transform a value localized to the language of the feature file combine the [Before hook](#before--after) and the
335+
scenario's language. For example, localized features about summer solstice may provide the date "21 kesäkuuta 2025"
336+
(Finnish) or "21 juin 2025" (French). Parsing these dates requires a `DateTimeFormatter` with a locale.
337+
338+
```java
339+
package com.example.app;
340+
341+
import io.cucumber.java.Before;
342+
import io.cucumber.java.ParameterType;
343+
import io.cucumber.java.Scenario;
344+
345+
import java.time.LocalDate;
346+
import java.time.format.DateTimeFormatter;
347+
import java.util.Locale;
348+
349+
public class TransformerDefinitions {
350+
351+
private DateTimeFormatter formatter;
352+
353+
@Before
354+
public void updateFormatter(final Scenario scenario) {
355+
String language = scenario.getLanguage();
356+
Locale locale = new Locale.Builder().setLanguage(language).build();
357+
formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy").withLocale(locale);
358+
}
359+
360+
@ParameterType(value = "\\d{1,2} \\w+ \\d{4}")
361+
public LocalDate localizedDate(String value) {
362+
return LocalDate.parse(value, formatter);
363+
}
364+
365+
}
366+
```
367+
368+
Similarly, the example `ObjectMapper` of the [aforementioned default transformers](#default-transformers) can be localized
369+
before each scenario.
370+
371+
```java
372+
[...]
373+
@Before
374+
public void updateObjectMapper(final Scenario scenario) {
375+
String language = scenario.getLanguage();
376+
Locale locale = new Locale.Builder().setLanguage(language).build();
377+
objectMapper.setLocale(locale);
378+
}
379+
[...]
380+
```
381+
382+
332383
### Empty Cells
333384

334385
Data tables in Gherkin cannot represent null or an empty string unambiguously. Cucumber will interpret empty cells as

0 commit comments

Comments
 (0)