Skip to content

Commit

Permalink
MonRG: Generate base resources into <base>.properties, not <base>_<ba…
Browse files Browse the repository at this point in the history
…seLocale>.properties, so other locales can inherit them by default.

[git-p4: depot-paths = "//open/mondrian/": change = 216]
  • Loading branch information
julianhyde committed Oct 26, 2002
1 parent e0f0071 commit f00cada
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 51 deletions.
2 changes: 1 addition & 1 deletion build.xml
Expand Up @@ -200,7 +200,7 @@ copy.properties"/>
<classpath refid="project.classpath"/>
</taskdef>

<resgen srcdir="${java.dir}">
<resgen srcdir="${java.dir}" locales="en_US,de_DE">
<include name="mondrian/olap/MondrianResource.xml"/>
</resgen>

Expand Down
2 changes: 1 addition & 1 deletion demo/monrg/build.xml
Expand Up @@ -16,7 +16,7 @@
</taskdef>

<target name="generate.resources">
<resgen srcdir="source">
<resgen srcdir="source" locales="en_US,fr_FR">
<include name="happy/BirthdayResource.xml"/>
<include name="happy/BirthdayResource_fr_FR.properties"/>
</resgen>
Expand Down
26 changes: 18 additions & 8 deletions doc/monrg.html
Expand Up @@ -60,7 +60,7 @@ <h3>Create ANT target</h3>
&lt;/taskdef&gt;

&lt;target name=&quot;generate.resources&quot;&gt;
&lt;resgen srcdir=&quot;source&quot;&gt;
&lt;resgen srcdir=&quot;source&quot; locales=&quot;en_US&quot;&gt;
&lt;include name=&quot;happy/BirthdayResource_en_US.xml&quot;/&gt;
&lt;/resgen&gt;
&lt;/target&gt;
Expand Down Expand Up @@ -99,7 +99,7 @@ <h3>Compile</h3>

Total time: 3 seconds</pre>
</blockquote>
<p>Three files are generated.</p>
<p>Four files are generated.</p>
<p><code>source/happy/BirthdayResource.java</code>:</p>
<blockquote>
<pre>package happy;
Expand Down Expand Up @@ -157,11 +157,17 @@ <h3>Compile</h3>
public BirthdayResource_en_US() throws IOException {}
}</pre>
</blockquote>
<p><code>source/happy/BirthdayResource_en_US.properties</code>:</p>
<p><code>source/happy/BirthdayResource.properties</code>:</p>
<blockquote>
<pre>HappyBirthday=Happy Birthday, {0}! You don't look {1,number}.
TooYoung={0} has not been born yet.</pre>
</blockquote>
<p><code>source/happy/BirthdayResource_en_US.properties</code>:</p>
<blockquote>
<pre># This file is intentionally blank. Add property values
# to this file to override the translations in the base
# properties file, BirthdayResource.properties.</pre>
</blockquote>
<p>For each resource, a <code>get</code><code><i>Xxx</i>()</code> method is
generated to retrieve that resource in the current locale, substituting
parameters appropriately. For exception resources, an additional two <code>new<i>Xxx</i>()</code>
Expand Down Expand Up @@ -267,30 +273,32 @@ <h2>Translation</h2>
<tr>
<th>Generated files</th>
<td valign="top">happy/BirthdayResource.java<br>
happy/BirthdayResource.properties<br>
happy/BirthdayResource_en_US.java<br>
happy/BirthdayResource_en_US.properties</td>
</tr>
<tr>
<th>Runtime files</th>
<td valign="top">happy/BirthdayResource.class<br>
happy/BirthdayResource.properties<br>
happy/BirthdayResource_en_US.class<br>
happy/BirthdayResource_en_US.properties</td>
</tr>
</table>
</blockquote>
<p>Do the following steps:</p>
<ol>
<li>Copy <code>happy/BirthdayResource_en_US.properties</code>&nbsp; to <code>happy/BirthdayResource_fr_FR.properties</code>,
<li>Copy <code>happy/BirthdayResource.properties</code>&nbsp; to <code>happy/BirthdayResource_fr_FR.properties</code>,
and translate the messages as appropriate for the new locale:<blockquote>
<pre>HappyBirthday=Bon anniversaire, {0}! {1,number}, c'est un bon age.
TooYoung={0} n'est pas encore n&eacute;(e).</pre>
</blockquote>
<p>&nbsp;</li>
<li>Add <code>happy/BirthdayResource_fr_FR.properties</code> to the ANT task:<blockquote>
<pre>&lt;target name=&quot;generate.resources&quot;&gt;
&lt;resgen srcdir=&quot;source&quot;&gt;
&lt;resgen srcdir=&quot;source&quot; <font color="#FF0000"><i>locales=&quot;en_US,fr_FR&quot;</i></font>&gt;
&lt;include name=&quot;happy/BirthdayResource_en_US.xml&quot;/&gt;
&lt;include name=&quot;happy/BirthdayResource_fr_FR.properties&quot;/&gt;
<font color="#FF0000"><i>&lt;include name=&quot;happy/BirthdayResource_fr_FR.properties&quot;/&gt;</i></font>
&lt;/resgen&gt;
&lt;/target&gt;</pre>
</blockquote></li>
Expand All @@ -312,13 +320,15 @@ <h2>Translation</h2>
<tr>
<th>Generated files</th>
<td dir="ltr" valign="top">happy/BirthdayResource.java<br>
happy/BirthdayResource.properties<br>
happy/BirthdayResource_en_US.java<br>
happy/BirthdayResource_en_US.properties<br>
<i>happy/BirthdayResource_fr_FR.java</i></td>
</tr>
<tr>
<th>Runtime files</th>
<td dir="ltr" valign="top">happy/BirthdayResource.class<br>
happy/BirthdayResource.properties<br>
happy/BirthdayResource_en_US.class<br>
happy/BirthdayResource_en_US.properties<br>
<i>happy/BirthdayResource_fr_FR.class<br>
Expand Down Expand Up @@ -428,7 +438,7 @@ <h2>About MonRG and Mondrian</h2>
<tr>
<td class="content">
<a target="_top" href="index.html">Home</a> |
<a target="_top" href="noframes.html">No frames</a> | This file is<i>
<a target="_top" href="monrg.html">No frames</a> | This file is<i>
<a href="http://apoptosis.dyndns.org:8080/open/mondrian/doc/resgen.html">$Id$
</a></i> (<a href="http://apoptosis.dyndns.org:8080/open/mondrian/doc/resgen.html?ac=22">log</a>)</td>
<td align="right">
Expand All @@ -440,4 +450,4 @@ <h2>About MonRG and Mondrian</h2>
</table>

</body>
</html>
</html>
88 changes: 64 additions & 24 deletions src/main/mondrian/resource/ResourceGen.java
Expand Up @@ -42,6 +42,8 @@
* function corresponding to each error message in
* <code>MyResource_en.xml</code>.</p>
*
* <p>See also the ANT Task, {@link ResourceGenTask}.</p>
*
* @author jhyde
* @since 3 December, 2001
* @version $Id$
Expand Down Expand Up @@ -265,7 +267,6 @@ static abstract class FileTask {
ResourceGenTask.Include include;
String className;
String fileName;
Locale locale;

abstract void process(ResourceGen generator) throws IOException;
abstract void generateBaseJava(
Expand Down Expand Up @@ -404,7 +405,6 @@ class XmlFileTask extends ResourceGen.FileTask {
baseClassName = "mondrian.resource.ShadowResourceBundle";
}
this.baseClassName = baseClassName;
this.locale = Util.fileNameToLocale(fileName, ".xml");
}
void process(ResourceGen generator) throws IOException {
URL url = Util.convertPathToURL(getFile());
Expand All @@ -413,19 +413,34 @@ void process(ResourceGen generator) throws IOException {
throw new BuildException(
"Resource file " + url + " must have locale");
}
this.locale = Util.parseLocale(resourceList.locale);
if (this.locale == null) {
ArrayList localeNames = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(include.root.locales,",");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
localeNames.add(token);
}
if (!localeNames.contains(resourceList.locale)) {
throw new BuildException(
"Invalid locale " + resourceList.locale);
"Resource file " + url + " has locale '" +
resourceList.locale +
"' which is not in the 'locales' list");
}
Locale[] locales = new Locale[localeNames.size()];
for (int i = 0; i < locales.length; i++) {
String localeName = (String) localeNames.get(i);
locales[i] = Util.parseLocale(localeName);
if (locales[i] == null) {
throw new BuildException(
"Invalid locale " + localeName);
}
}
generateJava(generator, resourceList, null);
if (locale != null) {
generateProperties(generator, resourceList, null);
for (int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
generateJava(generator, resourceList, locale);
generateProperties(generator, resourceList, locale);
}
if (locale == null) {
locale = Locale.getDefault();
}
generateProperties(generator, resourceList, locale);
}

/**
Expand Down Expand Up @@ -625,30 +640,28 @@ private void generateProperties(
}
PrintWriter pw = new PrintWriter(out);
try {
generateProperties(resourceList, pw, locale);
if (locale == null) {
generateBaseProperties(resourceList, pw);
} else {
generateProperties(resourceList, pw, locale);
}
} finally {
pw.close();
}
}
/**
* Generates a class containing a line for each resource.
*
* @pre locale != null
* Generates a properties file containing a line for each resource.
*/
private void generateProperties(
ResourceDef.ResourceBundle resourceList, PrintWriter pw,
Locale locale) {
String fullClassName = getClassName(locale);
private void generateBaseProperties(
ResourceDef.ResourceBundle resourceList, PrintWriter pw) {
String fullClassName = getClassName(null);
pw.println("# This file contains the resources for");
pw.println("# class '" + fullClassName + "' and locale '" + locale + "'.");
pw.println("# class '" + fullClassName + "'; the base locale is '" +
resourceList.locale + "'.");
pw.println("# It was generated by " + ResourceGen.class);
pw.println("# from " + getFile());
pw.println("# on " + new Date().toString() + ".");
pw.println();
String localeName = null;
if (locale != null) {
localeName = locale.toString();
}
for (int i = 0; i < resourceList.resources.length; i++) {
ResourceDef.Resource resource = resourceList.resources[i];
final String name = resource.name;
Expand All @@ -664,7 +677,32 @@ private void generateProperties(
}
pw.println("# End " + fullClassName + ".properties");
}
String getClassName(Locale locale) {

/**
* Generates an empty properties file named after the class and the given
* locale.
*
* @pre locale != null
*/
private void generateProperties(
ResourceDef.ResourceBundle resourceList, PrintWriter pw,
Locale locale) {
String fullClassName = getClassName(locale);
pw.println("# This file contains the resources for");
pw.println("# class '" + fullClassName + "' and locale '" + locale + "'.");
pw.println("# It was generated by " + ResourceGen.class);
pw.println("# from " + getFile());
pw.println("# on " + new Date().toString() + ".");
pw.println();
pw.println("# This file is intentionally blank. Add property values");
pw.println("# to this file to override the translations in the base");
String basePropertiesFileName = getClassNameSansPackage(locale) + ".properties";
pw.println("# properties file, " + basePropertiesFileName);
pw.println();
pw.println("# End " + fullClassName + ".properties");
}

private String getClassName(Locale locale) {
String s = className;
if (locale != null) {
s += '_' + locale.toString();
Expand All @@ -674,6 +712,8 @@ String getClassName(Locale locale) {
}

class PropertiesFileTask extends ResourceGen.FileTask {
Locale locale;

PropertiesFileTask(ResourceGenTask.Include include, String fileName) {
this.include = include;
this.fileName = fileName;
Expand Down
43 changes: 29 additions & 14 deletions src/main/mondrian/resource/ResourceGenTask.java
Expand Up @@ -20,29 +20,32 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.StringTokenizer;
import java.net.URL;

/**
* A <code>ResourceGenTask</code> is an ANT task to invoke the mondrian
* Resource Generator.
*
* Example:
* <ResourceGen srcdir="source" locale="en_US">
* <include name="happy/BirthdayResource.xml"/>
* </ResourceGen>
* <p>Example:<blockquote>
*
* Generates
* source/happy/BirthdayResource_en_US.properties
* source/happy/BirthdayResource.java
* source/happy/BirthdayResource_en_US.java
* <pre>&lt;resgen srcdir="source" locales="en_US"&gt;
* &lt;include name="happy/BirthdayResource.xml"/&gt;
*&lt;/resgen&gt;</pre>
*
* Files are not generated if there is an existing newer one.
* </blockquote>generates<blockquote>
*
* <pre>source/happy/BirthdayResource.properties
*source/happy/BirthdayResource_en_US.properties
*source/happy/BirthdayResource.java
*source/happy/BirthdayResource_en_US.java</pre>
*
* </blockquote>Files are not generated if there is an existing newer one.
* The output path is determined by 'dest' and the package-name.
*
* <h2>Element &lt;resourceGen&gt;</h2>
*
* <table>
* <table border="2">
* <tr>
* <th>Attribute</th>
* <th>Description</th>
Expand All @@ -66,6 +69,13 @@
* </tr>
*
* <tr>
* <td><a name="locales">locales</a></td>
* <td>Comma-separated list of locales to generate <code>.properties</code>
* and <code>.java</code> files for.</td>
* <td>No</td>
* </tr>
*
* <tr>
* <td><a name="static">static</a></td>
* <td>Whether to generate static or dynamic accessor methods. Default is
* true (generate static methods). Not yet implemented.</td>
Expand All @@ -74,7 +84,7 @@
*
* </table>
*
* Nested element: &lt;{@link ResourceGen.Include include}&gt;.
* Nested element: &lt;{@link Include include}&gt;.
*
* @author jhyde
* @since Oct 8, 2002
Expand All @@ -85,6 +95,7 @@ public class ResourceGenTask extends Task {
File src;
File dest;
boolean statik = true;
String locales;

public ResourceGenTask() {
}
Expand Down Expand Up @@ -133,12 +144,16 @@ public void setStatic(boolean statik) throws BuildException {
throw new BuildException(
"The 'static' parameter is not implemented yet");
}
/** Sets <a href="#locales">locales</a>. **/
public void setLocales(String locales) throws BuildException {
this.locales = locales;
}

/**
* <code>Include</code> implements &lt;include&gt; element nested
* within a &lt;resgen&gt; task (see {@link ResourceGenTask}).
*
* <table>
* <table border="2">
* <tr>
* <th>Attribute</th>
* <th>Description</th>
Expand All @@ -147,8 +162,8 @@ public void setStatic(boolean statik) throws BuildException {
*
* <tr>
* <td><a name="name">name</a></td>
* <td>The name, relative to <a href="#srcdir">, of the XML file which
* defines the resources.</td>
* <td>The name, relative to <a href="#srcdir">srcdir</a>, of the XML file
* which defines the resources.</td>
* <td>Yes</td>
* </tr>
*
Expand Down

0 comments on commit f00cada

Please sign in to comment.