/svarlang.lib/svarlang.c |
---|
52,15 → 52,22 |
unsigned short buff16[2]; |
unsigned short i; |
if ((lang == NULL) || (nlspath == NULL)) return(-1); |
if (lang == NULL) return(-1); |
if (nlspath == NULL) nlspath = ""; /* nlspath can be NULL, treat is as empty */ |
langid = *((unsigned short *)lang); |
langid &= 0xDFDF; /* make sure lang is upcase */ |
TRYNEXTPATH: |
/* skip any leading ';' separators */ |
while (*nlspath == ';') nlspath++; |
/* copy nlspath to buff and remember len */ |
for (i = 0; nlspath[i] != 0; i++) buff[i] = nlspath[i]; |
for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i]; |
nlspath += i; |
/* */ |
/* ignore the trailing backslash (I add one myself) */ |
if ((i > 0) && (buff[i - 1] == '\\')) i--; |
buff[i++] = '\\'; |
68,7 → 75,10 |
strcat(buff + i, ".lng"); |
fd = fopen(buff, "rb"); |
if (fd == NULL) return(-2); |
if (fd == NULL) { /* failed to open file - either abort or try next path */ |
if (*nlspath == 0) return(-2); |
goto TRYNEXTPATH; |
} |
/* read hdr, should be "SvL\33" */ |
if ((fread(buff, 1, 4, fd) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) { |
/svarlang.lib/svarlang.h |
---|
25,11 → 25,25 |
#ifndef SVARLANG_H |
#define SVARLANG_H |
/* loads translations for program PROGNAME, language LANG, in the path NLSPATH. |
* returns 0 on success. */ |
int svarlang_load(const char *progname, const char *lang, const char *nlspath); |
/* loads translations for program progname, language lang, in paths. |
* |
* only the two first letters of the lang strings are meaningful and they are |
* case insensitive. |
* |
* paths can be either a directory path (like "C:\DATA") or a list of paths |
* separated by a semicolon (example: "C:\DATA;.\LANGS;."). It may also be |
* NULL, in which case only the current directory will be searched. |
* |
* a typical call would be this: svarlang_load("myprog", "PL", NULL); |
* |
* this function returns 0 on success, non-zero otherwise. It is still possible |
* to call svarlang_strid() after a load failure, the previously loaded |
* language will be used then, or the default language if no loading has been |
* done yet. */ |
int svarlang_load(const char *progname, const char *lang, const char *paths); |
/* same as svarlang_load(), but relies on getenv() to pull LANG and NLSPATH. */ |
/* same as svarlang_load(), but relies on getenv() to pull LANG and NLSPATH. |
* this call should be used only by "CORE" SvarDOS programs. */ |
int svarlang_autoload(const char *progname); |
/* Returns a pointer to the string "id". Does not require svalang_load() to be |
/svarlang.lib/svarlang.txt |
---|
1,7 → 1,11 |
SVARLANG.LIB - THE SVARDOS TRANSLATION C LIBRARY |
Copyright (C) 2021-2022 Mateusz Viste |
SvarLANG is a library and tooling for enabling SvarDOS applications to easily |
support multiple languages. |
29,33 → 33,24 |
* DEFLANG.C - the default translations that will be embedded into the program |
Then, DEFLANG.C must be compiled and linked to your program along with |
SVARLNGx.LIB. From there, you will be able to use the following calls: |
SVARLNGx.LIB. From there you will be able to use SvarLANG calls, typically: |
/* loads translations for program PROGNAME, language LANG, in the path NLSPATH. |
* returns 0 on success. */ |
int svarlang_load(const char *progname, const char *lang, const char *nlspath); |
svarlang_load("myprogram", "pl", "."); /* load .\myprogram.lng */ |
puts(svarlang_str(2, 0)); /* display the string with id 2.0 */ |
/* same as svarlang_load(), but relies on getenv() to pull LANG and NLSPATH. */ |
int svarlang_autoload(const char *progname); |
Read svarlang.h for more information about available functions. |
/* Returns a pointer to the string "id". Does not require svalang_load() to be |
* executed, but then it will only return the reference language strings. |
* a string id is the concatenation of the CATS-style identifiers, for example |
* string 1,0 becomes 0x0100, string 2.10 is 0x020A, etc. */ |
const char *svarlang_strid(unsigned short id); |
/* a convenience definition to fetch strings by their CATS-style pairs instead |
* of the 16-bit id. */ |
#define svarlang_str(x, y) svarlang_strid((x << 8) | y) |
### ENVIRONMENT ### |
All translations files should be placed in the %NLSPATH% directory and should |
be named the same as the program's name, with the LNG extension (for example |
"INSTALL.LNG" if the program is named "INSTALL"). |
The program translation file should be named "PROGNAME.LNG", where PROGNAME |
is the program's name. This file should be placed in a well-known location, |
typically the program's own directory. An exception are SvarDOS "CORE" programs |
that store their translation files in a directory pointed out by %NLSPATH%. |
The %LANG% environment variable defines what language should be loaded. |
The %LANG% environment variable usually defines what language should be loaded, |
albeit the program can just as well provide its own controls for language |
selection and pass this information to svarlang_load() accordingly. |
### WHY IS IT BETTER THAN CATS? ### |