├── .gitignore ├── README.md ├── strings-xml-tools.iml ├── META-INF └── plugin.xml ├── src └── com │ └── civ │ └── stringsTools │ ├── Helpers.java │ ├── SortStringsAction.java │ └── AddMissingStringsAction.java ├── strings.xml tools.ipr └── strings.xml tools.iws /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #The project is no longer developed or supported 2 | 3 | #Android strings.xml tools 4 | 5 | A plugin for Intellij IDEA or Android studio which eases management of strings in multiple localization 6 | files in Android projects. 7 | 8 | ####Features: 9 | - Adding missing strings from the default strings.xml to localized one (Generate -> Add missing strings); 10 | - Adding missing strings to the default strings.xml file; 11 | - Sorting entries in localized file in the same order, as in the default one (Editor context menu -> Sort strings). 12 | - More will be added soon. 13 | -------------------------------------------------------------------------------- /strings-xml-tools.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.civ.androidStringsTools 3 | Android strings.xml tools 4 | 1.0 5 | Constantine Ivanov 6 | 7 | 8 | Provides simple actions for sorting entries in Android localization files and adding missing ones. 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 20 | org.jetbrains.android 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/com/civ/stringsTools/Helpers.java: -------------------------------------------------------------------------------- 1 | package com.civ.stringsTools; 2 | 3 | import com.intellij.psi.PsiDirectory; 4 | import com.intellij.psi.PsiFile; 5 | import com.intellij.psi.xml.XmlFile; 6 | import org.jetbrains.annotations.Nullable; 7 | 8 | /** 9 | * Created by kiva on 6/9/14. 10 | */ 11 | public class Helpers { 12 | 13 | public static final String STRINGS_FILE_NAME = "strings.xml"; 14 | public static final String DEFAULT_VALUES_DIR_NAME = "values"; 15 | public static final String LOCALIZED_VALUES_DIR_NAME = "values-"; 16 | public static final String TAG_STRING = "string"; 17 | public static final String TAG_STRING_ARRAY = "string-array"; 18 | public static final String TAG_PLURALS = "plurals"; 19 | public static final String ATTRIBUTE_NAME = "name"; 20 | 21 | /** 22 | * @return true if the given PsiFile is a strings.xml file. 23 | */ 24 | public static boolean isStringsFile(PsiFile file) { 25 | return file instanceof XmlFile && file.getName().equals(STRINGS_FILE_NAME); 26 | } 27 | 28 | /** 29 | * Returns true if the given file is the default strings.xml file 30 | */ 31 | public static boolean isLocalizedStringsFile(PsiFile file) { 32 | if(!isStringsFile(file)) 33 | return false; 34 | 35 | PsiDirectory dir = file.getContainingDirectory(); 36 | return dir != null && dir.getName().startsWith(LOCALIZED_VALUES_DIR_NAME); 37 | } 38 | 39 | /** 40 | * Returns default strings.xml file. 41 | * @param currentFile One of localized strings files. 42 | */ 43 | public static PsiFile getDefaultStringsFile(PsiFile currentFile) { 44 | PsiDirectory currentDir = currentFile.getContainingDirectory(); 45 | if (currentDir == null) 46 | return null; 47 | // If the specified file is from the "values" directory, it's already our default strings file 48 | if (currentDir.getName().equals(DEFAULT_VALUES_DIR_NAME)) 49 | return currentFile; 50 | 51 | PsiDirectory defaultDir = currentDir.getParentDirectory().findSubdirectory(DEFAULT_VALUES_DIR_NAME); 52 | if (defaultDir == null) 53 | return null; 54 | 55 | return defaultDir.findFile(STRINGS_FILE_NAME); 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/com/civ/stringsTools/SortStringsAction.java: -------------------------------------------------------------------------------- 1 | package com.civ.stringsTools; 2 | 3 | import com.intellij.openapi.actionSystem.AnAction; 4 | import com.intellij.openapi.actionSystem.AnActionEvent; 5 | import com.intellij.openapi.actionSystem.DataKeys; 6 | import com.intellij.openapi.command.WriteCommandAction; 7 | import com.intellij.psi.PsiElement; 8 | import com.intellij.psi.PsiFile; 9 | import com.intellij.psi.xml.XmlComment; 10 | import com.intellij.psi.xml.XmlFile; 11 | import com.intellij.psi.xml.XmlTag; 12 | 13 | import java.util.ArrayList; 14 | import java.util.HashMap; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | public class SortStringsAction extends AnAction { 19 | 20 | public void actionPerformed(AnActionEvent e) { 21 | PsiFile currentFile = e.getData(DataKeys.PSI_FILE); 22 | PsiFile defaultFile = Helpers.getDefaultStringsFile(currentFile); 23 | 24 | WriteCommandAction.runWriteCommandAction(e.getProject(), new SortStringsCommand((XmlFile) currentFile, 25 | (XmlFile) defaultFile)); 26 | } 27 | 28 | @Override 29 | public void update(AnActionEvent e) { 30 | PsiFile file = e.getData(DataKeys.PSI_FILE); 31 | 32 | if (!Helpers.isLocalizedStringsFile(file)) 33 | e.getPresentation().setVisible(false); 34 | } 35 | 36 | 37 | private static class SortStringsCommand implements Runnable { 38 | 39 | private XmlFile mReferenceFile; 40 | private XmlFile mTargetFile; 41 | 42 | private SortStringsCommand(XmlFile where, XmlFile reference) { 43 | mTargetFile = where; 44 | mReferenceFile = reference; 45 | } 46 | 47 | @Override 48 | public void run() { 49 | List referenceElements = getTagsAndComments(mReferenceFile); 50 | List targetElements = getTagsAndComments(mTargetFile); 51 | 52 | // Create maps of pairs to ease element search by name 53 | Map referenceMap = new HashMap(); 54 | Map targetMap = new HashMap(); 55 | for (int i = 0; i < referenceElements.size(); i++) { 56 | PsiElement element = referenceElements.get(i); 57 | referenceMap.put(getElementName(element), element); 58 | } 59 | for (int i = 0; i < targetElements.size(); i++) { 60 | PsiElement element = targetElements.get(i); 61 | targetMap.put(getElementName(element), element); 62 | } 63 | 64 | int targetIndex = 0; 65 | for (int i = 0; i < Math.min(referenceElements.size(), targetElements.size()); i++) { 66 | String referenceName = getElementName(referenceElements.get(i)); 67 | String targetName = getElementName(targetElements.get(targetIndex)); 68 | 69 | if (referenceName.equals(targetName)) { 70 | targetIndex++; 71 | continue; 72 | } 73 | 74 | PsiElement replace = targetMap.get(referenceName); 75 | if (replace == null) 76 | continue; 77 | 78 | int replaceIndex = targetElements.indexOf(replace); 79 | swapElements(targetElements, targetMap, targetIndex, replaceIndex); 80 | targetIndex++; 81 | } 82 | } 83 | 84 | private String getElementName(PsiElement element) { 85 | if (element instanceof XmlTag) 86 | return ((XmlTag) element).getAttributeValue(Helpers.ATTRIBUTE_NAME); 87 | else return element.getText(); 88 | } 89 | 90 | private List getTagsAndComments(XmlFile file) { 91 | List result = new ArrayList(); 92 | PsiElement[] elements = file.getRootTag().getChildren(); 93 | for (PsiElement element : elements) { 94 | if (element instanceof XmlTag || element instanceof XmlComment) 95 | result.add(element); 96 | } 97 | 98 | return result; 99 | } 100 | 101 | private void swapElements(List elements, Map map, int index1, int index2) { 102 | PsiElement element1 = elements.get(index1); 103 | PsiElement element2 = elements.get(index2); 104 | PsiElement parent = element1.getParent(); 105 | 106 | // Swap elements in the PSI tree 107 | PsiElement newElement1 = parent.addAfter(element2, element1); 108 | PsiElement newElement2 = parent.addAfter(element1, element2); 109 | element1.delete(); 110 | element2.delete(); 111 | 112 | // Swap elements in the provided list 113 | elements.set(index1, newElement1); 114 | elements.set(index2, newElement2); 115 | 116 | // Add new elements to the map 117 | map.put(getElementName(newElement1), newElement1); 118 | map.put(getElementName(newElement2), newElement2); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/com/civ/stringsTools/AddMissingStringsAction.java: -------------------------------------------------------------------------------- 1 | package com.civ.stringsTools; 2 | 3 | import com.intellij.openapi.actionSystem.AnAction; 4 | import com.intellij.openapi.actionSystem.AnActionEvent; 5 | import com.intellij.openapi.actionSystem.DataKeys; 6 | import com.intellij.openapi.command.WriteCommandAction; 7 | import com.intellij.psi.*; 8 | import com.intellij.psi.impl.source.tree.PsiPlainTextImpl; 9 | import com.intellij.psi.impl.source.xml.XmlCommentImpl; 10 | import com.intellij.psi.xml.XmlComment; 11 | import com.intellij.psi.xml.XmlFile; 12 | import com.intellij.psi.xml.XmlTag; 13 | 14 | import java.util.ArrayList; 15 | import java.util.HashSet; 16 | import java.util.List; 17 | import java.util.Set; 18 | 19 | /** 20 | * Created by kiva on 5/31/14. 21 | */ 22 | public class AddMissingStringsAction extends AnAction { 23 | 24 | @Override 25 | public void actionPerformed(AnActionEvent e) { 26 | XmlFile currentFile = (XmlFile) e.getData(DataKeys.PSI_FILE); 27 | XmlFile defaultFile = (XmlFile) Helpers.getDefaultStringsFile(currentFile); 28 | 29 | if (currentFile == defaultFile) { 30 | List localizedFiles = getLocalizedStringsFiles(currentFile); 31 | WriteCommandAction.runWriteCommandAction(e.getProject(), 32 | new AddMissingStringsFromMultipleSourcesCommand(localizedFiles, defaultFile)); 33 | } else { 34 | WriteCommandAction.runWriteCommandAction(e.getProject(), new AddMissingStringsCommand(defaultFile, 35 | currentFile)); 36 | } 37 | } 38 | 39 | @Override 40 | public void update(AnActionEvent e) { 41 | PsiFile file = e.getData(DataKeys.PSI_FILE); 42 | if (!Helpers.isStringsFile(file)) 43 | e.getPresentation().setEnabled(false); 44 | } 45 | 46 | private static List getLocalizedStringsFiles(PsiFile currentFile) { 47 | PsiDirectory currentDir = currentFile.getContainingDirectory(); 48 | if (currentDir == null) 49 | return null; 50 | 51 | PsiDirectory resDir = currentDir.getParentDirectory(); 52 | if (resDir == null) 53 | return null; 54 | 55 | List result = new ArrayList(); 56 | PsiDirectory[] subdirs = resDir.getSubdirectories(); 57 | for (PsiDirectory subdir : subdirs) { 58 | if (subdir.getName().startsWith(Helpers.LOCALIZED_VALUES_DIR_NAME)) { 59 | XmlFile localizedFile = (XmlFile) subdir.findFile(Helpers.STRINGS_FILE_NAME); 60 | if (localizedFile != null) 61 | result.add(localizedFile); 62 | } 63 | } 64 | 65 | return result; 66 | } 67 | 68 | private static class AddMissingStringsCommand implements Runnable { 69 | 70 | private XmlFile mSource; 71 | 72 | private XmlFile mDestination; 73 | private String mLocalizationName; 74 | 75 | private AddMissingStringsCommand(XmlFile source, XmlFile destination) { 76 | this.mSource = source; 77 | this.mDestination = destination; 78 | 79 | String dirName = destination.getContainingDirectory().getName(); 80 | mLocalizationName = dirName.equals(Helpers.DEFAULT_VALUES_DIR_NAME) ? "def" : dirName.substring(7); 81 | } 82 | 83 | @Override 84 | public void run() { 85 | Set existingNames = getStringsNames(mDestination); 86 | XmlTag[] tags = mSource.getRootTag().getSubTags(); 87 | 88 | for (XmlTag tag : tags) { 89 | String name = tag.getAttributeValue(Helpers.ATTRIBUTE_NAME); 90 | if (name != null && !existingNames.contains(name)) { 91 | XmlTag resultTag = mDestination.getRootTag().addSubTag(tag, false); 92 | localizeTag(resultTag); 93 | } 94 | } 95 | } 96 | 97 | private Set getStringsNames(XmlFile file) { 98 | Set names = new HashSet(); 99 | XmlTag[] tags = file.getRootTag().getSubTags(); 100 | 101 | for (XmlTag tag : tags) { 102 | String name = tag.getAttributeValue(Helpers.ATTRIBUTE_NAME); 103 | if (name != null) 104 | names.add(name); 105 | } 106 | 107 | return names; 108 | } 109 | 110 | private void localizeTag(XmlTag tag) { 111 | String name = tag.getName(); 112 | if (name.equals(Helpers.TAG_STRING)) 113 | localizeStringTag(tag); 114 | else if (name.equals(Helpers.TAG_STRING_ARRAY) || name.equals(Helpers.TAG_PLURALS)) 115 | localizeStringArrayTag(tag); 116 | // Do nothing for other tags 117 | } 118 | 119 | private void localizeStringTag(XmlTag tag) { 120 | // Do nothing if string tag contains subtags 121 | if (tag.getSubTags().length > 0) 122 | return; 123 | 124 | String text = tag.getValue().getText(); 125 | String trimmedText = tag.getValue().getTrimmedText(); 126 | boolean isCdata = text.startsWith(" mSources; 144 | private XmlFile mDestination; 145 | 146 | private AddMissingStringsFromMultipleSourcesCommand(List sources, XmlFile destination) { 147 | mSources = sources; 148 | mDestination = destination; 149 | } 150 | 151 | @Override 152 | public void run() { 153 | if (mSources == null || mSources.isEmpty()) 154 | return; 155 | 156 | for (XmlFile source : mSources) 157 | new AddMissingStringsCommand(source, mDestination).run(); 158 | } 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /strings.xml tools.ipr: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 159 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /strings.xml tools.iws: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 90 | 91 | 96 | 97 | 98 | 106 | 107 | 108 | 109 | 110 | 111 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | Android Lint 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 164 | 165 | 166 | 167 | 170 | 171 | 174 | 175 | 176 | 177 | 180 | 181 | 184 | 185 | 188 | 189 | 192 | 193 | 194 | 195 | 198 | 199 | 202 | 203 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 260 | 261 | 262 | 280 | 281 | 282 | 301 | 302 | 303 | 310 | 311 | 312 | 325 | 326 | 327 | 334 | 337 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 404 | 405 | 406 | 407 | 408 | 415 | 416 | 417 | 418 | 436 | 443 | 444 | 459 | 460 | 461 | 473 | 474 | 475 | 476 | 477 | localhost 478 | 5050 479 | 480 | 481 | 482 | 483 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 1401050661124 492 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 532 | 535 | 536 | 537 | 539 | 540 | 541 | 543 | 544 | 545 | 546 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 857 | 858 | 859 | 860 | 861 | 862 | Detection 863 | 864 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 880 | 881 | 882 | 883 | 884 | 885 | Android Studio AI-141.2024585 886 | 887 | 892 | 893 | 894 | 895 | 896 | 897 | strings-xml-tools 898 | 899 | 905 | 906 | 907 | 908 | 909 | 910 | 1.7 911 | 912 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 928 | 929 | 930 | 931 | 932 | 933 | --------------------------------------------------------------------------------