Giter VIP home page Giter VIP logo

localizer's Introduction

What is this?

This small tool reads your property files and generate Java classes that enables type-safe access to message resources.

For example, when you have a property file called org/acme/Messages.properties that looks like this:

foo=error at {0} with {1}

This tool generates the following org/acme/Messages.java:

public class Messages {  
     
   private final static ResourceBundleHolder holder = new ResourceBundleHolder(Messages.class);
   
  /**  
    * error at {0} with {1}  
    */  
  public static String foo(Object arg1, Object arg2) {  
      return holder.format("foo",arg1,arg2);  
  }  
     
   /**  
     * error at {0} with {1}  
     */  
  public static Localizable _foo(Object arg1, Object arg2) {  
      return new Localizable(holder, "foo", arg1, arg2);  
  }  
}

The first method formats the message by using the default locale, and the second method returns an object that can be later formatted into String by specifying Locale.

In this way, you can get auto-completion on choosing the right message, you'll never refer to a non-existent message, and you'll always use the right number of arguments.

How to use this?

Maven

For projects built with Maven, add the following entries to your POM.

For the list of configurations to the localizer-maven-plugin, refer to this document:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.jvnet.localizer</groupId>
            <artifactId>localizer-maven-plugin</artifactId>
            <version>1.28</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.jvnet.localizer</groupId>
        <artifactId>localizer</artifactId>
        <version>1.28</version>
    </dependency>
    ...
</dependencies>
<repositories>
    <repository>
        <id>jenkins-repo</id>
        <url>https://repo.jenkins-ci.org/releases/</url>
    </repository>
    ...
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>jenkins-repo</id>
        <url>https://repo.jenkins-ci.org/releases/</url>
    </pluginRepository>
</pluginRepositories>

Ant

For projects built with Ant, use the following task to generate source files:

<taskdef name="localizer-gen" classname="org.jvnet.localizer.GeneratorTask">
    <classpath>
        <pathelement location="path/to/localizer-maven-plugin.jar"/>
        <pathelement location="path/to/localizer.jar"/>
    </classpath>
</taskdef>
<localizer-gen todir="build/geenrated-sources" dir="./resources">
    <include name="**/Messages.properties"/>
</localizer-gen>

The localizer-gen task is a matching task, so you can use the usual FileSet-based filtering technique to specify the property files to be processed.

Using LocaleProvider

When you use methods that return String, the implementation consults a singleton LocaleProvider for determining the locale to be used.

The default implementation simply returns Locale.getDefault(), but in other situations (for example in web apps), you can have this method return different locales (for example by using ServletRequest.getLocale().)

localizer's People

Contributors

batmat avatar daniel-beck avatar dependabot[bot] avatar ikedam avatar imod avatar jtnord avatar kohsuke avatar kreyssel avatar stephenc avatar tan9 avatar ttozser avatar vlatombe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

localizer's Issues

generated code should suppress SpotBugs/compiler warnings

Generated code should suppress SpotBugs & compiler warnings by generating the code with @edu.umd.cs.findbugs.annotations.SuppressFBWarnings and SuppressWarnings("all")

The reason to add these suppressions is so that when the code is generated your compilation results will not warn about unescessary warnings (e.g. missing javadoc for generated code, incorrect naming practices for method names starting with a captal) so you can focus on errors in your non auto generated code.

maven-localizer-plugin is not thread safe

When running the plugin with maven 3+ and the threaded option -T you will see warnings that the plugin is not thread safe.

Code inspection shows that whilst the majority of the plugin is thread-safe the libraries that it uses have known threading issues.

the plugin should be updated to use libraries without threading issues and be marked as @threadsafe

Allow overriding the classloader from which resources are loaded

I suspect

URL res = owner.getResource(owner.getSimpleName()+(s.length()>0?'_'+s:"")+".properties");
is the main reason Jenkins plugins such as https://github.com/jenkinsci/localization-zh-cn-plugin cannot contribute localizations for Messages classes.

It would be useful to be able to customize from which classloader localized resources are loaded.

CC @LinuxSuRen

Mojibake in Javadoc comments

As of jenkinsci/jenkins#6719 (I think), some Javadoc comments in generated Messages.java files are garbled. For example, https://javadoc.jenkins.io/hudson/model/Messages.html#_Queue_AllNodesOffline(java.lang.Object)

Key Queue.AllNodesOffline: All nodes of label �{0}� are offline.

In core/target/generated-sources/localizer/hudson/model/Messages.java:

    /**
     * Key {@code Queue.AllNodesOffline}: {@code All nodes of label �{0}�
     * are offline}.
     * 
     * @param arg0
     *      1st format parameter, {@code {0}}, as {@link String#valueOf(Object)}.
     * @return
     *     {@code All nodes of label �{0}� are offline}
     */

Source https://github.com/jenkinsci/jenkins/blob/eb96b8e07bcf2da498411ede238d24a18ec075c3/core/src/main/resources/hudson/model/Messages.properties#L202

Queue.AllNodesOffline=All nodes of label ‘{0}’ are offline

If I understand https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Properties.html#load(java.io.InputStream) vs. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PropertyResourceBundle.html right,

ResourceBundleImpl(InputStream stream) throws IOException {
super(stream);
prefers UTF-8 at runtime on Java 11, whereas
Properties props = new Properties();
FileInputStream in = new FileInputStream(propertyFile);
try {
if (propertyFile.getName().endsWith(".xml")) {
props.loadFromXML(in);
} else {
props.load(in);
uses ISO-8859-1.

plugin should not dependes on artifacts that requires different repository than maven central

This plugin which intent seems to provide internationalization should not force depends on artifacts that requires define jenkinsci repositories.
For example our company as policy allow only maven central and jboss (redhat repositories). In our project we are trying to use this plugin but it fails because of org.jenkins-ci:annotation-indexer:1.4 that is published on maven central but it requires a parent pom of jenkins organisation.

"Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.jenkins-ci:jenkins:pom:1.26 in ...)"

Since seems quite impossible change all the chain to be published on maven central I see two possible approaches to make this project really independent:

  1. shade the access-modifier-annotation and its dependencies (embed works only in OSGi), in this manner you could use latest version
  2. apply a flatten-maven-plugin on https://github.com/jenkinsci/lib-annotation-indexer project

Plugin mixes multiple Maven versions and does not declare Maven artifacts in `provided` scope

Reproduction steps

  • Use Maven 3.9.2.
  • Run mvn clean verify in e.g. a Jenkins plugin that uses localizer-maven-plugin.

Expected Results

No warning

Actual Results

[WARNING]  * org.jvnet.localizer:localizer-maven-plugin:1.31
[…]
[WARNING]   Plugin issue(s):
[WARNING]    * Plugin mixes multiple Maven versions: [3.6.3, 3.0]
[WARNING]    * Plugin should declare these Maven artifacts in `provided` scope: [org.apache.maven:maven-settings-builder:3.0, org.apache.maven:maven-repository-metadata:3.0, org.apache.maven:maven-plugin-api:3.6.3, org.apache.maven:maven-settings:3.0, org.apache.maven:maven-aether-provider:3.0, org.apache.maven:maven-artifact:3.6.3, org.apache.maven:maven-core:3.0, org.apache.maven:maven-model-builder:3.0, org.apache.maven:maven-model:3.6.3]

Anything else?

See https://maven.apache.org/docs/3.9.2/release-notes.html#notable-new-features for the root cause.

Publish new localizer to Maven central repository?

Dear Kohsuke,

Can you publish this artifact to maven central repository?
Current version in maven central is really old (1.12 released in 2010).

This can ease to effort for developers who using localizer (no extra repository, pluginRepository settings in pom.xml).
And promote others to use this fancy tool!

Publish to JCenter

The Gradle JPI plugin depends on localizer. Gradle plugins published through the Gradle Plugin Portal must should/must have their dependencies available on JCenter. The latest version on JCenter is 1.19, but I need 1.20 or later to get the JavaDoc fixes for Java 8.

Using dependencies outside of JCenter forces a verbose Gradle build file syntax:

buildscript {
    repositories {
        jcenter()
        maven {
            url('https://repo.jenkins-ci.org/releases/')
        }
    }
    dependencies {
        classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.18.1'
    }
}

apply plugin: 'org.jenkins-ci.jpi'

instead of just

plugins {
    id 'org.jenkins-ci.jpi' version '0.18.1'
}

In order to publish to JCenter, the artifacts must be upload to Bintray from where they can be promoted to JCenter, see https://bintray.com/docs/usermanual/uploads/uploads_promotingyourmaterial.html.

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.