From 55ddc29c770241ad0d96276edd0833ddc63aa793 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Thu, 19 Jan 2017 13:43:32 -0500 Subject: [PATCH] ADDED: Basic localization configuration --- CMakeLists.txt | 3 ++ src/html.c | 4 +- src/localization.h | 114 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/localization.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 19d6bcb..c37b336 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,9 @@ include_directories( ${PROJECT_SOURCE_DIR}/src ) include_directories( ${PROJECT_SOURCE_DIR}/test ) include_directories(${PROJECT_BINARY_DIR}) +# Default is 256 -- needed for localization hash function +add_definitions("-fbracket-depth=264") + # ================= # Macro Definitions diff --git a/src/html.c b/src/html.c index 431c119..57d4dfe 100644 --- a/src/html.c +++ b/src/html.c @@ -60,15 +60,13 @@ #include "d_string.h" #include "html.h" #include "libMultiMarkdown.h" +#include "localization.h" #include "parser.h" #include "token.h" #include "scanners.h" #include "writer.h" -#define LC(x) x - - #define print(x) d_string_append(out, x) #define print_char(x) d_string_append_c(out, x) #define printf(...) d_string_append_printf(out, __VA_ARGS__) diff --git a/src/localization.h b/src/localization.h new file mode 100644 index 0000000..3c23324 --- /dev/null +++ b/src/localization.h @@ -0,0 +1,114 @@ +/** + + MultiMarkdown 6 -- MultiMarkdown - lightweight markup processor. + + @file localization.h + + @brief + + + @author Fletcher T. Penney + @bug + +**/ + +/* + + Copyright © 2016 - 2017 Fletcher T. Penney. + + + + +*/ + + +#ifndef LOCALIZATION_MULTIMARKDOWN_6_H +#define LOCALIZATION_MULTIMARKDOWN_6_H + + +#include +#include +#include +#include + + + +#define kNumberOfLanguages 4 +#define kNumberOfStrings 3 +#define kLanguage 0 + +//#define LOCALIZATION_DISABLED + + +// Hash function from http://lolengine.net/blog/2011/12/20/cpp-constant-string-hash +// via http://stackoverflow.com/questions/2826559/compile-time-preprocessor-hashing-of-string + + +#ifdef LOCALIZATION_DISABLED + #define LC(x) x +#else + #define H1(s,i,x) (x*65599u+(uint8_t)s[(i)>16))) + + #define LC(x) Translate(HASH(x), kLanguage) + + //#define LC(x) TranslateTest(__COUNTER__, __FILE__, __LINE__, __FUNCTION__ , x) + +static const char * lc_lookup[kNumberOfLanguages * kNumberOfStrings] = { + "return to body", + "return to body", + "return to body", + "return to le body", + + "see footnote", + "see footnote", + "see footnote", + "Ver nota de pie", + + "see citation", + "see citation", + "see citation", + "ver citation", +}; + + +static inline const char * TranslateTest(int c, char * file, int line, const char func[], char * x) { + fprintf(stderr, "%s: %d (%s) -> %d\n", file, line, func, c); + unsigned long h = HASH(x); + fprintf(stderr, "hash '%s' -> %lu\n", x, h); + + return lc_lookup[(c * kNumberOfLanguages) + kLanguage]; +} + +static inline const char * Translate(unsigned long x, int l) { + switch (x) { + case 3219553713: + return lc_lookup[0 * kNumberOfLanguages + l]; + case 657226305: + return lc_lookup[1 * kNumberOfLanguages + l]; + case 2977473004: + return lc_lookup[2 * kNumberOfLanguages + l]; + default: + return "localization error"; + } +} + + +// Based on ISO 639-1 names +// https://en.wikipedia.org/wiki/ISO_639-1 +enum lc_languages { + LC_EN = 0, //!< English is default + LC_FR, + LC_DE, + LC_ES, +}; + + +#endif + +#endif -- 2.40.0