55import java .io .File ;
66import java .io .IOException ;
77import java .nio .file .Files ;
8+ import java .util .Comparator ;
89import java .util .HashMap ;
910import java .util .Map ;
1011import java .util .TreeMap ;
1314
1415import javafx .collections .FXCollections ;
1516import javafx .collections .ObservableList ;
17+ import javafx .scene .shape .Path ;
1618
1719public class Data {
1820
1921 public static final String TRANSLATION_KEY = "translation key" ;
2022
21- /**key, language file, value*/
22- private Map <String , Map <String , String >> translations ;
23- /**language file, key, value*/
24- private Map <String , Map <String , String >> translationFiles ;
25- private static final Pattern translationPattern = Pattern .compile ("(.*?)=(.*)" );
23+ private static final Pattern TRANSLATION_PATTERN = Pattern .compile ("(.*?)=(.*)" );
24+
25+ private Map <String , Boolean > editedFiles ;
26+ private ObservableList <Map <String , String >> translationList ;
2627
2728 public Data () {
28- translations = new HashMap <>();
29- translationFiles = new HashMap <> ();
29+ editedFiles = new HashMap <>();
30+ translationList = FXCollections . observableArrayList ();
3031 }
3132
3233 public void load (File [] langFiles ) {
3334 if (langFiles == null || langFiles .length == 0 )
3435 return ;
3536
36- translations .clear ();
37- translationFiles .clear ();
37+ Map <String , Map <String , String >> translations = new TreeMap <>();
3838
3939 for (File file : langFiles ) {
40- translationFiles .put (file .getName (), new TreeMap <>() );
41- try (BufferedReader reader = Files .newBufferedReader (file .toPath ())){
40+ editedFiles .put (file .getName (), false );
41+ try (BufferedReader reader = Files .newBufferedReader (file .toPath ())) {
4242 while (reader .ready ()) {
4343 String line = reader .readLine ();
44- Matcher m = translationPattern .matcher (line );
44+ Matcher m = TRANSLATION_PATTERN .matcher (line );
4545 if (m .matches ()) {
4646 String key = m .group (1 );
4747 Map <String , String > values = translations .get (key );
@@ -50,71 +50,72 @@ public void load(File[] langFiles) {
5050 translations .put (m .group (1 ), values );
5151 }
5252 values .put (file .getName (), m .group (2 ));
53- translationFiles .get (file .getName ()).put (key , m .group (2 ));
5453 }
5554 }
5655 } catch (IOException e ) {
5756 e .printStackTrace ();
5857 }
5958 }
60- System .out .println ("================================" );
61- translations .forEach ((key , values ) -> System .out .println (key + "=" + values ));
62- System .out .println ("================================" );
63- translationFiles .forEach ((key , values ) -> {
64- System .out .println (key + ":" );
65- values .forEach ((trKey , trValue ) -> System .out .println (trKey + "=" + trValue ));
66- System .out .println ("-------------------------" );
67- });
59+
60+ this .translationList = generateDataInMap (translations );
6861 }
6962
7063 public void save (File folder ) {
71- translationFiles .forEach ((file , values ) -> {
72- try (BufferedWriter writer = Files .newBufferedWriter (new File (folder , file ).toPath ())) {
73- for (String key : values .keySet ()) {
74- writer .write (key + "=" + values .get (key ));
75- writer .newLine ();
64+ editedFiles .forEach ((file , edited ) -> {
65+ if (edited ) {
66+ try (BufferedWriter writer = Files .newBufferedWriter (new File (folder , file ).toPath ())) {
67+ for (Map <String , String > pairs : translationList ) {
68+ if (pairs .get (file ) != null ) {
69+ writer .write (pairs .get (TRANSLATION_KEY ) + "=" + pairs .get (file ));
70+ writer .newLine ();
71+ }
72+ }
73+ } catch (IOException e ) {
74+ e .printStackTrace ();
7675 }
77- } catch (IOException e ) {
78- e .printStackTrace ();
76+ editedFiles .put (file , false );
7977 }
8078 });
8179 }
8280
83- public ObservableList <Map <String , String >> generateDataInMap () {
81+ private ObservableList <Map <String , String >> generateDataInMap (Map < String , Map < String , String >> translations ) {
8482 ObservableList <Map <String , String >> allData = FXCollections .observableArrayList ();
8583 translations .forEach ((translatKey , values ) -> {
8684 Map <String , String > dataRow = new HashMap <>();
87-
88- System .out .println ("\n " + translatKey );
8985
9086 dataRow .put (TRANSLATION_KEY , translatKey );
9187 values .forEach ((translatFile , translatValue ) -> {
9288 dataRow .put (translatFile , translatValue );
93- System .out .println (translatFile + ":" + translatValue );
9489 });
9590
9691 allData .add (dataRow );
9792 });
98- System .out .println (allData );
9993 return allData ;
10094 }
10195
102- public void updateTranslationKey (String oldKey , String newKey ) {
103- Map <String , String > tr = translations .get (oldKey );
104- translations .remove (oldKey );
105- translations .put (newKey , tr );
106- translationFiles .forEach ((file , pair ) -> {
107- String value = pair .get (oldKey );
108- pair .remove (oldKey );
109- pair .put (newKey , value );
110- });
96+ public ObservableList <Map <String , String >> getTranslationList () {
97+ return this .translationList ;
98+ }
99+
100+ public void updateTranslationKey (String oldKey , String newKey , int selectedRow ) {
101+ translationList .get (selectedRow ).put (TRANSLATION_KEY , newKey );
102+ translationList .get (selectedRow ).keySet ().stream ().filter (s -> !s .equals (TRANSLATION_KEY )).forEach (l -> editedFiles .put (l , true ));
103+ }
104+
105+ public void updateTranslation (int selectedRow , String newValue , String lang ) {
106+ editedFiles .put (lang , true );
107+ translationList .get (selectedRow ).put (lang , newValue );
108+ }
109+
110+ public void removeTranslation (int selectedRow ) {
111+ translationList .remove (selectedRow ).keySet ().stream ().filter (s -> !s .equals (TRANSLATION_KEY )).forEach ((lang ) -> editedFiles .put (lang , true ));
111112 }
112113
113- public void updateTranslation (String key , String newValue , String lang ) {
114- translations . get ( key ). put ( lang , newValue );
115- translationFiles . get ( lang ). put (key , newValue );
116- System . out . println ( translations );
117- System . out . println ( translationFiles );
114+ public void addTranslation (String key ) {
115+ Map < String , String > newMap = new HashMap <>( );
116+ newMap . put (Data . TRANSLATION_KEY , key );
117+ translationList . add ( newMap );
118+ translationList . sort (( o1 , o2 ) -> o1 . get ( TRANSLATION_KEY ). compareTo ( o2 . get ( TRANSLATION_KEY )) );
118119 }
119120
120121}
0 commit comments