Skip to content

Commit

Permalink
Merge pull request #213 from Ruby-Dragon/main
Browse files Browse the repository at this point in the history
add two letter language code fallback
  • Loading branch information
MrGlockenspiel authored Sep 5, 2024
2 parents 63f5e5a + 41f4c89 commit 633b280
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

// Compare 5 first chars from strings
#define match_str(match, with) (match && with && (strncmp(match, with, 5) == 0))

// Length of array
#define length(array) (sizeof(array) / sizeof(array[0]))

Expand Down Expand Up @@ -131,6 +132,40 @@ bool match_lang_code(const char *lang_code, const char *lang) {
return false;
}

bool match_lang_two_letter_code(const char *lang_code, const char *lang) {
int i = 0;
while (lang_code[i]) {
int ii = 0;
bool failed = false;

while (lang_code[i] != 0) {
if (lang_code[i] == '_') {
i +=4;
break;
}
if (lang_code[i] == ',') {
i++;
break;
}

if (failed) {
i++;
continue;
}

if (lang_code[i++] != lang[ii++]) {
failed = true;
}
}

if (!failed) {
return true;
}
}

return false;
}

void i18n_set_lang_id(void) {
if (lang_id != -1)
return;
Expand Down Expand Up @@ -161,6 +196,11 @@ void i18n_set_lang_id(void) {
if (match_lang_code(langs[lang_id].code, lang))
return;

__info__("Attempting to use two-letter code as fallback\n");
for (lang_id = length(langs) - 1; lang_id >= 0; lang_id--)
if (match_lang_two_letter_code(langs[lang_id].code, lang))
return;

lang_id = 0;
__error__("activate-linux lacks translation for `%s' language. You are welcome to fix this :3\n", lang);
__error__("Using English translation\n");
Expand Down

0 comments on commit 633b280

Please # to comment.