Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2012-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 ninja.i18n;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import ninja.Context;
import ninja.Result;
import ninja.utils.NinjaConstant;
import ninja.utils.NinjaProperties;
import ninja.utils.SwissKnife;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class MessagesImpl implements Messages {
private static Logger logger = LoggerFactory.getLogger(MessagesImpl.class);
private final Map langToKeyAndValuesMapping;
private final NinjaProperties ninjaProperties;
private final Lang lang;
@Inject
public MessagesImpl(NinjaProperties ninjaProperties,
Lang lang) {
this.ninjaProperties = ninjaProperties;
this.lang = lang;
this.langToKeyAndValuesMapping = loadAllMessageFilesForRegisteredLanguages();
}
@Override
public Optional get(String key,
Context context,
Optional result,
Object... parameter) {
Optional language = lang.getLanguage(context, result);
return get(key, language, parameter);
}
@Override
public Optional get(String key, Optional language, Object... params) {
Configuration configuration = getLanguageConfigurationForLocale(language);
String value = configuration.getString(key);
if (value != null) {
MessageFormat messageFormat = getMessageFormatForLocale(value, language);
return Optional.of(messageFormat.format(params));
} else {
return Optional.absent();
}
}
@Override
public Map