TextΒΆ

The Text utility class helps your code access GeoTools text facilities.:

        InternationalString greeting;

        // simple text place holder
        greeting = Text.text("hello world");

        // translated text for an internationalized application
        // useful as message.properties, message_fr.properties etc can be translated
        // outside of your application
        greeting = Text.text("greeting", "message.properties");

        // the next method is good for quickly doing things as a developer
        Map<String, String> translations = new HashMap<>();
        translations.put("greeting", "Hello World");
        translations.put("greeting_it", "ciao mondo");

        greeting = Text.text("greeting", translations);

        // you can actually use the same map to configure several international strings
        // (each string will only pick up the entries with a matching key)
        //
        // With that in mind we make a special effort to allow you to use properties
        Properties properties = new Properties();
        properties.load(new FileInputStream("message.properties"));

        InternationalString title = Text.text("title", properties);
        InternationalString description = Text.text("description", properties);

The idea of an InternationalString comes from the gt-api module.

The above class is backed by a couple of implementations provided by gt-metadata:

  • ResourceInternationalString

    Used to set up an InternationalString based on a resource bundle.

            ResourceInternationalString greeting =
                    new ResourceInternationalString("message.properties", "greeting");
    
            System.out.println(greeting); // will output best match to current Locale
            System.out.println(greeting.toString(Locale.CANADA_FRENCH)); // should output best match
    
            // Note the "messages.properties" above is used with Java ResourceBundle
            // In a manner similar to this
            ResourceBundle.getBundle("message.properties", Locale.getDefault()).getString("greeting");
    

    As noted above Java ResourceBundle is responsible for loading the correct property file for the requested String.

  • SimpleInternationalString

    This is used when you need to quickly get back to work. You can replace the default English text with a translated string when a volunteer shows interest.

            SimpleInternationalString greeting = new SimpleInternationalString("Hello World");
    
            System.out.println(greeting); // will output best match to current
            System.out.println(greeting.toString(Locale.FRENCH)); // but since there is only one...
    
  • GrowableInternaionalString

    The easiest one to use as a programmer; allows you to set up multiple translations in code.

            GrowableInternationalString greeting = new GrowableInternationalString();
    
            // Make use of Locale when adding translations
            greeting.add(Locale.ENGLISH, "Hello World");
            greeting.add(Locale.FRENCH, "Bonjour tout le monde");
            greeting.add("fr", "greeting", "");
    
            System.out.println(greeting); // will output best match to current Locale
            System.out.println(greeting.toString(Locale.CANADA_FRENCH)); // should output best match
    
            // You can also use strings to avoid creating Locale objects right away
            // The method references a "key" which is handy when processing property files
            greeting.add("key_en", "key", "Hello World");
            greeting.add("key_fr", "key", "Bonjour tot le monde");
    
            // You can also quickly create an GrowableInternationalString and add to it later
            GrowableInternationalString cheerfulGreeting = new GrowableInternationalString("G'Day");
            cheerfulGreeting.add(Locale.UK, "Welcome");
            cheerfulGreeting.add(Locale.US, "Hello");