Chapter 8: a language from the environment, built

A program can learn its language from the machine it runs on. EnvGreet reads the APP_LANG environment variable, asks for the Greeting in that language, and keeps the answer only if the language exists - an unset variable, or a language nobody wrote, falls back to English. The IDE's tools build the program, the Greeting type and its locales; you type the wording and the lookup, then run it twice with different environments. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-env-greet.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.env.choice

  defines text for "en"
    Greeting
      welcome()
        -> name as String
        `Hello, ${name}!`

  defines text for "fr"
    Greeting
      welcome()
        -> name as String
        `Bonjour, ${name} !`

  defines text for "de"
    Greeting
      welcome()
        -> name as String
        `Hallo, ${name}!`

  defines program
    EnvGreet()
      stdout <- Stdout()
      me <- "Ada"

      env <- EnvVars()
      requestedLang <- env.get("APP_LANG")

      chosen <- Greeting(requestedLang)
      greeter <- chosen? <- chosen : Greeting("en")
      stdout.println(greeter.welcome(me))

Step 1: The program, hard-coding English

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

EnvGreet is the program's name - no application to bind, no parameters, nothing injected. Type EnvGreet and choose Preview.

The EK9 IDE just before choosing Preview: EnvGreet is the program's name - no application to bind, no parameters, nothing injected

The preview shows EnvGreet about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows EnvGreet about to be written

Select the stub, from ready <- true down to its //AI line, and type EnvGreet's body: a Stdout, the name Ada and the English greeting hard-coded. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type EnvGreet's body

Step 2: welcome - the real message

Select the hard-coded greeting, right-click it and choose Localisation ▸ Extract to Localized Text…. Name the method welcome; type Greeting as the text type - new, so the dialog offers its first locale (en); name greeter as the text variable. The Parameters table shows me - retype it to name, then choose Preview.

The EK9 IDE just before choosing Preview: Select the hard-coded greeting, right-click it and choose Localisation ▸ Extract to Localized Text…

The preview shows Greeting created with its welcome() message and greeter declared above the statement that uses it. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Greeting created with its welcome() message and greeter declared above the statement that uses it

Step 3: Adding French

Choose Code > Localisation > Add Locale…

The EK9 IDE once this is done: Choose Code > Localisation > Add Locale…

Adding French, cloned from en - a stub waits where the real words go. Type fr as the new locale - en is already chosen to clone from, the only locale so far. Choose Preview.

The EK9 IDE just before choosing Preview: Adding French, cloned from en - a stub waits where the real words go

The preview shows fr cloned from en, stubbed for translation. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows fr cloned from en, stubbed for translation

Select the stub, from //AI: translate the message down to its empty "", and type the French line. Saves when typed.

The EK9 IDE once this is done: Select the stub, from //AI

Step 4: Adding German

Choose Code > Localisation > Add Locale…

The EK9 IDE once this is done: Choose Code > Localisation > Add Locale…

Adding German, cloned from en the same way. Type de as the new locale - en is already chosen to clone from, the only locale so far. Choose Preview.

The EK9 IDE just before choosing Preview: Adding German, cloned from en the same way

The preview shows de cloned from en, stubbed for translation. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows de cloned from en, stubbed for translation

Select the stub, from //AI: translate the message down to its empty "", and type the German line. Saves when typed.

The EK9 IDE once this is done: Select the stub, from //AI

Step 5: Read the language from the environment

greeter is still always English. Select the greeter line and type the lookup: EnvVars().get("APP_LANG") is the requested language, chosen asks for it, and greeter keeps chosen only if it is set (chosen?), else Greeting("en").

The EK9 IDE once this is done: greeter is still always English

Step 6: Run it, APP_LANG not set

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

APP_LANG is not set, so chosen is unset and the greeting falls back to English: Hello, Ada! Choose Close.

The EK9 IDE just before choosing Close: APP_LANG is not set, so chosen is unset and the greeting falls back to English

Step 7: Run it, APP_LANG=fr

Choose Run > Configurations…

The EK9 IDE once this is done: Choose Run > Configurations…

Choose + New Configuration… - the project's program is already the one it will belong to.

Name it In French, and into Environment type APP_LANG=fr - the one field with anywhere to put it. Choose Create.

The EK9 IDE just before choosing Create: Name it In French, and into Environment type APP_LANG=fr - the one field with anywhere to put it

In French is already selected. Choose Run.

The EK9 IDE just before choosing Run: In French is already selected

APP_LANG=fr reaches the program through its environment: Bonjour, Ada ! Choose Close.

The EK9 IDE just before choosing Close: APP_LANG=fr reaches the program through its environment

The result

EnvGreet.ek9 now reads exactly as chapter 8 of Getting Started with EK9 shows it: the language is read from the APP_LANG environment variable, so the same program greets in English when it is unset and in French when a run configuration sets it. The tools wrote the program, the Greeting type and two more locales; only the wording and the environment lookup were typed. The same tools - ek9_add_program, ek9_extract_to_localized_text and ek9_edit_localized_text - are what an AI assistant calls to do the same thing.

The EK9 IDE when the lesson has finished

Where this lesson comes from