Package com.google.sitebricks.i18n

Examples of com.google.sitebricks.i18n.Message


  private void bindMessages(Localization localization) {
    Class<?> iface = localization.clazz;
    Map<String, MessageDescriptor> messages = Maps.newHashMap();

    for (Method method : iface.getMethods()) {
      Message message = method.getAnnotation(Message.class);

      check(null != message,
          "Found an i18n interface method missing @Message annotation: ", iface, method);

      if (null != message) {
        check(!Strings.empty(message.message()),
            "Empty @Message annotation is not allowed ", iface, method);
      }

      String template = localization.messageBundle.get(method.getName());
      check(null != template,
          "Provided resource bundle does not contain a localization for message: ", iface, method);
      check(String.class.equals(method.getReturnType()),
          "All i18n interface methods MUST return String: ", iface, method);

      int argumentCount = method.getParameterTypes().length;
      Map<String, Type> arguments = Maps.newLinkedHashMap();

      for (int i = 0; i < argumentCount; i++) {
        Annotation[] annotations = method.getParameterAnnotations()[i];

        check(annotations.length == 1,
            "Only @Named annotations are allowed on i18n method arguments: ", iface, method);
        if (annotations.length == 0) {
          continue;
        }

        check(Named.class.isInstance(annotations[0]),
            "Named annotation is missing from i18n interface method argument: ", iface, method);

        // Bind each argument to a template parameter a la Dynamic Finders.
        arguments.put(((Named) annotations[0]).value(), method.getParameterTypes()[i]);
      }

      // No point in throwing an NPE ourselves, but we want to keep processing errors so continue
      if (null == template || null == message) {
        continue;
      }

      // Compile arg names against message template to ensure it works.
      List<Token> tokens = null;
      try {
        MvelEvaluatorCompiler compiler = new MvelEvaluatorCompiler(arguments);

        // Compile both the default message as well as the provided localized one.
        Parsing.tokenize(message.message(), compiler);
        tokens = Parsing.tokenize(template, compiler);
      } catch (ExpressionCompileException e) {
        check(false, "Compile error in i18n message template: \n  " + e.getError().getError() +
            " in expression " + e.getError().getExpression() +"\n\n  ...in: ", iface, method);
      }
View Full Code Here


   */
  public static Localization defaultLocalizationFor(Class<?> iface) {
    Map<String, String> defaultMessages = Maps.newHashMap();

    for (Method method : iface.getMethods()) {
      Message msg = method.getAnnotation(Message.class);
      if (null != msg) {
        defaultMessages.put(method.getName(), msg.message());
      }
    }

    return new Localization(iface, Locale.getDefault(), defaultMessages);
  }
View Full Code Here

  private void bindMessages(Localization localization) {
    Class<?> iface = localization.clazz;
    Map<String, MessageDescriptor> messages = Maps.newHashMap();

    for (Method method : iface.getMethods()) {
      Message message = method.getAnnotation(Message.class);

      check(null != message,
          "Found an i18n interface method missing @Message annotation: ", iface, method);

      if (null != message) {
        check(!Strings.empty(message.message()),
            "Empty @Message annotation is not allowed ", iface, method);
      }

      String template = localization.messageBundle.get(method.getName());
      check(null != template,
          "Provided resource bundle does not contain a localization for message: ", iface, method);
      check(String.class.equals(method.getReturnType()),
          "All i18n interface methods MUST return String: ", iface, method);

      int argumentCount = method.getParameterTypes().length;
      Map<String, Type> arguments = Maps.newLinkedHashMap();

      for (int i = 0; i < argumentCount; i++) {
        Annotation[] annotations = method.getParameterAnnotations()[i];

        check(annotations.length == 1,
            "Only @Named annotations are allowed on i18n method arguments: ", iface, method);
        if (annotations.length == 0) {
          continue;
        }

        check(Named.class.isInstance(annotations[0]),
            "Named annotation is missing from i18n interface method argument: ", iface, method);

        // Bind each argument to a template parameter a la Dynamic Finders.
        arguments.put(((Named) annotations[0]).value(), method.getParameterTypes()[i]);
      }

      // No point in throwing an NPE ourselves, but we want to keep processing errors so continue
      if (null == template || null == message) {
        continue;
      }

      // Compile arg names against message template to ensure it works.
      List<Token> tokens = null;
      try {
        MvelEvaluatorCompiler compiler = new MvelEvaluatorCompiler(arguments);

        // Compile both the default message as well as the provided localized one.
        Parsing.tokenize(message.message(), compiler);
        tokens = Parsing.tokenize(template, compiler);
      } catch (ExpressionCompileException e) {
        check(false, "Compile error in i18n message template: \n  " + e.getError().getError() +
            " in expression " + e.getError().getExpression() +"\n\n  ...in: ", iface, method);
      }
View Full Code Here

   */
  public static Localization defaultLocalizationFor(Class<?> iface) {
    Map<String, String> defaultMessages = Maps.newHashMap();

    for (Method method : iface.getMethods()) {
      Message msg = method.getAnnotation(Message.class);
      if (null != msg) {
        defaultMessages.put(method.getName(), msg.message());
      }
    }

    return new Localization(iface, Locale.getDefault(), defaultMessages);
  }
View Full Code Here

TOP

Related Classes of com.google.sitebricks.i18n.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.