Skip to main content

Localization

Write Gherkin specs in the language your team uses to discuss the feature. Set the keyword language once on a marker class so the specs it discovers can use that language without a # language: header in every file.

Source: examples/going-further/example-10 on GitHub.

What this demonstrates​

  • keywordLanguages can be shared across marker classes through a base class
  • A # language: header takes precedence for an individual file
  • Generated JUnit display names keep the spec's keywords, and step method names come from the step text by default
  • keywordSynonyms adds or replaces keyword spellings for the specs a marker discovers

Set the language on the marker class​

Set keywordLanguages to German on Kasse.java. A bare @Gherkin2JUnit discovers specs next to the marker class by convention.

Kasse.java
package dev.specbinder.examples.goingfurther.localization;

import dev.specbinder.annotations.Gherkin2JUnit;
import dev.specbinder.annotations.Gherkin2JUnitOptions;
import dev.specbinder.annotations.KeywordLanguage;

@Gherkin2JUnit
@Gherkin2JUnitOptions(keywordLanguages = {KeywordLanguage.GERMAN})
public abstract class Kasse {
}

Place Kasse.specb next to the marker. The file starts with Funktionalität: and uses German keywords throughout.

Kasse.specb
Funktionalität: Kasse

Regel: Kartenzahlung

Szenario: Mit Karte bezahlen
Gegeben sei ein Warenkorb mit "2" Artikeln
Wenn ich mit Karte bezahle
Dann hat die Bestellung den Status "bezahlt"
Und der Warenkorb ist leer

To share the language setting across markers, put the option on a base class they extend. A marker that sets its own list replaces the inherited list. See Config Inheritance.

If a marker covers specs written in different languages, list those languages in keywordLanguages. A file without a header can also mix keywords from the list. See the keywordLanguages reference.

Generated output​

The German Regel becomes a JUnit @Nested class. SpecBinder keeps the German keywords in the display names and removes the full Gegeben sei keyword when deriving the first step method.

KasseScenarios.java (excerpt)
@DisplayName("Funktionalität: Kasse")
public abstract class KasseScenarios extends Kasse {
public abstract void einWarenkorbMit$p1Artikeln(Integer p1);

public abstract void ichMitKarteBezahle();

public abstract void hatDieBestellungDenStatus$p1(String p1);

public abstract void derWarenkorbIstLeer();

@Nested
@DisplayName("Regel: Kartenzahlung")
public class Rule_1 {
@Test
@DisplayName("Szenario: Mit Karte bezahlen")
public void rule_1_scenario_1() {
einWarenkorbMit$p1Artikeln(2);
ichMitKarteBezahle();
hatDieBestellungDenStatus$p1("bezahlt");
derWarenkorbIstLeer();
}
}
}

The language applies to keywords. Quoted values follow the usual type inference rules. Here "2" becomes an Integer and "bezahlt" becomes a String. Use "3.5" for a decimal, since "3,5" is read as text, and "true" or "false" for booleans. See Step Parameters.

Set the language for one file​

Caisse.specb sits next to Kasse.specb and shares its marker. Its # language: fr header takes precedence over keywordLanguages for that file.

Caisse.specb
# language: fr
Fonctionnalité: Caisse

Règle: Paiement par carte

Scénario: Payer par carte
Soit un panier avec "2" articles
Quand je paie par carte
Alors la commande a le statut "payée"
Et le panier est vide

SpecBinder uses English when neither the header nor keywordLanguages selects a language.

Add your team's own keywords​

KasseMitSynonymen extends Kasse to inherit German. It lives in a separate package with its own spec and declares one @KeywordSynonym entry per keyword it customizes.

KasseMitSynonymen.java (excerpt)
@Gherkin2JUnit
@Gherkin2JUnitOptions(keywordSynonyms = {
@KeywordSynonym(keyword = GherkinKeyword.RULE, synonyms = "Geschäftsregel", replace = true),
@KeywordSynonym(keyword = GherkinKeyword.GIVEN, synonyms = "Vorausgesetzt")
})
public abstract class KasseMitSynonymen extends Kasse {
}
  • With replace = true, Geschäftsregel becomes the only rule keyword for this marker.
  • By default, a synonym adds to the selected language's keywords. Vorausgesetzt is another way to write a Given step alongside Gegeben sei and Angenommen.

Use the colon after Geschäftsregel in the spec. The annotation takes the spelling "Geschäftsregel" on its own.

KasseMitSynonymen.specb
Funktionalität: Kasse

Geschäftsregel: Kartenzahlung

Szenario: Mit Karte bezahlen
Vorausgesetzt ein Warenkorb mit "2" Artikeln
Wenn ich mit Karte bezahle
Dann hat die Bestellung den Status "bezahlt"
Und der Warenkorb ist leer

JUnit displays the rule as Geschäftsregel: Kartenzahlung. With the default naming options, the first step generates einWarenkorbMit$p1Artikeln(Integer p1) from the text after Vorausgesetzt.

Synonyms apply after SpecBinder selects the language, including for specs with a # language: header. You can put shared synonyms on a base class. An entry on the marker replaces the inherited entry for that keyword only. See the keywordSynonyms reference for the syntax and validation rules.

Use Narrative: as a feature heading​

CheckoutNarrative adds Narrative alongside the English feature keywords Feature, Ability, and Business Need.

CheckoutNarrative.java (excerpt)
@Gherkin2JUnit
@Gherkin2JUnitOptions(keywordSynonyms = {
@KeywordSynonym(keyword = GherkinKeyword.FEATURE, synonyms = "Narrative")
})
public abstract class CheckoutNarrative {
}

The heading and the As a / I want to / So that format are inspired by JBehave's story grammar.

CheckoutNarrative.specb
Narrative: Pay by card at checkout
As a shopper
I want to pay for the items in my cart by card
So that I can complete my purchase without cash

Rule: A successful card payment completes the order and empties the cart

Scenario: Pay by card for two items
Given a cart with "2" items
When I pay by card
Then the order status is "paid"
And the cart is empty

The three story lines form the feature description. The generated class uses Narrative: Pay by card at checkout as its JUnit display name, and the scenario under the rule becomes a runnable test.

Run it​

Run the example from the SpecBinder repository root with JDK 21.

./mvnw -pl examples/going-further/example-10 -am test

All four checkout scenarios should pass. Open target/generated-test-sources/test-annotations/ in the example module to see the generated Java and its display names.

Tooling support​

  • The IntelliJ plugin reads a spec with the same keywords as the generator: its # language: header and the marker's keywordLanguages and keywordSynonyms. Highlighting, keyword completion, the Structure and Project views, running, navigation, debugging and Find Usages all work as they do for an English spec, and edits to these settings take effect in open specs straight away.
  • The execution reporter uses the same keyword languages and synonyms as the generator and preserves the original step text in reports.

All languages​

Use the Gherkin code in a # language: header and the KeywordLanguage constant in keywordLanguages.

Supported languages and codes
ConstantGherkin codeLanguage
AFRIKAANSafAfrikaans
ARMENIANamArmenian (հայերեն)
AMHARICamhAmharic (አማርኛ)
ARAGONESEanAragonese (Aragonés)
ARABICarArabic (العربية)
ASTURIANastAsturian (asturianu)
AZERBAIJANIazAzerbaijani (Azərbaycanca)
BELARUSIANbeBelarusian (Беларуская)
BULGARIANbgBulgarian (български)
MALAYbmMalay (Bahasa Melayu)
BOSNIANbsBosnian (Bosanski)
CATALANcaCatalan (català)
CZECHcsCzech (Česky)
WELSHcy-GBWelsh (Cymraeg)
DANISHdaDanish (dansk)
GERMANdeGerman (Deutsch)
GREEKelGreek (Ελληνικά)
EMOJIemEmoji (😀)
ENGLISHenEnglish
ENGLISH_SCOUSEen-ScouseScouse
ENGLISH_AUSTRALIANen-auAustralian
ENGLISH_LOLCATen-lolLOLCAT
ENGLISH_OLDen-oldOld English (Englisc)
ENGLISH_PIRATEen-piratePirate
ENGLISH_TEXASen-txTexas
ESPERANTOeoEsperanto
SPANISHesSpanish (español)
ESTONIANetEstonian (eesti keel)
PERSIANfaPersian (فارسی)
FINNISHfiFinnish (suomi)
FRENCHfrFrench (français)
IRISHgaIrish (Gaeilge)
GUJARATIgjGujarati (ગુજરાતી)
GALICIANglGalician (galego)
HEBREWheHebrew (עברית)
HINDIhiHindi (हिंदी)
CROATIANhrCroatian (hrvatski)
CREOLEhtCreole (kreyòl)
HUNGARIANhuHungarian (magyar)
INDONESIANidIndonesian (Bahasa Indonesia)
ICELANDICisIcelandic (Íslenska)
ITALIANitItalian (italiano)
JAPANESEjaJapanese (日本語)
JAVANESEjvJavanese (Basa Jawa)
GEORGIANkaGeorgian (ქართული)
KANNADAknKannada (ಕನ್ನಡ)
KOREANkoKorean (한국어)
LITHUANIANltLithuanian (lietuvių kalba)
LUXEMBURGISHluLuxemburgish (Lëtzebuergesch)
LATVIANlvLatvian (latviešu)
MACEDONIAN_CYRILLICmk-CyrlMacedonian (Македонски)
MACEDONIAN_LATINmk-LatnMacedonian (Latin) (Makedonski (Latinica))
MALAYALAMmlMalayalam (മലയാളം)
MONGOLIANmnMongolian (монгол)
MARATHImrMarathi (मराठी)
NEPALIneNepali (नेपाली)
DUTCHnlDutch (Nederlands)
NORWEGIANnoNorwegian (norsk)
PANJABIpaPanjabi (ਪੰਜਾਬੀ)
POLISHplPolish (polski)
PORTUGUESEptPortuguese (português)
ROMANIANroRomanian (română)
RUSSIANruRussian (русский)
SLOVAKskSlovak (Slovensky)
SLOVENIANslSlovenian (Slovenski)
SERBIAN_CYRILLICsr-CyrlSerbian (Српски)
SERBIAN_LATINsr-LatnSerbian (Latin) (Srpski (Latinica))
SWEDISHsvSwedish (Svenska)
TAMILtaTamil (தமிழ்)
TELUGUteTelugu (తెలుగు)
THAIthThai (ไทย)
KLINGONtlhKlingon (tlhIngan)
TURKISHtrTurkish (Türkçe)
TATARttTatar (Татарча)
UKRAINIANukUkrainian (Українська)
URDUurUrdu (اردو)
UZBEKuzUzbek (Узбекча)
VIETNAMESEviVietnamese (Tiếng Việt)
CHINESE_SIMPLIFIEDzh-CNChinese simplified (简体中文)
CHINESE_TRADITIONALzh-TWChinese traditional (繁體中文)