GithubHelp home page GithubHelp logo

bhanditz / sofia Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evanchooly/sofia

0.0 2.0 0.0 228 KB

A typesafe generated API on top of l10n/i18n resource bundles

Kotlin 58.69% Shell 2.02% FreeMarker 32.50% Java 6.80%

sofia's Introduction

Sofia

Sofia is a typesafe(ish) layer on top of a properties file. Given a properties file, it will generate a java class providing compile time checks that you’re using values that actually exist in your properties file. It supports multiple locales if you provide them.

A simple example looks like this. For this properties file:

test.property=I'm the first test property
parameterized.property.long.name=I need parameters {0} and {1}
new.property=New Property

@error.date.property=Today''s date {0,date,full} and now a number {1,number}

@error.another=I'm an error
@warn.me=I'm just a warning, though.

lonely=I'm only in the default bundle.

a java file will be generated with the following interface:

public class com.antwerkz.sofia.Sofia {
  public static java.lang.String another(java.util.Locale...);
  public static void logAnother(java.util.Locale...);
  public static java.lang.String dateProperty(java.util.Date, java.lang.Number, java.util.Locale...);
  public static void logDateProperty(java.util.Date, java.lang.Number, java.util.Locale...);
  public static java.lang.String me(java.util.Locale...);
  public static void logMe(java.util.Locale...);
  public static java.lang.String lonely(java.util.Locale...);
  public static java.lang.String newProperty(java.util.Locale...);
  public static java.lang.String parameterizedPropertyLongName(java.lang.Object, java.lang.Object, java.util.Locale...);
  public static java.lang.String testProperty(java.util.Locale...);
}

There are some other details but they’re mostly irrelevant. Feel free to explore the generated file if you’re really interested. What you see here is one method for each property listed in your properties file. Those properties that take parameters result in methods that take parameters as well. Notice that each parameter is typed according to the formatting specified in the properties file. If no formatting is defined, "Object" is used for that parameter type.

Also note that each method takes a Locale…​ parameter. This allows sofia to generate one method per property while supporting an optional Locale to be supplied without having to generate two identical methods or having to pass in a null to a "singleton" parameter. Multiple Locales can be passed in but only one will be used.

So …​ why?

When I worked on glassfish, we’d standardized on a library that did something similar. My biggest complaint about that one is that all the generated methods WERE IN ALL CAPS WHICH MADE READING THE CODE AWKWARD. Also, I’m not really sure where the source for that library is so patching it would be tricky. In any case, it turned out to be a very handy facet to a project which makes use of properties files to manage its messages so I wanted to improve on it where I could.

Great. How do I use it?

Add the following to your pom.xml:

<plugin>
  <groupId>com.antwerkz.sofia</groupId>
  <artifactId>maven</artifactId>
  <version>0.13</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
</plugin>

There are a few configuration options:

Value Description Default

inputFile

The base properties file to use

src/main/resources/sofia.properties

outputDirectory

Generated source location

${project.build.directory}/generated-sources/sofia

loggingType

Use a specific logger (supported: none, jul, slf4j)

slf4j

packageName

Defines the package for the generated class

com.antwerkz.sofia

charset

Change the character coding used when reading property files

ISO-8859-1

If you’d like continuous monitoring and regeneration of your files, there’s also "sofia:watch." Using this target, sofia will watch for changes to your input file and regenerate as needed.

sofia's People

Contributors

evanchooly avatar fge avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.