Add Persian Language to Asterisk

In order to add Persian language we need to do some modifications on the Asterisk source code. For that, download the same Asterisk version as your current one (in case you’re using FreePBX/Trixbox). The main thing to change is the Asterisk say digits. Here is what must be changed:

  1. edit _AsteriskFolder_/main/say.c and add the following function somewhere in the file: (download say.c)
static int ast_say_number_full_fa(struct ast_channel *chan, int num, const char *ints, const char *language, int audiofd, int ctrlfd)
{
    int res = 0;
    int playh = 0;
    char fn[256] = "";
    if (!num)
        return ast_say_digits_full(chan, 0,ints, language, audiofd, ctrlfd);

    while (!res && (num || playh)) {
        if (num < 0) {
            //ast_copy_string(fn, "digits/minus", sizeof(fn));
            snprintf(fn, sizeof(fn), "digitsfarsi/minus");
            if ( num > INT_MIN ) {
                num = -num;
            } else {
                num = 0;
            }    
        } else if (playh) {
//            snprintf(fn, sizeof(fn), "digits/hundred");
            playh = 0;
        } else    if (num < 20) {
            snprintf(fn, sizeof(fn), "digitsfarsi/%d", num);
            num = 0;

                } else  if (num == 20 || num == 30 || num == 40 || num == 50 || num == 60 || num == 70 || num == 80 || num == 90) {
                        snprintf(fn, sizeof(fn), "digitsfarsi/%d", (num /10) * 10);
                        num -= ((num / 10) * 10);

//                } else  if (num > 20) {
//                        snprintf(fn, sizeof(fn), "digitsfarsi/_%d", (num /10) * 10);
//                        num -= ((num / 10) * 10);

//                } else  if (Substring(2, 2, 23) != 0) {
//                        snprintf(fn, sizeof(fn), "digitsfarsi/_%d", num);
//                        num = 0;

        } else    if (num < 100) {
            snprintf(fn, sizeof(fn), "digitsfarsi/_%d", (num /10) * 10);
            num -= ((num / 10) * 10);
        } else {
            if (num < 1000){
                snprintf(fn, sizeof(fn), "digitsfarsi/_%d", ((num/100) * 100));
//                playh++;
                num -= ((num / 100) * 100);
            } else {
                if (num < 1000000) { /* 1,000,000 */
                    res = ast_say_number_full_fa(chan, num / 1000, ints, language, audiofd, ctrlfd);
                    if (res)
                        return res;
                    num = num % 1000;
                    snprintf(fn, sizeof(fn), "digitsfarsi/thousand");
                } else {
                    if (num < 1000000000) {    /* 1,000,000,000 */
                        res = ast_say_number_full_fa(chan, num / 1000000, ints, language, audiofd, ctrlfd);
                        if (res)
                            return res;
                        num = num % 1000000;
                        snprintf(fn, sizeof(fn), "digitsfarsi/million");
                    } else {
                        ast_debug(1, "Number '%d' is too big for me\n", num);
//                        ast_debug(LOG_DEBUG, "Number '%d' is too big for me\n", num);
                        res = -1;
                    }
                }
            }
        }
        if (!res) {
            if (!ast_streamfile(chan, fn, language)) {
                if ((audiofd  > -1) && (ctrlfd > -1))
                    res = ast_waitstream_full(chan, ints, audiofd, ctrlfd);
                else
                    res = ast_waitstream(chan, ints);
            }
            ast_stopstream(chan);
        }
    }
    return res;
}

3) You need to compile asterisk now. Make sure you have all the required prerequisites installed and then run the following:

# ./configure
# make
# make instal

4) Load the sound files to /var/lib/asterisk/sounds/fa/digits (Download digits)

5) Setting the Language

The default language for a particular channel is set in that channel’s configuration file. Look for a setting like:

language=en

in the [general] section.

 

You may specify any text (up to 20 characters long) as the language code. When playing sounds from the /var/lib/asterisk/sounds directory, Asterisk will look for a subdirectory with the same name as the currently selected language.

You can override the channel’s default language using the SetLanguage command in your Dialplan. You may use the ${LANGUAGE} channel variable to discover what the currently selected language code is.

 

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *