/*
 * Copyright 2007-2016 the original author or authors.
 *
 * 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 net.ymate.platform.webmvc.support;

import net.ymate.platform.core.i18n.II18NEventHandler;
import net.ymate.platform.core.lang.BlurObject;
import net.ymate.platform.core.util.RuntimeUtils;
import net.ymate.platform.webmvc.context.WebContext;
import net.ymate.platform.webmvc.util.CookieHelper;
import org.apache.commons.lang.LocaleUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

/**
 * @author 刘镇 (suninformation@163.com) on 15/7/20 上午10:02
 * @version 1.0
 */
public class I18NWebEventHandler implements II18NEventHandler {

    public static final String I18N_LANG_KEY = "_long";

    public Locale onLocale() {
        String _langStr = null;
        // 先尝试取URL参数变量
        if (WebContext.getContext() != null) {
            _langStr = WebContext.getRequestContext().getAttribute(I18N_LANG_KEY);
            if (_langStr == null) {
                // 再尝试从请求参数中获取
                _langStr = WebContext.getRequest().getParameter(I18N_LANG_KEY);
                if (_langStr == null) {
                    // 最后一次机会，尝试读取Cookies
                    BlurObject _langCookie = CookieHelper.bind(WebContext.getContext().getOwner()).getCookie(I18N_LANG_KEY);
                    if (_langCookie != null) {
                        _langStr = _langCookie.toStringValue();
                    }
                }
            }
        }
        return LocaleUtils.toLocale(StringUtils.trimToNull(_langStr));
    }

    public void onChanged(Locale locale) {
        if (WebContext.getContext() != null) {
            CookieHelper.bind(WebContext.getContext().getOwner()).setCookie(I18N_LANG_KEY, locale.toString());
        }
    }

    public InputStream onLoad(String resourceName) throws IOException {
        File _resFile = new File(RuntimeUtils.replaceEnvVariable("${user.dir}/i18n/"), resourceName);
        if (_resFile.exists() && _resFile.canRead()) {
            return new FileInputStream(_resFile);
        }
        return null;
    }
}

@@ -807,24 +807,65 @@
 ntimeUtils;%0A
+import net.ymate.platform.webmvc.WebMVC;%0A
 import net.y
@@ -905,16 +905,16 @@
 ontext;%0A
-
 import n
@@ -1385,16 +1385,48 @@
 long%22;%0A%0A
+    private String __i18nHome;%0A%0A
     publ
@@ -1640,32 +1640,55 @@
 %0A            if 
+(StringUtils.trimToNull
 (_langStr == nul
@@ -1672,32 +1672,33 @@
 mToNull(_langStr
+)
  == null) %7B%0A    
@@ -1819,24 +1819,47 @@
          if 
+(StringUtils.trimToNull
 (_langStr ==
@@ -1855,16 +1855,17 @@
 _langStr
+)
  == null
@@ -2361,32 +2361,50 @@
 .getContext() !=
+ null && locale !=
  null) %7B%0A       
@@ -2608,85 +2608,323 @@
     
-File _resFil
+if (__i18nHom
 e =
+=
  n
-ew File(RuntimeUtils.replaceEnvVariable(%22$%7Buser.dir%7D/i18n/%22)
+ull) %7B%0A            __i18nHome = RuntimeUtils.replaceEnvVariable(StringUtils.defaultIfBlank(WebMVC.get().getOwner().getConfig().getParam(%22i18n_resources_home%22), %22$%7Broot%7D/i18n/%22));%0A        %7D%0A        if (StringUtils.trimToNull(resourceName) != null) %7B%0A            File _resFile = new File(__i18nHome
 , re
@@ -2932,24 +2932,28 @@
 ourceName);%0A
+    
         if (
@@ -2972,16 +2972,37 @@
 sts() &&
+ _resFile.isFile() &&
  _resFil
@@ -3012,24 +3012,28 @@
 anRead()) %7B%0A
+    
             
@@ -3066,24 +3066,38 @@
 (_resFile);%0A
+            %7D%0A
         %7D%0A  

优化WebMVC框架的I18N事件处理器