|
| 1 | +package me.cxdev.commerce.toolkit.impex; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | + |
| 6 | +import de.hybris.platform.servicelayer.i18n.util.LanguageUtils; |
| 7 | + |
| 8 | +import org.apache.commons.lang3.StringUtils; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +public class ImpexLocalizationHelper { |
| 13 | + private static final Logger LOG = LoggerFactory.getLogger(ImpexLocalizationHelper.class); |
| 14 | + |
| 15 | + public static boolean languageSupported(final String isocode) { |
| 16 | + return LanguageUtils.isLanguagePresent(isocode); |
| 17 | + } |
| 18 | + |
| 19 | + public static boolean languageSupported(final String isocode, String languages) { |
| 20 | + if (StringUtils.isBlank(languages)) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + boolean isContainedInList = Arrays.stream(StringUtils.split(languages, ",")) |
| 25 | + .map(StringUtils::trimToEmpty) |
| 26 | + .anyMatch(isocode::equalsIgnoreCase); |
| 27 | + return isContainedInList && languageSupported(isocode); |
| 28 | + } |
| 29 | + |
| 30 | + public static void filterUnlocalizedLine(HashMap<Integer, String> valueLine, Integer column) { |
| 31 | + if (isLocalizedLine(valueLine, column)) { |
| 32 | + valueLine.clear(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void filterUnlocalizedOrPrimaryLine(HashMap<Integer, String> valueLine, Integer column, String isocode) { |
| 37 | + if (isLocalizedLine(valueLine, column) && !hasCorrectLocale(valueLine, column, isocode)) { |
| 38 | + valueLine.clear(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static void filterLocalizedLine(HashMap<Integer, String> valueLine, Integer column, String isocode) { |
| 43 | + if (isUnlocalizedLine(valueLine, column) || !hasCorrectLocale(valueLine, column, isocode)) { |
| 44 | + valueLine.clear(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static boolean isLocalizedLine(HashMap<Integer, String> valueLine, Integer column) { |
| 49 | + return StringUtils.isNotBlank(valueLine.get(column)); |
| 50 | + } |
| 51 | + |
| 52 | + public static boolean isUnlocalizedLine(HashMap<Integer, String> valueLine, Integer column) { |
| 53 | + return !isLocalizedLine(valueLine, column); |
| 54 | + } |
| 55 | + |
| 56 | + public static boolean hasCorrectLocale(HashMap<Integer, String> valueLine, Integer column, String isocode) { |
| 57 | + return (StringUtils.isBlank(isocode) && StringUtils.isBlank(valueLine.get(column))) |
| 58 | + || StringUtils.equalsIgnoreCase(valueLine.get(column), isocode); |
| 59 | + } |
| 60 | +} |
0 commit comments