{"meta":{"title":"Übersetzen von Code in eine andere Programmiersprache","intro":"Copilot-Chat kann dir beim Umschreiben von Code helfen, damit dieser in einer anderen Programmiersprache die gleichen Vorgänge ausführt.","product":"GitHub Copilot","breadcrumbs":[{"href":"/de/copilot","title":"GitHub Copilot"},{"href":"/de/copilot/tutorials","title":"Anleitungen"},{"href":"/de/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat-Cookbook"},{"href":"/de/copilot/tutorials/copilot-chat-cookbook/refactor-code","title":"Umgestalten von Code"},{"href":"/de/copilot/tutorials/copilot-chat-cookbook/refactor-code/translate-code","title":"Übersetzen von Code"}],"documentType":"article"},"body":"# Übersetzen von Code in eine andere Programmiersprache\n\nCopilot-Chat kann dir beim Umschreiben von Code helfen, damit dieser in einer anderen Programmiersprache die gleichen Vorgänge ausführt.\n\nEs gibt viele Gründe, warum du Code von einer Programmiersprache in eine andere umwandeln möchtest. Jede Programmiersprache hat ihre eigenen Stärken und Schwächen. Du kannst die Features nutzen, die in einer anderen Sprache verfügbar sind. Du kannst zum Beispiel Code in eine andere Programmiersprache umwandeln, die eine bessere Leistung bietet oder mit starker Typisierung dabei hilft, Bugs vorzubeugen.\n\nUm den Code einfacher zu verwalten, solltest du ihn in eine Programmiersprache umwandeln, die in deiner Organisation häufiger verwendet wird. Wenn beispielsweise nur wenige Personen in deiner Organisation mit einer älteren Programmiersprache wie Perl vertraut sind, kannst du noch genutzten Perl-Code in eine häufiger verwendete Programmiersprache wie Python oder JavaScript umwandeln.\n\nCopilot kann dir dabei helfen, Code von einer Programmiersprache in eine andere umzuwandeln. Das Übersetzen einer eigenständigen Datei wie z. B. eines Skripts ist leicht. Dieser Vorgang wird in diesem Artikel beschrieben.\n\nDas Übersetzen eines Projekts mit mehreren Dateien ist ein komplexerer Prozess und wird in [Verwenden von GitHub Copilot für die Migration eines Projekts zu einer anderen Programmiersprache](/de/copilot/tutorials/using-copilot-to-migrate-a-project) beschrieben.\n\n## Beispielszenario\n\nMit dem folgenden Perl-Skript wird die benutzende Person aufgefordert, den Pfad zu einer Textdatei einzugeben. Es wird überprüft, was die Person eingibt und ob eine Textdatei in diesem Pfad gefunden wird. Eine Wortanzahl und Zeichenanzahl werden für den Inhalt der Datei ausgegeben.\n\n```perl copy id=perl-script\n#!/usr/bin/perl\nuse strict;\nuse warnings;\nuse File::Spec;\nuse File::Basename;\n\nprint \"Please enter the path to a local text file: \";\nchomp(my $file_path = <STDIN>);\n\nif ($file_path =~ /[`|;&<>(){}\\[\\]]/ || !File::Spec->file_name_is_absolute($file_path) && dirname($file_path) ne '.') {\n    die \"Invalid file path. Please provide a valid path.\\n\";\n}\nif (!-e $file_path) {\n    die \"File not found. Please provide an existing file path.\\n\";\n}\n\nopen(my $fh, '<', $file_path) or die \"Could not open file '$file_path': $!\\n\";\n\nmy $is_binary = 0;\nwhile (read($fh, my $buffer, 1024)) {\n    if ($buffer =~ /[^\\x09\\x0A\\x0D\\x20-\\x7E]/) {\n        $is_binary = 1;\n        last;\n    }\n}\nclose($fh);\n\nif ($is_binary) {\n    die \"The file '$file_path' is a binary file. Please provide a text file.\\n\";\n}\n\nopen($fh, '<', $file_path) or die \"Could not open file '$file_path': $!\\n\";\nmy $content = do { local $/; <$fh> };\nclose($fh);\n\nmy $word_count = scalar(split /\\s+/, $content);\nmy $char_count = length($content);\n\nprint \"The file '$file_path' contains $word_count words and $char_count characters.\\n\";\n```\n\n## Beispielprompt\n\n```copilot copy prompt ref=perl-script\nTell me how I can convert this Perl script to Typescript. The functionality and output of the new script should be exactly the same as the existing one. Also tell me how to run the new file using Node.\n```\n\n## Beispielantwort\n\nCopilot generiert den folgenden TypeScript-Code.\n\n> \\[!NOTE] Da die Copilot-Chat-Antworten nicht deterministisch sind, erhältst du möglicherweise eine andere Antwort als die hier beschriebene.\n\n```typescript copy\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as readline from 'readline';\n\nconst rl = readline.createInterface({\n    input: process.stdin,\n    output: process.stdout\n});\n\nrl.question('Please enter the path to a local text file: ', (filePath) => {\n    rl.close();\n\n    if (/[`|;&<>(){}\\[\\]]/.test(filePath) || (!path.isAbsolute(filePath) && path.dirname(filePath) !== '.')) {\n        console.error('Invalid file path. Please provide a valid path.');\n        process.exit(1);\n    }\n\n    if (!fs.existsSync(filePath)) {\n        console.error('File not found. Please provide an existing file path.');\n        process.exit(1);\n    }\n\n    const fileBuffer = fs.readFileSync(filePath);\n    const isBinary = fileBuffer.some(byte => byte < 9 || (byte > 13 && byte < 32) || byte > 126);\n\n    if (isBinary) {\n        console.error(`The file '${filePath}' is a binary file. Please provide a text file.`);\n        process.exit(1);\n    }\n\n    const content = fileBuffer.toString('utf-8');\n    const wordCount = content.split(/\\s+/).length;\n    const charCount = content.length;\n\n    console.log(`The file '${filePath}' contains ${wordCount} words and ${charCount} characters.`);\n});\n```\n\nSpeichern Sie diese Datei als `wordcount.ts`.\n\nCopilot erklärt dann, wie die neue Datei mit Node ausgeführt wird.\n\n1. ```\n          **Installieren von Node.js:** Lade Node.js von der offiziellen Website herunter, und installiere sie, wenn dies noch nicht geschehen ist: https://nodejs.org/.\n   ```\n\n2. ```\n          **Installieren von TypeScript:**\n   ```\n\n   ```shell\n   npm install -g typescript\n   ```\n\n3. ```\n          **Kompilieren des TypeScript-Codes:**\n   ```\n\n   ```shell\n   tsc wordcount.ts\n   ```\n\n   Dadurch wird eine `wordcount.js`-Datei im selben Verzeichnis erstellt.\n\n4. ```\n          **Ausführen der JavaScript-Datei mit Node.js:**\n   ```\n\n   ```shell\n   node wordcount.js\n   ```\n\n## Weitere Informationen\n\n* [Prompt-Engineering für GitHub Copilot Chat](/de/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Bewährte Methoden für die Verwendung von GitHub Copilot](/de/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}