diff --git a/translations/.gitignore b/translations/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..cf75c4f3f916fc1399785b7d58b9dcd00f9f379f --- /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 0000000000000000000000000000000000000000..85000ea455bcb370eccaa9804ddf273ad35a048c --- /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 0000000000000000000000000000000000000000..03eace7274ae24c02ae3e63339f85d5fff4e9657 --- /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 0000000000000000000000000000000000000000..657b2bae0e8af02dacfd86c00f0415018e453e8c --- /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 0000000000000000000000000000000000000000..a1e84e3bee3ad6d585cd90e8c95bcc84167b0acd --- /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); +}