Thread: Data structure for storing multiple language versions of my software

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    44

    Question Data structure for storing multiple language versions of my software

    Hello, I have created a simple program for a microcontroller. I have created a menu displayed on an LCD.
    Now I want to add the possibility of choosing a language version, so that a group of strings that are used for language A are exchanged with group of strings from language B.


    How to do that?


    Thank you in advance for any suggestions!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,863
    I am not sure if it is applicable to you, but one of the tools used for this purpose is gettext.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    44
    Quote Originally Posted by laserlight View Post
    I am not sure if it is applicable to you, but one of the tools used for this purpose is gettext.
    Thank you for your help, but it is not applicable for my idea. I am running the app on a microcontroller.

    ------
    I have an idea - is array of string arrays applicable and would solve the problem?

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    I would personally use something like the following:
    Code:
    #define LANGS 2
    
    static const char *const message[][LANGS] = {
    
    #define MSG_HELLO 0
        /* 0 */ { "Hello", "Terve" },
    
    #define MSG_READY 1
        /* 1 */ { "Ready", "Valmis" },
    
    #define MSG_ERROR 2
        /* 2 */ { "Error", "Virhe" },
    
    };
    Note, however, that these strings are stored in ROM (FLASH or EEPROM), not in RAM. Some microcontrollers differentiate the two.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    44

    Exclamation

    Quote Originally Posted by Nominal Animal View Post
    I would personally use something like the following:
    Code:
    #define LANGS 2
    /*didnt want to copy your whole code*/
    Note, however, that these strings are stored in ROM (FLASH or EEPROM), not in RAM. Some microcontrollers differentiate the two.
    Thank you for the suggestion. However, it does not seem to work on my PC.
    My code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        #define LANGUAGES 2
    
    
    static const char *const message[][LANGUAGES] = {
    
    
    #define MSG_INTRO 0
         /*0*/  { "Hello", "Witamy" },
    
    
    #define MSG_MENU1 1
         /*1*/  { "Option1", "Opcja1" },
    
    
    #define MSG_MENU2 2
         /*2*/  { "Option2", "Opcja2" },
    
    
    };
        printf("The text is %s", message[MSG_INTRO][LANGUAGES]);
        return 0;
    }
    When run - printf shows "The text is Option1".
    And when I put MSG_MENU1 into printf then I get "The text is Option2".
    And what is even more funny with MSG_MENU2 I get some random rubbish printed out.
    Why is that happening? I am so confused.

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Pole View Post
    Thank you for the suggestion. However, it does not seem to work on my PC.
    No, because your code is wrong. The message array should be global, and here,
    Quote Originally Posted by Pole View Post
    Code:
        printf("The text is %s", message[MSG_INTRO][LANGUAGES]);
    your latter index (LANGUAGES) is out of bounds. Remember, in C indexes start at 0, and reach up to and including SIZE-1.

    Here is a fixed example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define LANG_EN     0
    #define LANG_PL     1
    #define LANG_COUNT  2
    
    static const char *const message[][LANG_COUNT] = {
    
    #define MSG_INTRO 0
        /* 0 */ { "Hello", "Witamy" },
     
     
    #define MSG_MENU1 1
        /* 1 */ { "Option1", "Opcja1" },
     
    
    #define MSG_MENU2 2
        /* 2 */ { "Option2", "Opcja2" },
    
    #define MSG_COUNT 3
    };
    
    int main(void)
    {
        printf("message[MSG_INTRO][LANG_EN] = \"%s\"\n", message[MSG_INTRO][LANG_EN]);
        printf("message[MSG_MENU2][LANG_PL] = \"%s\"\n", message[MSG_MENU2][LANG_PL]);
        return EXIT_SUCCESS;
    }
    To iterate over all possible messages in each language, use e.g.
    Code:
    int main(void)
    {
        int msg, lang;
    
        for (msg = 0; msg < MSG_COUNT; msg++)
            for (lang = 0; lang < LANG_COUNT; lang++)
                printf("message[%d][%d] = \"%s\"\n", msg, lang, message[msg][lang]);
    
        return EXIT_SUCCESS;
    }
    instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Choice of data structure for storing an image
    By lilrayray in forum C Programming
    Replies: 5
    Last Post: 01-24-2010, 03:11 PM
  2. Data structure for storing dates
    By phoneix_hallows in forum C Programming
    Replies: 28
    Last Post: 08-30-2009, 11:38 AM
  3. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  4. different software versions
    By scotslass in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2002, 12:17 PM
  5. Storing Data in a Structure
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2001, 06:43 AM