From 28466a329caa77e0c42be24790f7fb3bcd2b721d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= <martin.sjolund@liu.se>
Date: Wed, 26 Jun 2019 10:03:32 +0200
Subject: [PATCH] Added part of the translation lab

---
 translations/.gitignore    |  3 +++
 translations/Makefile      |  8 ++++++++
 translations/base.c        | 19 +++++++++++++++++++
 translations/gettext.c     |  9 +++++++++
 translations/setuplocale.c | 20 ++++++++++++++++++++
 5 files changed, 59 insertions(+)
 create mode 100644 translations/.gitignore
 create mode 100644 translations/Makefile
 create mode 100644 translations/base.c
 create mode 100644 translations/gettext.c
 create mode 100644 translations/setuplocale.c

diff --git a/translations/.gitignore b/translations/.gitignore
new file mode 100644
index 0000000..cf75c4f
--- /dev/null
+++ b/translations/.gitignore
@@ -0,0 +1,3 @@
+*-main.c
+*.mo
+gettext
diff --git a/translations/Makefile b/translations/Makefile
new file mode 100644
index 0000000..85000ea
--- /dev/null
+++ b/translations/Makefile
@@ -0,0 +1,8 @@
+all: gettext-main.c linguist-main.c
+
+gettext: gettext-main.c setuplocale.c gettext.c
+
+gettext-main.c: base.c
+	cp $< $@
+linguist-main.c: base.c
+	cp $< $@
diff --git a/translations/base.c b/translations/base.c
new file mode 100644
index 0000000..03eace7
--- /dev/null
+++ b/translations/base.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+void setup();
+void setuplocale();
+
+int main(int argc, char** argv)
+{
+  setuplocale();
+  setup();
+  /* TODO: You need to update this code to output translated strings
+   * Note that you should make gettext-main.c linguist-main.c and modify
+   * those files rather than base.c
+   */
+  printf("The current language is the default (C/POSIX)\n");
+  printf("The horse can run.\n");
+  printf("How fast can the horse run?\n");
+  printf("It typically runs at speed of less than 50 km/h.\n");
+  return 0;
+}
diff --git a/translations/gettext.c b/translations/gettext.c
new file mode 100644
index 0000000..657b2ba
--- /dev/null
+++ b/translations/gettext.c
@@ -0,0 +1,9 @@
+#include <locale.h>
+#include <libintl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+void setup()
+{
+  /* Your code here. Setup a domain for gettext. */
+}
diff --git a/translations/setuplocale.c b/translations/setuplocale.c
new file mode 100644
index 0000000..a1e84e3
--- /dev/null
+++ b/translations/setuplocale.c
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <locale.h>
+
+void setuplocale()
+{
+  /* Usually, the locale is taken from the user's environment.
+   * We have hard-coded a locale for purposes of this lab.
+   */
+  char *newlocale, *targetLocale = "sv_SE.utf8";
+  setenv("LANGUAGE", targetLocale, 1);
+  setenv("LANG", targetLocale, 1);
+  setenv("LC_MESSAGES", targetLocale, 1);
+  newlocale = setlocale(LC_MESSAGES, targetLocale);
+  if (newlocale == NULL) {
+    fprintf(stderr, "Error: %s locale not installed (perhaps use a different hard-coded locale as a work-around)\n", targetLocale);
+    exit(1);
+  }
+  printf("Set locale to: %s\n", newlocale);
+}
-- 
GitLab