r/C_Programming 15d ago

Question Simple dialogue system in C

Hello everyone. I'm working on a personal project where I'm porting a little game to C for learning purposes. It has very simple dialogue: it boils down to a few choices and a response on selecting a choice. There's the occasional conditional choice (and response), but that's all it is. My current approach has been as simple as it can be (see code below).

However, I'd like to ask for help with supporting translations, i.e. allow for a way to let outside users add a translation in any way. (You can imagine someone translating in a csv, or json, or C directly). I want to add a way to do so, but I can't figure out a good way to do so. (English will be the base language, so for all intents and purposes it being stored inside dialogue.c is fine for now).

Thanks in advance! :)

static const DialogueChoices CHOICES = { 
  .count = 1,
  .options = {
    {  
       .choice = "Choice goes here", 
       .response = "Response goes here"
    }
}

// switch over character id and return the appropriate DialogueChoices struct
DialogueChoices get_dialogue_choices(CharacterId id);
12 Upvotes

4 comments sorted by

12

u/PseudoFrequency 15d ago

Gettext is the open-source standard for this. Old Windows stuff uses resource files compiled into DLLs, but I wouldn't recommend it. Gettext can handle most translation cases you'd want. The input files are .po format and many professional translators know how to work with them.

7

u/aquamarine121 15d ago

Check out gettext. All translations are stored in external files that are loaded at runtime. The program source is modified slightly to adopt the internalization.

1

u/Still_Explorer 14d ago

For translations the best choice is to give a file with a list of key-value (uid-text) things. Otherwise giving a structured file (xml/json) there's the risk of having the data structure messed up and then say welcome to a lot of painful debugging.

<story>
  <choice>
    <text tid="a031b5">ARE YOU SURE BOUT THAT?!</text>
    <response tid="0584a7">No...</response>
    <response tid="917e50">Perhaps...</response>
  </choice>
</story>

File with translation IDs:
<translation lang="de">
  <entry tid="a031b5">BIST DU DIR DA SICHER?!</entry>
  <entry tid="0584a7">Nein...</entry>
  <entry tid="917e50">Vielleicht...</entry>
</translation>

1

u/Woshiwuja 15d ago

Map the struct to a json with the same fields