summaryrefslogtreecommitdiffstats
path: root/src/lng.c
blob: 607c69d832ff298428e4402d6a76fdcd311acbec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <string.h>
#include <locale.h>

#include <librcd.h>
#include "librcc.h"

const char *rccGetLanguageName(rcc_context *ctx, rcc_language_id language_id) {
    if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL;
    return ctx->languages[language_id]->sn;
}

language_id rccGetLanguageByName(rcc_context *ctx, const char *name) {
    unsigned int i;
    if ((!ctx)||(!name)) return 0;
    
    for (i=0;ctx->languages[i];i++) 
	if (!strcmp(ctx->languages[i]->sn, name)) return i;

    return 0;
}

static int rccGetLocaleLanguage(char *result, const char *lv, unsigned int n) {
    charset_list_t *enc;
    char *l;
    
    if (!lv) return -1;

    l = setlocale(lv, NULL);
    if (!l) return -1;
    else if ((strcmp(l,"C")==0)||(strcmp(l,"POSIX")==0)) return -1;

    for (i=0;((l[i])&&(l[i]!='.'));i++);

    for (i=0;rcc_default_aliases[i].alias;i++) 
	if (strncmp(l,rcc_default_aliases[i].alias,i)==0) {
	    l = rcc_default_aliases[i].alias;
	    break;
	}

    for (i=0;((l[i])&&(l[i]!='.')&&(l[i]!='_'));i++);
    if (i>=n) return -1;

    strncpy(result,l,i);
    result[i]=0;

    return 0;
}

static rcc_language_id rccGetDefaultLanguage(rc_context *ctx) {
    int err;
    unsigned int i;
    char stmp[RCC_MAX_LANGUAGE_CHARS+1];

    if (!ctx) return -1;
    
    err = rccGetLocaleLanguage(stmp, ctx->locale_variable, RCC_MAX_LANGUAGE_CHARS);
    if (err) {
	if (ctx->n_languages>1) return 1;
	return -1;
    }
    
    for (i=0;ctx->languages[i];i++)
	if (!strcmp(ctx->languages[i]->sn, stmp)) return i;
    
    if (i>1) return 1;
    return -1;
}

rcc_language_id rccGetRealLanguage(rcc_context *ctx, rcc_language_id language_id) {
    if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return -1;
    if (language_id) return language_id;
    return rccGetDefaultLanguage(ctx);    
}

const char *rccGetRealLanguageName(rcc_context *ctx, rcc_language_id language_id) {
    language_id = rccGetRealLanguage(ctx, language_id);
    if (language_id<0) return NULL;
    
    return rccGetLanguageName(ctx, language_id);
}

rcc_language_id rccGetSelectedLanguage(rcc_context *ctx) {
    if (!ctx) return NULL;
    return ctx->current_language;
}

const char *rccGetSelectedLanguageName(rcc_context *ctx) {
    rcc_language_id language_id;
    
    language_id = rccGetSelectedLanguage(ctx);
    if (language_id<0) return NULL;
    
    return rccGetLanguageName(ctx, language_id);
}

rcc_language_id rccGetCurrentLanguage(rcc_context *ctx) {
    if (!ctx) return -1;
    return rccGetRealLanguage(ctx, ctx->current_language);    
}

const char *rccGetCurrentLanguageName(rcc_context *ctx) {
    rcc_language_id language_id;
    
    language_id = rccGetCurrentLanguage(ctx);
    if (language_id<0) return NULL;
    
    return rccGetLanguageName(ctx, language_id);
}


int rccSetLanguage(rcc_context *ctx, rcc_language_id language_id) {
    rcc_language_config config;
    
    if ((!ctx)||(language_id < 0)||(language_id >= ctx->n_languages)) return -1;
    if ((!ctx->languages[language_id]->engines[0])||(!ctx->languages[language_id]->charsets[0])) return -2;

    if (ctx->current_language != language_id) {
	config = rccGetConfig(ctx, language_id);
	if (!config) return -1;
	
	ctx->configure = 1;
	ctx->current_language = language_id;
	ctx->current_config = config; 
    }
}

int rccSetLanguageByName(rcc_context *ctx, const char *name) {
    rcc_language_id language_id;
    
    language_id = rccGetLanguageByName(ctx, name);
    if (language_id < 0) return -1;
    
    return rccSetLanguage(ctx, language_id);
}