Java Code Examples for org.springframework.extensions.surf.util.I18NUtil#getNearestLocale()

The following examples show how to use org.springframework.extensions.surf.util.I18NUtil#getNearestLocale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParameterizedItemDefinitionImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getParameterDefintion(java.lang.String)
 */
public ParameterDefinition getParameterDefintion(String name)
{
    ParameterDefinition result = null;

    if (null != paramDefinitionsByName)
    {
        Set<Locale> locales = paramDefinitionsByName.keySet();
        Locale match = I18NUtil.getNearestLocale(I18NUtil.getLocale(), locales);
        Map<String, ParameterDefinition> localizedDefinitionsByName = paramDefinitionsByName.get(match);

        if (null == localizedDefinitionsByName)
        {
            localizedDefinitionsByName = paramDefinitionsByName.get(Locale.ROOT);
        }

        result = (null == localizedDefinitionsByName) ? (null) : (localizedDefinitionsByName.get(name));
    }

    return result;
}
 
Example 2
Source File: MultilingualContentServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** {@inheritDoc} */
public NodeRef getTranslationForLocale(NodeRef translationNodeRef, Locale locale)
{
    // Get all the translations
    Map<Locale, NodeRef> nodeRefsByLocale = getTranslations(translationNodeRef);
    // Get the closest matching locale
    Set<Locale> locales = nodeRefsByLocale.keySet();
    Locale nearestLocale = I18NUtil.getNearestLocale(locale, locales);
    NodeRef nearestNodeRef = nodeRefsByLocale.get(nearestLocale);
    if (nearestNodeRef == null)
    {
        // There is no translation for the locale, so get the pivot translation
        nearestNodeRef = getPivotTranslation(translationNodeRef);
        if (nearestNodeRef == null)
        {
            // There is no pivot translation, so just use the given node
            nearestNodeRef = translationNodeRef;
        }
    }
    // Done
    if (logger.isDebugEnabled())
    {
        logger.debug("Found nearest locale: \n" +
                "   Given node:   " + translationNodeRef + "\n" +
                "   Given locale: " + locale + "\n" +
                "   Found node:   " + nearestNodeRef + "\n" +
                "   Found locale: " + nearestLocale);
    }
    return nearestNodeRef;
}
 
Example 3
Source File: MLPropertyInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Serializable getClosestValue(MLText mlText)
{
    Set<Locale> locales = mlText.getLocales();
    Locale contentLocale = I18NUtil.getContentLocale();
    Locale locale = I18NUtil.getNearestLocale(contentLocale, locales);
    if (locale != null)
    {
        return mlText.getValue(locale);
    }

    // If the content locale is too specific, try relaxing it to just language
    Locale contentLocaleLang = I18NUtil.getContentLocaleLang();
    // We do not expect contentLocaleLang to be null
    if (contentLocaleLang != null)
    {
        locale = I18NUtil.getNearestLocale(contentLocaleLang, locales);
        if (locale != null)
        {
            return mlText.getValue(locale);
        }
    }
    else
    {
        logger.warn("contentLocaleLang is null in getClosestValue. This is not expected.");
    }
    
    // Just return the default translation
    return mlText.getDefaultValue();
}
 
Example 4
Source File: MLPropertyInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Locale getClosestLocale(Collection<?> collection)
{
    if (collection.size() == 0)
    {
        return null;
    }
    // Use the available keys as options
    HashSet<Locale> locales = new HashSet<Locale>();
    for(Object o : collection)
    {
        MLText mlText = (MLText)o;
        locales.addAll(mlText.keySet());
    }
    // Try the content locale
    Locale locale = I18NUtil.getContentLocale();
    Locale match = I18NUtil.getNearestLocale(locale, locales);
    if (match == null)
    {
        // Try just the content locale language
        locale = I18NUtil.getContentLocaleLang();
        match = I18NUtil.getNearestLocale(locale, locales);
        if (match == null)
        {
            // No close matches for the locale - go for the default locale
            locale = I18NUtil.getLocale();
            match = I18NUtil.getNearestLocale(locale, locales);
            if (match == null)
            {
                // just get any locale
                match = I18NUtil.getNearestLocale(null, locales);
            }
        }
    }
    return match;
}
 
Example 5
Source File: ParameterizedItemDefinitionImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getParameterDefinitions()
 */
public List<ParameterDefinition> getParameterDefinitions()
{
    Set<Locale> locales = parameterDefinitions.keySet();
    Locale match = I18NUtil.getNearestLocale(I18NUtil.getLocale(), locales);
    List<ParameterDefinition> result = parameterDefinitions.get(match);

    if (null == result)
    {
        result = parameterDefinitions.get(Locale.ROOT);
    }

    return result;
}
 
Example 6
Source File: MLText.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * The given locale is used to search for a matching value according to:
 * <ul>
 *   <li>An exact locale match</li>
 *   <li>A match of locale ISO language codes</li>
 *   <li>The value for the locale provided in the {@link MLText#MLText(Locale, String) constructor}</li>
 *   <li>An arbitrary value</li>
 *   <li><tt>null</tt></li>
 * </ul>
 * 
 * @param locale the locale to use as the starting point of the value search
 * @return Returns a default <tt>String</tt> value or null if one isn't available.
 *      <tt>null</tt> will only be returned if there are no values associated with
 *      this instance.  With or without a match, the return value may be <tt>null</tt>,
 *      depending on the values associated with the locales.
 */
public String getClosestValue(Locale locale)
{
    if (this.size() == 0)
    {
        return null;
    }
    // Use the available keys as options
    Set<Locale> options = keySet();
    // Get a match
    Locale match = I18NUtil.getNearestLocale(locale, options);
    if (match == null)
    {
        // No close matches for the locale - go for the default locale
        locale = I18NUtil.getLocale();
        match = I18NUtil.getNearestLocale(locale, options);
        if (match == null)
        {
            // just get any locale
            match = I18NUtil.getNearestLocale(null, options);
        }
    }
    // Did we get a match
    if (match == null)
    {
        // We could find no locale matches
        return null;
    }
    else
    {
        return get(match);
    }
}
 
Example 7
Source File: MessageServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Locale getNearestLocale(Locale templateLocale, Set<Locale> options)
{
    return I18NUtil.getNearestLocale(templateLocale, options);
}