How do you manage the String Translation Process?

后端 未结 11 1942
小鲜肉
小鲜肉 2020-12-30 23:37

I am working on a Software Project that needs to be translated into 30 languages. This means that changing any string incurs into a relatively high cost. Additionally, tra

11条回答
  •  借酒劲吻你
    2020-12-30 23:45

    There are a number of major issues that need to be considered when internationalizing an application.

    • Not all strings are created equally. Depending upon the language, the length of a sentence can change significantly. In some languages, it can be half as long and in others it can be triple the length. Make sure to design your GUI widgets with enough space to handle strings that are larger than your English strings.
    • Translators are typically not programmers. Do not expect the translators to be able to read and maintain the correct file formats for resource files. You should setup a mechanism where you can transform the translated data round trip to your resource files from something like an spreadsheet. One possibility is to use XSL filters with Open Office, so that you can save to Resource files directly in a spreadsheet application. Also, translators or translation service companies may already have their own databases, so it is good to ask about what they use and write some tools to automate.
    • You will need to append data to strings - don't pretend that you will never have to or you will always be able to put the string at the end. Make sure that you have a string formatter setup for replacing placeholders in strings. Furthermore, make sure to document what are typical values that will be replaced for the translators. Remember, the order of the placeholders may change in different languages.
    • Name your i8n string variables something that reflects their meaning. Do you really want to be looking up numbers in a resource file to find out what is the contents of a given string. Developers depend on being able to read the string output in code for efficiency a lot more than they often realize.
    • Don't be afraid of code-generation. In my current project, I have written a small Java program that is called by ant that parses all of the keys of the default language (master) resource file and then maps the key to a constant defined in my localization class. See below. The lines in between the //---- comments is auto-generated. I run the generator every time I add a string.

    
    public final class l7d {
    ...normal junk

    /** * Reference to the localized strings resource bundle. */ public static final ResourceBundle l7dBundle = ResourceBundle.getBundle(BUNDLE_PATH);

    //---- start l7d fields ----\ public static final String ERROR_AuthenticationException; public static final String ERROR_cannot_find_algorithm; public static final String ERROR_invalid_context; ...many more //---- end l7d fields ----\ static {
    //---- start setting l7d fields ----\ ERROR_AuthenticationException = l7dBundle.getString("ERROR_AuthenticationException"); ERROR_cannot_find_algorithm = l7dBundle.getString("ERROR_cannot_find_algorithm"); ERROR_invalid_context = l7dBundle.getString("ERROR_invalid_context"); ...many more //---- end setting l7d fields ----\ }

    The approach above offers a few benefits.

    1. Since your string key is now defined as a field, your IDE should support code completion for it. This will save you a lot of type. It get's really frustrating looking up every key name and fixing typos every time you want to print a string.
    2. Someone please correct me if I am wrong. By loading all of the strings into memory at static instantiation (as in the example) will result in a quicker load time at the cost of additional memory usage. I have found the additional amount of memory used is negligible and worth the trade off.

提交回复
热议问题