A Polyglot Polyglot Program
Here’s kind of a cool code golf challenge: write a single program that, when run in different programming languages, produces the classic “Hello, World!” string in different natural languages. So it might produce “ሰላም ልዑል!” when run in C (Amharic for Hello World!) and “Hej Verden!” (Danish Hello World!) when run in Python.
The name for a program that runs successfully in different languages is a polyglot. This program is a polyglot at both the code and the natural language level. The example includes a list of Hello World translations to draw from:
...
Armenian: Բարեւ աշխարհ!
Basque: Kaixo Mundua!
Belarussian: Прывітанне Сусвет!
Bengali: ওহে বিশ্ব!
Bulgarian: Здравей свят!
Catalan: Hola món!
Chichewa: Moni Dziko Lapansi!
Chinese: 你好世界!
...
There are a number of common tricks for writing polyglot programs, such as using comments or preprocessor directives that only exist in one language and not the other to cause certain lines to be ignored.
The objective in code golf is to produce the best results (in this case, the most variations) in the shortest code, as with code length determined in bytes (hence golf, the lowest score wins). The competition is still active, but the current leader (in terms of upvotes) has 16 languages in 1363 bytes. Here is the code:
#define ip"MemuL tulaS",,,,,,,,,,"!",@++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.@,kc"Kaixo Mundua!"v#!
#define print(A,B,C)main(){printf("Helo Byd!");}//ss ;ooooooooooooooooooo"Moni Dziko Lapansi!"<
#ifdef __cplusplus//p ;;];;
#include<cstdio>//ffffffffff?
#define print(A,B,C)int main(){printf("Halo Dunya!");}//ssp
#endif//; [;;;;;;;;; "!etejivs vardzoP"]
#ifdef __OBJC__//;;;;;
#define print(A,B,C)main(){printf("Hallo Welt!");}//ss
#endif//\
"""echo" "Salom Dunyo!";"exit";puts"Moien Welt!";\
__END__
#define B//\
"""#;;;;;
print(["Hai dunia!","Hej Verden!","Halo Dunia!"][(int(1>0)is 1)+~int(-1/2)*2])
#define s eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeejeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeejiiiiiiiijeeeeeeeeeeeeeejiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiijeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeejeejijiiiiiiiiiijeeeeeeeeeeejiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiij
#define U !"!dlereW ollaH"< ;
#define T@,kc"Sannu Duniya!?%?"
These are the language combinations:
Python 3 = Halo Dunia! (Indonesian)
Python 2 = Hej Verden! (Danish)
Python 1 = Hai dunia! (Malay)
C (gcc) = Helo Byd! (Welsh)
C++ (gcc) = Halo Dunya! (Sundanese)
Objective C (clang) = Hallo Welt! (German)
Brainfuck = Hello World! (English, obviously)
Befunge-93 = Salut Lume! (Romanian)
Ruby = Moien Welt! (Luxemburgish)
Bash = Salom Dunyo! (Uzbek)
Befunge-98 (PyFunge) = Kaixo Mundua! (Basque)
Unefunge-98 (PyFunge) = Sannu Duniya! (Hausa)
Cardinal = Hallo Wereld! (Dutch)
Alphuck = Ciao mondo! (Italian)
><> = Moni Dziko Lapansi! (Chichewa)
Fission = Pozdrav svijete! (Croatian)
You can get a sense of how this comes together in the first line; all that crazy punctuation is a brainfuck program, but since brainfuck only reads punctuation, all the rest of the program is ignored. Meanwhile, the #define only has meaning in C-derived languages, but the # character is a comment in Python, so the Python 1, 2, and 3 instances ignore the line completely.