Improved Language Support
This commit is contained in:
@ -7,13 +7,29 @@
|
||||
|
||||
#include "language.h"
|
||||
|
||||
void languageInit(language_t *language) {
|
||||
language->stringCount = 0;
|
||||
}
|
||||
|
||||
languagestring_t * languageAddString(language_t *lang, char *key, char *text) {
|
||||
languagestring_t *string;
|
||||
|
||||
string = lang->strings + lang->stringCount;
|
||||
string->key = key;
|
||||
string->text = text;
|
||||
|
||||
lang->stringCount++;
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
languagestring_t * languageGetStringByKey(language_t *language, char *key) {
|
||||
int32_t i;
|
||||
languagestring_t *string;
|
||||
|
||||
for(i = 0; i < language->stringCount; i++) {
|
||||
string = language->strings + i;
|
||||
if(string->key != key) continue;
|
||||
if(strcmp(string->key, key) != 0) continue;
|
||||
return string;
|
||||
}
|
||||
|
||||
@ -21,25 +37,103 @@ languagestring_t * languageGetStringByKey(language_t *language, char *key) {
|
||||
}
|
||||
|
||||
char * languageGetText(language_t *language, languagestring_t *string) {
|
||||
// Try and find the value
|
||||
|
||||
// Load the value into memory.
|
||||
|
||||
// Return the value
|
||||
return NULL;
|
||||
// TODO: Buffer this from somewhere.
|
||||
return string->text;
|
||||
}
|
||||
|
||||
char * languageGetTextWithVariables(
|
||||
language_t *language, languagestring_t *string, languagevariable_t *variable
|
||||
int32_t languageGetTextWithVariables(
|
||||
language_t *language, languagestring_t *string,
|
||||
languagevariable_t *variables, int32_t variableCount,
|
||||
char *buffer
|
||||
) {
|
||||
char *text = languageGetText(language, string);
|
||||
if(text == NULL) return NULL;
|
||||
int32_t i, l, n;
|
||||
char *text;
|
||||
char c;
|
||||
char keyBuffer[32];
|
||||
languagevariable_t *variable;
|
||||
|
||||
// Scan the string, determine the new length and positions of the variables
|
||||
text = languageGetText(language, string);
|
||||
if(text == NULL) return 0;
|
||||
|
||||
// Now create some memory for the new string
|
||||
// Start two counters. I holds source index, L holds target index.
|
||||
i = 0;
|
||||
l = 0;
|
||||
|
||||
// Now buffer the old string into the new string and take the language
|
||||
while(true) {
|
||||
c = text[i];
|
||||
if(c == '\0') break;// Break on end of string.
|
||||
|
||||
return text;
|
||||
// Look for {{, if not {{ then just treat as normal string.
|
||||
if(c != '{' || text[i + 1] != '{') {
|
||||
buffer[l] = c;
|
||||
i++;
|
||||
l++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//Ignore those two chars
|
||||
i += 2;
|
||||
|
||||
// Skip space(s)
|
||||
while(true) {
|
||||
c = text[i];
|
||||
if(c != ' ') break;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Get the key name
|
||||
n = 0;// Will hold the index within keyBuffer to copy chars into.
|
||||
while(true) {
|
||||
c = text[i];
|
||||
|
||||
// Don't overflow
|
||||
if(c == '\0') break;
|
||||
|
||||
// Skip spaces
|
||||
if(c == ' ') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle end of key
|
||||
if(c == '}') {
|
||||
i++;
|
||||
if(text[i] == '}') i++;// For }} then skip the second }
|
||||
break;
|
||||
}
|
||||
|
||||
// Add to buffer.
|
||||
keyBuffer[n] = c;
|
||||
n++;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Seal the keyBuffer string
|
||||
keyBuffer[n] = '\0';
|
||||
|
||||
// Now check each variable for a match.
|
||||
for(n = 0; n < variableCount; n++) {
|
||||
variable = variables + n;
|
||||
if(strcmp(keyBuffer, variable->name) != 0) continue;// Does the key match?
|
||||
|
||||
// Begin copying the variables' value into the string.
|
||||
n = 0;
|
||||
while(true) {
|
||||
if(variable->value[n] == '\0') break;// Handle end of variable value
|
||||
buffer[l] = variable->value[n];
|
||||
l++;
|
||||
n++;
|
||||
}
|
||||
// We found the value, so break.
|
||||
break;
|
||||
}
|
||||
|
||||
// Continue looking for next {{ variable }}...
|
||||
}
|
||||
|
||||
// Buffer has been fully cloned, seal the string.
|
||||
buffer[l] = '\0';
|
||||
|
||||
// Return the count of chars we wrote to the buffer. -1 due to null term.
|
||||
return l - 1;
|
||||
}
|
@ -8,6 +8,21 @@
|
||||
#pragma once
|
||||
#include <dawn/dawn.h>
|
||||
|
||||
/**
|
||||
* Initializes a language.
|
||||
* @param language Language to initialize.
|
||||
*/
|
||||
void languageInit(language_t *language);
|
||||
|
||||
/**
|
||||
* Adds a string to a language.
|
||||
* @param lang Language to add to.
|
||||
* @param key Key to use for the string.
|
||||
* @param text Value to use for the language string
|
||||
* @return The language string added to the language.
|
||||
*/
|
||||
languagestring_t * languageAddString(language_t *lang, char *key, char *text);
|
||||
|
||||
/**
|
||||
* Returns the language string for a given language string key.
|
||||
*
|
||||
@ -26,6 +41,19 @@ languagestring_t * languageGetStringByKey(language_t *language, char *key);
|
||||
*/
|
||||
char * languageGetText(language_t *language, languagestring_t *string);
|
||||
|
||||
char * languageGetTextWithVariables(
|
||||
language_t *language, languagestring_t *string, languagevariable_t *variable
|
||||
/**
|
||||
* Retreives the value of a given language string, and replaces the variables in
|
||||
* the string with the provided variable values.
|
||||
*
|
||||
* @param language Language to read from.
|
||||
* @param string String to get and parse.
|
||||
* @param variables Variables to use in the parsing.
|
||||
* @param variableCount How many variables in the array.
|
||||
* @param buffer The buffer to write the output data to.
|
||||
* @return The count of characters that were written to the buffer.
|
||||
*/
|
||||
int32_t languageGetTextWithVariables(
|
||||
language_t *language, languagestring_t *string,
|
||||
languagevariable_t *variables, int32_t variableCount,
|
||||
char *buffer
|
||||
);
|
Reference in New Issue
Block a user