001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 018 * <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a> 019 * 020 * 021 * $Id$ 022 */ 023 024package org.nuxeo.ecm.platform.rendering.fm.extensions; 025 026import java.text.MessageFormat; 027import java.util.List; 028import java.util.Locale; 029import java.util.MissingResourceException; 030 031import org.nuxeo.ecm.platform.rendering.fm.i18n.ResourceComposite; 032 033import freemarker.template.SimpleScalar; 034import freemarker.template.TemplateMethodModelEx; 035import freemarker.template.TemplateModelException; 036 037/** 038 * Message method that differs from the standard one as its second argument is the locale. 039 * 040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 041 * @author <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a> 042 */ 043public class LocaleMessagesMethod implements TemplateMethodModelEx { 044 045 protected ResourceComposite bundle; 046 047 public LocaleMessagesMethod(ResourceComposite bundle) { 048 setBundle(bundle); 049 } 050 051 public void setBundle(ResourceComposite bundle) { 052 this.bundle = bundle; 053 if (this.bundle == null) { 054 this.bundle = new ResourceComposite(); 055 } 056 } 057 058 public ResourceComposite getBundle() { 059 return bundle; 060 } 061 062 @Override 063 public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { 064 int size = arguments.size(); 065 if (size < 2) { 066 throw new TemplateModelException("Invalid number of arguments for messages(key, local [, args ..]) method"); 067 } 068 String key; 069 SimpleScalar scalar = (SimpleScalar) arguments.get(0); 070 if (scalar != null) { 071 key = scalar.getAsString(); 072 } else { 073 throw new TemplateModelException("the argument is not defined"); 074 } 075 076 scalar = (SimpleScalar) arguments.get(1); 077 String locale; 078 if (scalar != null) { 079 locale = scalar.getAsString(); 080 } else { 081 throw new TemplateModelException("the argument is not defined"); 082 } 083 084 String value; 085 try { 086 value = bundle.getString(key, new Locale(locale)); 087 } catch (MissingResourceException e) { 088 return '!' + key + '!'; 089 } 090 if (size > 2) { // format the string using given args 091 String[] args = new String[size - 2]; 092 for (int i = 0; i < args.length; i++) { 093 args[i] = ((SimpleScalar) arguments.get(i + 2)).getAsString(); 094 } 095 value = MessageFormat.format(value, (Object[]) args); 096 } 097 return value; 098 } 099 100}