[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cdi-devel] Multilinguales Hello World [1]



Mit ein wenig Compiler- und Linker-Magie ist es moeglich, Uebersetzungen
nur dazulinken zu muessen, damit sie funktionieren.

[1] Multilingual bezieht sich uebrigens sowohl auf Programmier- als auch
auf natuerliche Sprachen ;-)
---
 lang_de.c |   25 +++++++++++++++++++++++
 lang_is.c |   25 +++++++++++++++++++++++
 main.pas  |   17 +++++++++++++++
 res.pas   |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tms.h     |   20 ++++++++++++++++++
 5 files changed, 153 insertions(+), 0 deletions(-)
 create mode 100644 lang_de.c
 create mode 100644 lang_is.c
 create mode 100644 main.pas
 create mode 100644 res.pas
 create mode 100644 tms.h

diff --git a/lang_de.c b/lang_de.c
new file mode 100644
index 0000000..4cb80c3
--- /dev/null
+++ b/lang_de.c
@@ -0,0 +1,25 @@
+#include "tms.h"
+
+extern void* RESSTR_MAIN_RSHELLOWORLD;
+
+static int get_number(int n)
+{
+    return (n == 1 ? 0 : 1);
+}
+
+static const struct tms_strings dict[] = {
+    &RESSTR_MAIN_RSHELLOWORLD,
+    "Hallo Welt!",
+
+    0,
+    0,
+};
+
+static const struct tms_lang lang = {
+    .lang = "de",
+    .numbers = 2,
+    .get_number = get_number,
+    .strings = dict,
+};
+
+LANGUAGE(&lang)
diff --git a/lang_is.c b/lang_is.c
new file mode 100644
index 0000000..4eb8eb5
--- /dev/null
+++ b/lang_is.c
@@ -0,0 +1,25 @@
+#include "tms.h"
+
+extern void* RESSTR_MAIN_RSHELLOWORLD;
+
+static int get_number(int n)
+{
+    return (n == 1 ? 0 : 1);
+}
+
+static const struct tms_strings dict[] = {
+    &RESSTR_MAIN_RSHELLOWORLD,
+    "Halló heimur!",
+
+    0,
+    0,
+};
+
+static const struct tms_lang lang = {
+    .lang = "is",
+    .numbers = 2,
+    .get_number = get_number,
+    .strings = dict,
+};
+
+LANGUAGE(&lang)
diff --git a/main.pas b/main.pas
new file mode 100644
index 0000000..c142932
--- /dev/null
+++ b/main.pas
@@ -0,0 +1,17 @@
+unit main;
+
+interface
+
+    resourcestring
+        rsHelloWorld = 'Hello World!';
+
+    procedure run;
+
+implementation
+
+    procedure run;
+    begin
+        WriteLn(rsHelloWorld);
+    end;
+
+end.
diff --git a/res.pas b/res.pas
new file mode 100644
index 0000000..6234ea5
--- /dev/null
+++ b/res.pas
@@ -0,0 +1,66 @@
+program res;
+{$MODE ObjFPC}
+{$H+}
+
+uses main;
+
+{$L lang_de.o}
+{$L lang_is.o}
+
+type
+    tres = packed record
+        name: AnsiString;
+        value: AnsiString;
+    end;
+
+    tms_dict = packed record
+        resstr: ^tres;
+        translation: PChar;
+    end;
+    ptms_dict = ^tms_dict;
+
+function LoadTranslation(name, value: AnsiString; Hash: Longint; arg: Pointer): AnsiString;
+var
+    dict: ptms_dict;
+    i: integer;
+begin
+    dict := arg;
+    i := 0;
+    while dict[i].resstr <> nil do begin
+        if Pointer(name) = Pointer(dict[i].resstr^.name) then begin
+            exit(dict[i].translation);
+        end;
+        Inc(i);
+    end;
+
+    exit(value);
+end;
+
+type
+    TGetNumber = function(n: integer): integer;
+    TLanguage = record
+        lang:       PChar;
+        numbers:    integer;
+        get_number: TGetNumber;
+        strings:    ptms_dict;
+    end;
+    PLanguage = ^TLanguage;
+    PPLanguage = ^PLanguage;
+
+var
+    tmslang_start: PLanguage; external name '__start_tmslang';
+    tmslang_end: PLanguage; external name '__stop_tmslang';
+
+    lang: PPLanguage;
+begin
+    main.run;
+
+    lang := @tmslang_start;
+    while lang < @tmslang_end do begin
+        WriteLn;
+        WriteLn('==== ', lang^^.lang);
+        SetResourceStrings(@LoadTranslation, lang^^.strings);
+        main.run;
+        Inc(lang);
+    end;
+end.
diff --git a/tms.h b/tms.h
new file mode 100644
index 0000000..ae4b73c
--- /dev/null
+++ b/tms.h
@@ -0,0 +1,20 @@
+#ifndef _TMS_H_
+#define _TMS_H_
+
+struct tms_strings {
+    void*   resstr;
+    char*   translation;
+} __attribute__((packed));
+
+struct tms_lang {
+    char*   lang;
+    int     numbers;
+    int     (*get_number)(int);
+
+    const struct tms_strings* strings;
+};
+
+#define LANGUAGE(x) \
+    static const void* __attribute__((section("tmslang"))) __lang = (x);
+
+#endif
-- 
1.6.0.2