JavaScript translation:Hidden pitfalls and tips you need to know
Consejos para traducir JavaScript: Hidden pitfalls and tips you need to know when translating JavaScript code
Did you know that some JavaScript strings, comments, and identifiers have special meaning in the context of a translation? Or that a string might be split and merged depending on the context? Or even that there are special characters used for code snippets? If you are a professional technical translator who frequently deals with source code, you likely have already come across JavaScript at some point. But how comfortable are you with this programming language? Do you know everything there is to know about it from the perspective of a JavaScript technical translator? JavaScript is one of the most potent languages for front-end web development. However, when you decide to translate JavaScript code, it’s not always straightforward and can be difficult. There are a lot of potential pitfalls that you need to look out for. This blog will look at some of the hidden pitfalls and tips you need to know when translating JavaScript code.
Hidden pitfall: Automatic identification of comments and strings in JavaScript
JavaScript has built-in functionality that automatically identifies comments and strings that contain a sequence of characters that looks like a comment or a string. This functionality may cause problems when you try to translate your code. JavaScript recognizes two types of comments: single-line comments that start with “//” and multiline comments that begin with /* and conclude with */. These comments can be found in many programming languages and are often used to write notes or instructions for future maintenance. In the context of a translation, they may contain text that should be included in the translation. The same holds for strings that contain a sequence of characters that looks like a string. The language might have a special syntax for concatenating or embedding strings. However, when JavaScript automatically identifies strings, these special symbols are no longer present in the source code. This means the translation will contain the source code’s literal values and not the intended translation.
Hidden pitfall: Automated translation tools are not always reliable for translating JavaScript code
Automated translation tools are designed to recognize source code and store strings, comments, and identifiers as is. Unfortunately, they know nothing about special characters used in JavaScript. This means they will not be able to recognize them as part of the source code. This can lead to erroneous or missing technical translations as a result. Therefore, it is recommended that automated translation tools not be used to translate JavaScript. Instead, you should translate the source code manually. This process gives you complete control over the code and ensures that your translation will be accurate.
Hidden pitfall: Identifiers have special meaning in the context of a translation
Some identifiers have special meaning in a programming language used for various operations in the code. In other programming languages, identifiers are usually written in lowercase letters and in CamelCase for class and method definitions. However, in JavaScript, identifiers use snake_case. This means that you can write one identifier name in different ways. For example, the identifier for the “if” statement is if, and the identifier for the “for” statement is for. Identifiers can be written differently, and automated translation tools don’t know which one to choose. To ensure that your translator will recognize all identifiers correctly and choose the correct translation, you must be very careful when writing the source code. Ensure you follow the Writing Guidelines below, and don’t forget to use PEP8-style identifiers. This will make your life a lot easier when translating your code.

Photo by Gabriel Heinzer on Unsplash
Hidden pitfall: Strings might be split and merged depending on the context
There are several circumstances when a string may split and merge based on the context, such as when you wish to include the string in multiple parts of the code. Typically, the string will be split at the first occurrence of a special JavaScript character known as the “splitting marker.” For example, the splitting marker for the + operator is the addition sign (+), and the splitting marker for the & operator is the ampersand (&). The string will be split at the first occurrence of the splitting marker, and the remaining parts of the string will be inserted after the location of the splitting marker. For example, consider the following code: var kim = “Kim” + ” Ritman” The string “Kim Ritman” is first split at the plus sign, and the remaining parts of the string will be inserted after the Kim variable. Therefore, the Kim variable will be updated with “Kim Ritman.”
Hidden pitfall: There are special characters used for code snippets
Special characters are used for code snippets in JavaScript. This means that when the code snippet is executed, the special characters used to denote the code snippet will be removed from the source code and won’t be visible in the final product. You shouldn’t worry about this when translating your code, but you should ensure that the translator is aware of this. The translator must check that the source code has no special characters that the JavaScript interpreter might remove as a result of executing the code snippet. This can lead to wrong or missing translations as the translator might interpret the source code as literal text. The translator might interpret the code snippet as a literal string, and the special characters will be removed from the source code as a result.
Tip: Check your translated code by running it in an interpreter or debugger
To ensure that your translated code is correct, you can use various debugging tools to test the code and see that it executes as expected. However, you need to ensure that you run your translated code, not the source code. Otherwise, all identifiers will be replaced with the source code’s literal values, and you won’t see your translation results. There are many tools you can use to debug your translated code. Visual Studio, Eclipse, Sublime Text, and Visual Studio Code are the most popular debuggers. These tools come with integrated debuggers designed especially for debugging translated code. If you can’t use one of these integrated debuggers, you can still debug translated code and make sure your translated code is correct. You need to rename your translation files to include the language name and ensure your code uses the language variable to select the valid translation file. For example, if you translate your code to Spanish and name your translation file es.js, you can add the following code before running your code to select the correct translation.
Tip: Don’t rely 100% on automated tools when translating JavaScript code. Manual review is still necessary.
Although automated translation tools can usually be helpful for specific content areas, this doesn’t mean you can entirely rely on them for code translation. Code is generally written in a way that makes it difficult for translation tools to understand. Therefore, code translation must be done manually by a technical translator who understands the code. It’s best to use automated translation tools as guidance rather than as a final result. Use them to give you a general idea of the content, and then do a manual review to clean up the text. This way, you can ensure that the code is translated accurately.
Tip: Be careful with strings containing function definitions and declarations, as they will be replaced with the final values at runtime.
Function definitions and declarations are unique strings that are inserted in the code. When a program runs, these definitions and declarations are replaced with the real content so the program can execute properly. Therefore, these strings should never be marked as translatable or included in your translation file. Instead, it would be best to write them in English so translators can find them during the initial review.
Conclusion
There are a lot of potential pitfalls that you need to look out for when translating JavaScript code. Automated translation tools are not always reliable for translating JavaScript code. You should, therefore, not rely 100% on them and do a manual review. Be careful with strings containing function definitions and declarations, as they will be replaced with the final values at runtime. With these tips in mind, you’ll have a much smoother ride when translating JavaScript code.