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:[email protected]">Bogdan Stefanescu</a> 018 * <a href="mailto:[email protected]">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:[email protected]">Bogdan Stefanescu</a> 041 * @author <a href="mailto:[email protected]">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 public Object exec(List arguments) throws TemplateModelException { 063 int size = arguments.size(); 064 if (size < 2) { 065 throw new TemplateModelException("Invalid number of arguments for messages(key, local [, args ..]) method"); 066 } 067 String key; 068 SimpleScalar scalar = (SimpleScalar) arguments.get(0); 069 if (scalar != null) { 070 key = scalar.getAsString(); 071 } else { 072 throw new TemplateModelException("the argument is not defined"); 073 } 074 075 scalar = (SimpleScalar) arguments.get(1); 076 String locale; 077 if (scalar != null) { 078 locale = scalar.getAsString(); 079 } else { 080 throw new TemplateModelException("the argument is not defined"); 081 } 082 083 String value; 084 try { 085 value = bundle.getString(key, new Locale(locale)); 086 } catch (MissingResourceException e) { 087 return '!' + key + '!'; 088 } 089 if (size > 2) { // format the string using given args 090 String[] args = new String[size - 2]; 091 for (int i = 0; i < args.length; i++) { 092 args[i] = ((SimpleScalar) arguments.get(i + 2)).getAsString(); 093 } 094 value = MessageFormat.format(value, (Object[]) args); 095 } 096 return value; 097 } 098 099}