Supporting different languages

edited November 2013 in Android Mode

I read something about supporting different languages on Android, although it requires xml files and work with IDs instead of Strings. Do you think is still possible with Processing?

Tagged:

Answers

  • Probably yes, the way described in the article (I haven't read it) or another way... Processing is Java, so i18n in Processing should work the same than in Java.

    A lot time ago, I made a sketch using Java i18n capabilities (more or less, I don't recall the details). See if some ideas can be useful to you: http://bazaar.launchpad.net/~philho/+junk/Processing/files/head:/_SmallPrograms/DisplayText/

  • Thanks PhiLho. Seems a bit more difficult than xml/ids way. I'll check it out

  • edited December 2013

    Hello @calsign, I've read you this in an answer, but I might have taken without the context,

    ...Open the sketch folder. Create a folder called res. Inside this folder, create a folder called values. Inside this folder, create a text (XML) file called strings.xml. The XML file's content should look something like this (you can change where it says Default Text to whatever you want):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="text_field_default_text">Default Text</string>
    </resources>
    

    You have created a native-Android resource. To access this from within the application, use the following (it is vital that the text text_field_default_text remain the same in both the XML file and in the sketch)...

    So would it be possible to use an xml file, and call the string with R.strings.text... so that have different string.xml inside different "values" languages folders?

  • Indeed, it should be possible, although Processing might mess around with something that allows it to work. In that particular post, the OP was unable to solve their problem with that method... but that may be for an entirely unrelated reason. I am able to successfully support multiple languages while developing native Android apps... I have not tried a Processing sketch yet. In theory, it should be the same approach.

    Please attempt to do this using the approach as explained in the tutorial you linked to and in my quoted comment above. If you encounter any errors, then I can help you through the process...

    It may be the case that this is, in fact, impossible... but exporting the sketch and moving to Eclipse would solve the problem (and create other problems). Perhaps I can work on submitting a pull request to the Processing source that would solve the issue if it should arise. Maybe a library would be more user-friendly...

  • edited December 2013

    got it, I'll try it soon and comment here, first in Processing, else in Eclipse. thanks in advance for your support calsign!

  • edited December 2013

    At first I've tried this,

    in res/values/string.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="title">My Application</string>
        <string name="hello_world">Hello World!</string>
    </resources>
    

    and the .pde file

    import android.R.*;
    
    void setup(){
      size(displayWidth, displayHeight);
    }
    
    void draw(){
      background(0);
      stroke(255);
    
      text(R.string.title, 10, 10);
      text(R.string.hello_world, 10, 25);
    }
    

    It printed:

    2130968576

    2130968577

    which seems to be some constant values as I've read here, but I'll keep searching and trying.

  • edited December 2013 Answer ✓

    Yes, this is expected behavior... because the R constants are all int values that are arbitrarily generated (either at run time or at compile time, I'm not entirely sure). You must pass these values to a native function that is capable of retrieving the String with the given ID. Try this:

    text(getResources().getString(R.string.title), 10, 10);
    text(getResources().getString(R.string.hello_world), 10, 25);
    

    ...where getResources().getString(int id) retrieves the String with the given resource ID.

    Also, some native Android functions are capable of taking these int values for convenience as a way of simplifying things. text() is not designed to work this way and is, in fact, designed to take int values and display them as a String in a similar fashion to println(). This may be what led to your confusion.

  • edited December 2013

    you're right!! it worked. this is great.

Sign In or Register to comment.