[pseudolocalization-tool] r9 committed - ...

11 views
Skip to first unread message

pseudolocal...@googlecode.com

unread,
Oct 11, 2011, 5:00:33 PM10/11/11
to pseudolocal...@googlegroups.com
Revision: 9
Author: pseudolocalization.mirrorbot
Date: Tue Oct 11 14:00:14 2011
Log:
Add a command-line tool for pseudolocalization-tool, support x- variant
tags.

R=staudacher
DELTA=794 (793 added, 0 deleted, 1 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3443

http://code.google.com/p/pseudolocalization-tool/source/detail?r=9

Added:
/trunk/java/com/google/i18n/pseudolocalization/format
/trunk/java/com/google/i18n/pseudolocalization/format/FormatRegistry.java
/trunk/java/com/google/i18n/pseudolocalization/format/JavaProperties.java
/trunk/java/com/google/i18n/pseudolocalization/format/MessageCatalog.java
/trunk/java/com/google/i18n/pseudolocalization/format/MessagePerFile.java

/trunk/java/com/google/i18n/pseudolocalization/format/ReadableMessageCatalog.java

/trunk/java/com/google/i18n/pseudolocalization/format/WritableMessageCatalog.java
/trunk/java/com/google/i18n/pseudolocalization/tool
/trunk/java/com/google/i18n/pseudolocalization/tool/Pseudolocalizer.java
Modified:

/trunk/java/com/google/i18n/pseudolocalization/PseudolocalizationPipeline.java

=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/format/FormatRegistry.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.i18n.pseudolocalization.format;
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Registry of known file formats.
+ */
+public class FormatRegistry {
+
+ private static Map<String, Class<? extends MessageCatalog>> registry;
+ private static final Object registryLock = new Object[0];
+
+ static {
+ synchronized(registryLock) {
+ registry = new HashMap<String, Class<? extends MessageCatalog>>();
+ registry.put("properties", JavaProperties.class);
+ }
+ }
+
+ /**
+ * Get a {@link MessageCatalog} for a given file extension. If nothing
+ * matches, a default is returned that treats the entire file contents
as a
+ * single message.
+ *
+ * @param extension
+ * @return a {@link MessageCatalog} instance to use to read/write a file
with
+ * the specified extension
+ * @throws RuntimeException if the chosen {@link MessageCatalog} could
not be
+ * instantiated
+ */
+ public static MessageCatalog getMessageCatalog(String extension) {
+ Class<? extends MessageCatalog> clazz;
+ synchronized (registryLock) {
+ clazz = registry.get(extension);
+ }
+ if (clazz == null) {
+ // if an unknown format, simply treat the entire file as one message
+ clazz = MessagePerFile.class;
+ }
+ try {
+ return clazz.newInstance();
+ } catch (InstantiationException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Register a new {@link MessageCatalog} class with one or more
extensions.
+ *
+ * @param clazz
+ * @param extensions one or more file extensions to register
+ */
+ public static void register(Class<? extends MessageCatalog> clazz,
+ String... extensions) {
+ synchronized(registryLock) {
+ for (String extension : extensions) {
+ registry.put(extension, clazz);
+ }
+ }
+ }
+
+ private FormatRegistry() {
+ }
+}
=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/format/JavaProperties.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.i18n.pseudolocalization.format;
+
+import com.google.i18n.pseudolocalization.message.Message;
+import com.google.i18n.pseudolocalization.message.MessageFragment;
+import com.google.i18n.pseudolocalization.message.SimpleMessage;
+import com.google.i18n.pseudolocalization.message.SimpleTextFragment;
+import com.google.i18n.pseudolocalization.message.impl.AbstractPlaceholder;
+import com.google.i18n.pseudolocalization.message.impl.IterableTransformer;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class JavaProperties implements MessageCatalog {
+
+ static class PropertiesMessage extends SimpleMessage {
+
+ private static List<MessageFragment> parseMessage(String text) {
+ List<MessageFragment> list = new ArrayList<MessageFragment>();
+ // TODO: handle quoting
+ Matcher m = MESSAGE_FORMAT_ARG.matcher(text);
+ int start = 0;
+ while (m.find()) {
+ String plainText = text.substring(start, m.start());
+ start = m.end();
+ if (plainText.length() > 0) {
+ list.add(new SimpleTextFragment(plainText));
+ }
+ list.add(new MessageFormatPlaceholder(m.group()));
+ }
+ String plainText = text.substring(start);
+ if (plainText.length() > 0) {
+ list.add(new SimpleTextFragment(plainText));
+ }
+ return list;
+ }
+
+ private final String key;
+
+ public PropertiesMessage(String key, String text) {
+ super(parseMessage(text));
+ this.key = key;
+ }
+
+ @Override
+ public String getId() {
+ return key;
+ }
+ }
+
+ private static class JavaPropertiesReader implements
ReadableMessageCatalog {
+
+ private final InputStream stream;
+ private Properties properties;
+
+ public JavaPropertiesReader(InputStream stream) {
+ this.stream = stream;
+ }
+
+ private void ensureProperties() throws IOException {
+ if (properties != null) {
+ return;
+ }
+ properties = new Properties();
+ try {
+ properties.load(stream);
+ } finally {
+ stream.close();
+ }
+ }
+
+ public void close() {
+ // do nothing
+ }
+
+ public Iterable<Message> readMessages() throws IOException {
+ ensureProperties();
+ return new IterableTransformer<String,
Message>(Collections.unmodifiableSet(
+ properties.stringPropertyNames())) {
+ @Override
+ protected Message transform(String val) {
+ return new PropertiesMessage(val, properties.getProperty(val));
+ }
+ };
+ }
+ }
+
+ private static class JavaPropertiesWriter implements
WritableMessageCatalog {
+
+ private final Properties properties = new Properties();
+ private final OutputStream stream;
+
+ JavaPropertiesWriter(OutputStream stream) {
+ this.stream = stream;
+ }
+
+ public void close() throws IOException {
+ properties.store(stream, "");
+ }
+
+ public void writeMessage(Message msg) {
+ properties.put(msg.getId(), ((PropertiesMessage) msg).getText());
+ }
+ }
+
+ private static class MessageFormatPlaceholder extends
AbstractPlaceholder {
+
+ private final String text;
+
+ public MessageFormatPlaceholder(String text) {
+ this.text = text;
+ }
+
+ @Override
+ public String getTextRepresentation() {
+ return text;
+ }
+ }
+
+ /**
+ * Matches {number}, {number,word}, {number,word,extra} as a quick hack
for
+ * MessageFormat-style placeholders. Note that this is an incomplete
solution
+ * since it doesn't handle quoting.
+ */
+ static final Pattern MESSAGE_FORMAT_ARG = Pattern.compile(
+ "\\{((\\d+)|#|(\\w+))(,\\w+(,[^\\}]+)?)?\\}");
+
+ public ReadableMessageCatalog readFrom(InputStream istr) {
+ return new JavaPropertiesReader(istr);
+ }
+
+ public WritableMessageCatalog writeTo(OutputStream ostr) {
+ return new JavaPropertiesWriter(ostr);
+ }
+}
=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/format/MessageCatalog.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.i18n.pseudolocalization.format;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Interface of a message catalog which is both readable and writable.
+ *
+ * NOTE: THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE.
+ */
+public interface MessageCatalog {
+
+ /**
+ * Read a message catalog from an input stream.
+ *
+ * @param istr
+ * @return a {@link ReadableMessageCatalog} instance
+ * @throws IOException
+ */
+ ReadableMessageCatalog readFrom(InputStream istr) throws IOException;
+
+ /**
+ * Write a message catalog to an output stream.
+ *
+ * @param ostr
+ * @return a {@link WritableMessageCatalog} instance
+ * @throws IOException
+ */
+ WritableMessageCatalog writeTo(OutputStream ostr) throws IOException;
+}
=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/format/MessagePerFile.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,83 @@
+package com.google.i18n.pseudolocalization.format;
+
+import com.google.i18n.pseudolocalization.message.DefaultVisitor;
+import com.google.i18n.pseudolocalization.message.Message;
+import
com.google.i18n.pseudolocalization.message.NonlocalizableTextFragment;
+import com.google.i18n.pseudolocalization.message.Placeholder;
+import com.google.i18n.pseudolocalization.message.SimpleMessage;
+import com.google.i18n.pseudolocalization.message.TextFragment;
+import com.google.i18n.pseudolocalization.message.VisitorContext;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A MessageCatalog that treats the file contents as a single message
encoded in
+ * UTF8.
+ */
+class MessagePerFile implements MessageCatalog {
+
+ private static final Charset UTF8 = Charset.forName("UTF-8");
+
+ private List<Message> messages = new ArrayList<Message>();
+
+ public ReadableMessageCatalog readFrom(InputStream stream) throws
IOException {
+ StringBuilder buf = new StringBuilder();
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(stream, UTF8));
+ int ch;
+ while ((ch = reader.read()) != -1) {
+ buf.append((char) ch);
+ }
+ reader.close();
+ List<Message> list = new ArrayList<Message>();
+ list.add(new SimpleMessage(buf.toString()));
+ messages = Collections.unmodifiableList(list);
+ return new ReadableMessageCatalog() {
+ public Iterable<Message> readMessages() {
+ return messages;
+ }
+
+ public void close() {
+ // do nothing
+ }
+ };
+ }
+
+ public WritableMessageCatalog writeTo(final OutputStream out) {
+ return new WritableMessageCatalog() {
+ public void writeMessage(Message msg) throws IOException {
+ // TODO(jat): extract this to a common place
+ final StringBuilder buf = new StringBuilder();
+ msg.accept(new DefaultVisitor() {
+ @Override
+ public void visitNonlocalizableTextFragment(VisitorContext ctx,
+ NonlocalizableTextFragment fragment) {
+ buf.append(fragment.getText());
+ }
+
+ @Override
+ public void visitPlaceholder(VisitorContext ctx, Placeholder
placeholder) {
+ buf.append(placeholder.getTextRepresentation());
+ }
+
+ @Override
+ public void visitTextFragment(VisitorContext ctx, TextFragment
fragment) {
+ buf.append(fragment.getText());
+ }
+ });
+ out.write(buf.toString().getBytes(UTF8));
+ }
+
+ public void close() throws IOException {
+ out.close();
+ }
+ };
+ }
+}
=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/format/ReadableMessageCatalog.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.i18n.pseudolocalization.format;
+
+import com.google.i18n.pseudolocalization.message.Message;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * A message catalog that allows reading structured messages.
+ */
+public interface ReadableMessageCatalog extends Closeable {
+
+ /**
+ * Must be called when this catalog is no longer needed.
+ */
+ void close() throws IOException;
+
+ /**
+ * Load messages from the catalog. This may involve reading the entire
+ * contents of the catalog immediately, or it may read them as requested
+ * from the iterator.
+ *
+ * @return a collection of {@link Message}s
+ * @throws IOException
+ */
+ Iterable<Message> readMessages() throws IOException;
+}
=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/format/WritableMessageCatalog.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.i18n.pseudolocalization.format;
+
+import com.google.i18n.pseudolocalization.message.Message;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * A message catalog that allows writing structured messages.
+ */
+public interface WritableMessageCatalog extends Closeable {
+
+ /**
+ * Must be called when all messages have been written.
+ */
+ void close() throws IOException;
+
+ /**
+ * Write a single message to the catalog.
+ *
+ * @param msg
+ * @throws IOException
+ */
+ void writeMessage(Message msg) throws IOException;
+}
=======================================
--- /dev/null
+++
/trunk/java/com/google/i18n/pseudolocalization/tool/Pseudolocalizer.java
Tue Oct 11 14:00:14 2011
@@ -0,0 +1,333 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.i18n.pseudolocalization.tool;
+
+import com.google.i18n.pseudolocalization.PseudolocalizationPipeline;
+import com.google.i18n.pseudolocalization.format.FormatRegistry;
+import com.google.i18n.pseudolocalization.format.MessageCatalog;
+import com.google.i18n.pseudolocalization.format.ReadableMessageCatalog;
+import com.google.i18n.pseudolocalization.format.WritableMessageCatalog;
+import com.google.i18n.pseudolocalization.message.Message;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Run a pseudolocalization pipeline on a set of input files.
+ * <p>
+ * See {@link PseudolocalizerArguments#printUsage()} for command-line
usage.
+ */
+public class Pseudolocalizer {
+
+ // @VisibleForTesting
+ static class PseudolocalizerArguments {
+
+ /**
+ * Print a usage message.
+ */
+ private static void printUsage() {
+ System.err.println("Usage: Pseudolocalizer [--ext=fqcn[,fqcn...]]
[--variant=varname|"
+ + "--method=method[,method...] [--type=filetype] [<--interactive|
files>]");
+ System.err.println("filetype: a registered file type, typically the
same as the extension");
+ System.err.println();
+ System.err.println("If given a list of files, output is written to
file_variant.ext");
+ System.err.println("If a method list is used instead of a variant,
the suffix is \"pseudo\"");
+ System.err.println("If no variant or methods are given, psaccent is
used");
+ }
+
+ private final List<String> fileNames;
+
+ private final String fileType;
+
+ private final boolean isInteractive;
+
+ private final List<String> methods;
+
+ private final PseudolocalizationPipeline pipeline;
+
+ private final String variant;
+
+ /**
+ * Process command-line arguments.
+ *
+ * @throws RuntimeException with error message on fatal errors.
+ */
+ public PseudolocalizerArguments(String[] args) {
+ Set<String> validMethods =
PseudolocalizationPipeline.getRegisteredMethods();
+ methods = new ArrayList<String>();
+ boolean error = false;
+ boolean tmpIsInteractive = false;
+ String tmpVariant = null;
+ String tmpFileType = null;
+ int argIndex = 0;
+ while (argIndex < args.length && args[argIndex].startsWith("--")) {
+ String argName = args[argIndex].substring(2);
+ if (argName.startsWith("method=")) {
+ for (String method : argName.substring(7).split(",")) {
+ if (!validMethods.contains(method)) {
+ System.err.println("Unknown method '" + method + "'");
+ error = true;
+ continue;
+ }
+ methods.add(method);
+ }
+ } else if (argName.startsWith("variant=")) {
+ if (tmpVariant != null) {
+ throw new RuntimeException("More than one variant supplied");
+ }
+ tmpVariant = argName.substring(8);
+ } else if (argName.startsWith("ext=")) {
+ for (String className : argName.substring(4).split(",")) {
+ try {
+ /*
+ * Just load the named class, let its static initializer do
whatever
+ * registration is required.
+ */
+ Class.forName(className);
+ } catch (ClassNotFoundException e) {
+ System.err.println("Unable to load extension class " +
className);
+ error = true;
+ }
+ }
+ } else if (argName.startsWith("type=")) {
+ tmpFileType = argName.substring(5);
+ } else if (argName.equals("interactive")) {
+ tmpIsInteractive = true;
+ } else {
+ System.err.println("Unrecognized option: " + argName);
+ error = true;
+ }
+ argIndex++;
+ }
+
+ if (tmpVariant != null) {
+ if (!methods.isEmpty()) {
+ System.err.println("May not specify both --variant and --method,
using variant");
+ }
+ } else if (methods.isEmpty()) {
+ tmpVariant = "psaccent";
+ }
+
+ fileType = tmpFileType;
+ variant = tmpVariant;
+ isInteractive = tmpIsInteractive;
+
+ if (error || (isInteractive && argIndex < args.length)) {
+ printUsage();
+ System.exit(1);
+ }
+
+ // collect file names to process
+ fileNames = new ArrayList<String>();
+ while (argIndex < args.length) {
+ fileNames.add(args[argIndex++]);
+ }
+
+ // build pipeline
+ if (variant != null) {
+ pipeline = PseudolocalizationPipeline.getVariantPipeline(variant);
+ } else {
+ pipeline = PseudolocalizationPipeline.buildPipeline(methods);
+ }
+ if (pipeline == null) {
+ throw new RuntimeException("Unable to construct pipeline for
methods " + methods);
+ }
+ }
+
+ /**
+ * @return list of filenames -- empty if should read from stdin
+ */
+ public List<String> getFileNames() {
+ return fileNames;
+ }
+
+ /**
+ * @return the methods
+ */
+ public List<String> getMethods() {
+ return methods;
+ }
+
+ /**
+ * @return the pipeline
+ */
+ public PseudolocalizationPipeline getPipeline() {
+ return pipeline;
+ }
+
+ /**
+ * @return the file type, or null if not specified
+ */
+ public String getType() {
+ return fileType;
+ }
+
+ /**
+ * @return the variant
+ */
+ public String getVariant() {
+ return variant;
+ }
+ /**
+ * @return the isInteractive
+ */
+ public boolean isInteractive() {
+ return isInteractive;
+ }
+ }
+
+ /**
+ * @param args
+ * @throws IOException
+ */
+ public static void main(String[] args) throws IOException {
+ Pseudolocalizer pseudolocalizer = new Pseudolocalizer();
+ PseudolocalizerArguments arguments = new
PseudolocalizerArguments(args);
+ pseudolocalizer.run(arguments);
+ }
+
+ /**
+ * Run the pseudolocalizer with the supplied arguments.
+ *
+ * @param arguments
+ * @throws IOException
+ */
+ // @VisibleForTesting
+ void run(PseudolocalizerArguments arguments) throws IOException {
+ List<String> fileNames = arguments.getFileNames();
+ PseudolocalizationPipeline pipeline = arguments.getPipeline();
+ if (arguments.isInteractive()) {
+ runStdin(pipeline);
+ return;
+ }
+ if (fileNames.size() == 0) {
+ // if no files given, read from stdin / write to stdout
+ MessageCatalog msgCat =
FormatRegistry.getMessageCatalog(arguments.getType());
+ writeMessages(msgCat, readAndProcessMessages(pipeline, msgCat,
System.in), System.out);
+ return;
+ }
+
+ // get the suffix to use for output file names
+ String suffix = arguments.getVariant();
+ if (suffix == null) {
+ suffix = "_pseudo";
+ } else {
+ suffix = "_" + suffix;
+ }
+
+ for (String fileName : fileNames) {
+ File file = new File(fileName);
+ if (!file.exists()) {
+ System.err.println("File " + fileName + " not found");
+ continue;
+ }
+
+ // get the extension of the input file and construct the output file
name
+ int lastDot = fileName.lastIndexOf('.');
+ String extension;
+ String outFileName;
+ if (lastDot >= 0) {
+ extension = fileName.substring(lastDot + 1);
+ outFileName = fileName.substring(0, lastDot) + suffix + "." +
extension;
+ } else {
+ extension = "";
+ outFileName = fileName + suffix;
+ }
+ System.out.println("Processing " + fileName + " into " +
outFileName);
+
+ // get the message catalog object for the specified (or inferred)
file type
+ String fileType = arguments.getType();
+ if (fileType == null) {
+ fileType = extension;
+ }
+ MessageCatalog msgCat = FormatRegistry.getMessageCatalog(fileType);
+
+ // read and process messages
+ InputStream inputStream = new FileInputStream(file);
+ List<Message> processedMessages = readAndProcessMessages(pipeline,
msgCat, inputStream);
+
+ OutputStream outputStream = new FileOutputStream(new
File(outFileName));
+ writeMessages(msgCat, processedMessages, outputStream);
+ }
+ }
+
+ /**
+ * @param pipeline
+ * @param msgCat
+ * @param inputStream
+ * @return processed messages
+ * @throws IOException
+ */
+ private List<Message> readAndProcessMessages(PseudolocalizationPipeline
pipeline,
+ MessageCatalog msgCat, InputStream inputStream)
+ throws IOException {
+ List<Message> processedMessages = new ArrayList<Message>();
+ ReadableMessageCatalog input = msgCat.readFrom(inputStream);
+ try {
+ for (Message msg : input.readMessages()) {
+ pipeline.localize(msg);
+ processedMessages.add(msg);
+ }
+ } finally {
+ input.close();
+ }
+ return processedMessages;
+ }
+
+ /**
+ * @param pipeline
+ * @throws IOException
+ */
+ private void runStdin(PseudolocalizationPipeline pipeline) throws
IOException {
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
+ String line;
+ System.out.println("Enter text to pseudolocalize:");
+ while ((line = reader.readLine()) != null) {
+ if (line.length() == 0) {
+ break;
+ }
+ System.out.println("=> " + pipeline.localize(line));
+ }
+ }
+
+ /**
+ * @param msgCat
+ * @param messages
+ * @param outputStream
+ * @throws IOException
+ */
+ private void writeMessages(MessageCatalog msgCat, List<Message> messages,
+ OutputStream outputStream) throws IOException {
+ // write messages
+ WritableMessageCatalog output = msgCat.writeTo(outputStream);
+ try {
+ for (Message msg : messages) {
+ output.writeMessage(msg);
+ }
+ } finally {
+ output.close();
+ }
+ }
+}
=======================================
---
/trunk/java/com/google/i18n/pseudolocalization/PseudolocalizationPipeline.java
Mon Apr 25 14:33:15 2011
+++
/trunk/java/com/google/i18n/pseudolocalization/PseudolocalizationPipeline.java
Tue Oct 11 14:00:14 2011
@@ -364,7 +364,11 @@
*/
public static synchronized PseudolocalizationPipeline getVariantPipeline(
Map<String, String> options, boolean preserveHtml, String variant) {
- String[] pipeline =
variantRegistry.get(variant.toLowerCase(Locale.ENGLISH));
+ variant = variant.toLowerCase(Locale.ENGLISH);
+ String[] pipeline = variantRegistry.get(variant);
+ if (pipeline == null && variant.startsWith("x-")) {
+ pipeline = variantRegistry.get(variant.substring(2));
+ }
if (pipeline == null) {
return null;
}

Reply all
Reply to author
Forward
0 new messages