| 1 | package com.jsql.view.swing.util; | |
| 2 | ||
| 3 | import com.jsql.util.I18nUtil; | |
| 4 | import com.jsql.util.StringUtil; | |
| 5 | import com.jsql.view.swing.dialog.DialogAbout; | |
| 6 | import com.jsql.view.swing.text.JPlaceholder; | |
| 7 | import com.jsql.view.swing.text.JToolTipI18n; | |
| 8 | import com.jsql.view.swing.tree.model.NodeModelEmpty; | |
| 9 | import org.apache.commons.lang3.StringUtils; | |
| 10 | ||
| 11 | import javax.swing.*; | |
| 12 | import javax.swing.text.JTextComponent; | |
| 13 | import java.util.*; | |
| 14 | ||
| 15 | public class I18nViewUtil { | |
| 16 | ||
| 17 | /** | |
| 18 | * A list of graphical components for each i18n keys in the main properties | |
| 19 | */ | |
| 20 | private static final Map<String, Set<Object>> COMPONENTS_LOCALIZED = new HashMap<>(); | |
| 21 | | |
| 22 | // Initialize the list of graphical components | |
| 23 | static { | |
| 24 | for (String keyI18n: I18nUtil.BUNDLE_ROOT.keySet()) { | |
| 25 | I18nViewUtil.COMPONENTS_LOCALIZED.put(keyI18n, new HashSet<>()); | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | private I18nViewUtil() { | |
| 30 | // Utility class | |
| 31 | } | |
| 32 | | |
| 33 | /** | |
| 34 | * Return the i18n keys of components whose text is replaced | |
| 35 | * when the translation changes. | |
| 36 | * @return a set of key names of an i18n key in the properties | |
| 37 | */ | |
| 38 | public static Set<String> keys() { | |
| 39 |
1
1. keys : replaced return value with Collections.emptySet for com/jsql/view/swing/util/I18nViewUtil::keys → NO_COVERAGE |
return I18nViewUtil.COMPONENTS_LOCALIZED.keySet(); |
| 40 | } | |
| 41 | | |
| 42 | /** | |
| 43 | * Get a list of graphical components whose text corresponds | |
| 44 | * to the i18n key in the properties. | |
| 45 | * @param key name of an i18n key in the properties | |
| 46 | * @return set of graphical components | |
| 47 | */ | |
| 48 | public static Set<Object> componentsByKey(String key) { | |
| 49 |
1
1. componentsByKey : replaced return value with Collections.emptySet for com/jsql/view/swing/util/I18nViewUtil::componentsByKey → NO_COVERAGE |
return I18nViewUtil.COMPONENTS_LOCALIZED.get(key); |
| 50 | } | |
| 51 | ||
| 52 | public static void switchI18nComponents() { | |
| 53 | for (String key : I18nViewUtil.keys()) { | |
| 54 | for (Object component : I18nViewUtil.componentsByKey(key)) { | |
| 55 | switch (component) { | |
| 56 |
1
1. switchI18nComponents : removed call to com/jsql/view/swing/text/JPlaceholder::setPlaceholderText → NO_COVERAGE |
case JPlaceholder c -> c.setPlaceholderText(I18nUtil.valueByKey(key)); |
| 57 |
1
1. switchI18nComponents : removed call to com/jsql/view/swing/dialog/DialogAbout::setTitle → NO_COVERAGE |
case DialogAbout c -> c.setTitle(I18nUtil.valueByKey(key) + " " + StringUtil.APP_NAME); |
| 58 |
1
1. switchI18nComponents : removed call to com/jsql/view/swing/text/JToolTipI18n::setText → NO_COVERAGE |
case JToolTipI18n c -> c.setText(I18nViewUtil.valueByKey(key)); |
| 59 |
1
1. switchI18nComponents : removed call to com/jsql/view/swing/tree/model/NodeModelEmpty::setText → NO_COVERAGE |
case NodeModelEmpty c -> c.setText(I18nViewUtil.valueByKey(key)); |
| 60 |
1
1. switchI18nComponents : removed call to javax/swing/JLabel::setText → NO_COVERAGE |
case JLabel c -> c.setText(I18nViewUtil.valueByKey(key)); |
| 61 |
1
1. switchI18nComponents : removed call to javax/swing/JMenuItem::setText → NO_COVERAGE |
case JMenuItem c -> c.setText(I18nViewUtil.valueByKey(key)); |
| 62 |
1
1. switchI18nComponents : removed call to javax/swing/JButton::setText → NO_COVERAGE |
case JButton c -> c.setText(I18nViewUtil.valueByKey(key)); |
| 63 |
1
1. switchI18nComponents : removed call to javax/swing/JComboBox::setToolTipText → NO_COVERAGE |
case JComboBox<?> c -> c.setToolTipText(I18nViewUtil.valueByKey(key)); |
| 64 |
1
1. switchI18nComponents : removed call to javax/swing/text/JTextComponent::setText → NO_COVERAGE |
case JTextComponent c -> c.setText(I18nViewUtil.valueByKey(key)); |
| 65 | default -> { | |
| 66 | // ignore | |
| 67 | } | |
| 68 | } | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | | |
| 73 | /** | |
| 74 | * Add a graphical component to those whose text must be changed when | |
| 75 | * the language changes. | |
| 76 | * @param key name of an i18n key in the properties | |
| 77 | * @param component graphical component which will receive the translated text | |
| 78 | */ | |
| 79 | public static void addComponentForKey(String key, Object component) { | |
| 80 | I18nViewUtil.COMPONENTS_LOCALIZED.get(key.replace(" ", "_")).add(component); // e.g BIND BINARY | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Return the text corresponding to an i18n key in the properties. | |
| 85 | * @param key an i18n key in the properties | |
| 86 | * @return text corresponding to the key | |
| 87 | */ | |
| 88 | public static String valueByKey(String key) { | |
| 89 |
2
1. valueByKey : replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::valueByKey → NO_COVERAGE 2. valueByKey : negated conditional → NO_COVERAGE |
return I18nViewUtil.isNonUbuntu(I18nUtil.getCurrentLocale()) |
| 90 | ? I18nViewUtil.formatNonLatin(I18nUtil.valueByKey(key)) | |
| 91 | : I18nUtil.valueByKey(key); | |
| 92 | } | |
| 93 | ||
| 94 | public static boolean isNonUbuntu(Locale locale) { | |
| 95 |
2
1. isNonUbuntu : negated conditional → NO_COVERAGE 2. isNonUbuntu : replaced boolean return with true for com/jsql/view/swing/util/I18nViewUtil::isNonUbuntu → NO_COVERAGE |
return Locale.forLanguageTag("zh").getLanguage().equals(locale.getLanguage()) |
| 96 |
1
1. isNonUbuntu : negated conditional → NO_COVERAGE |
|| Locale.forLanguageTag("ko").getLanguage().equals(locale.getLanguage()) |
| 97 |
1
1. isNonUbuntu : negated conditional → NO_COVERAGE |
|| Locale.forLanguageTag("ja").getLanguage().equals(locale.getLanguage()); |
| 98 | } | |
| 99 | ||
| 100 | public static String formatNonLatin(String label) { | |
| 101 |
1
1. formatNonLatin : replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::formatNonLatin → NO_COVERAGE |
return I18nViewUtil.formatNonLatin(label, StringUtils.EMPTY); |
| 102 | } | |
| 103 | ||
| 104 | public static String formatNonLatin(String label, String custom) { | |
| 105 |
1
1. formatNonLatin : replaced return value with "" for com/jsql/view/swing/util/I18nViewUtil::formatNonLatin → NO_COVERAGE |
return String.format( |
| 106 | "<html><span style=\"font-family:'%s';%s\">%s</span></html>", | |
| 107 | UiUtil.FONT_NAME_MONO_ASIAN, | |
| 108 | custom, | |
| 109 | label | |
| 110 | ); | |
| 111 | } | |
| 112 | } | |
Mutations | ||
| 39 |
1.1 |
|
| 49 |
1.1 |
|
| 56 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |
|
| 59 |
1.1 |
|
| 60 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 64 |
1.1 |
|
| 89 |
1.1 2.2 |
|
| 95 |
1.1 2.2 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 101 |
1.1 |
|
| 105 |
1.1 |