├── .DS_Store
├── .gitignore
├── LICENSE
├── Localized.iml
├── README.md
├── out
├── .DS_Store
├── artifacts
│ └── .DS_Store
└── production
│ └── Localized
│ ├── META-INF
│ ├── Localized.kotlin_module
│ └── MANIFEST.MF
│ └── az
│ └── rasul
│ └── localized
│ └── fxml
│ └── main.fxml
├── screenshots
└── Screen Shot 2020-04-28 at 17.18.41.png
└── src
├── META-INF
└── MANIFEST.MF
└── az
└── rasul
├── .DS_Store
└── localized
├── Main.java
├── controller
└── MainController.kt
├── fxml
└── main.fxml
├── localizers
├── AndroidStrings.kt
├── CSV.kt
└── LocalizableStrings.kt
└── model
└── Data.kt
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agarasul/Localized/e98d5fc030697a18174016f0ff82d95835d0b022/.DS_Store
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 | /.idea
13 | ./out
14 | # Package Files #
15 | *.jar
16 | *.war
17 | *.nar
18 | *.ear
19 | *.zip
20 | *.tar.gz
21 | *.rar
22 |
23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
24 | hs_err_pid*
25 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/Localized.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Localized
2 | An application to convert strings to diffirent formats
3 |
4 |
5 | This app will help developers to convert strings from application in one platform to another.
6 |
7 | To use this app, Java 8 RE must be installed.
8 |
9 | Features:
10 |
11 | - Show parsed strings in user friendly interface
12 | - Work with multiple formats:
13 | - Android strings.xml including their placeholders
14 | - Apple Localizable.strings including their placeholders
15 | - CSV
16 |
17 |
18 | Link for download:
19 | [Localized.jar](https://github.com/agarasul/Localized/releases/download/1.0.1/Localized.jar)
20 |
21 |
22 | 
23 |
24 |
25 |
26 |
27 |
28 | License
29 | ----
30 | ```
31 | Copyright 2019 Rasul Aghakishiyev
32 |
33 | Licensed under the Apache License, Version 2.0 (the "License");
34 | you may not use this file except in compliance with the License.
35 | You may obtain a copy of the License at
36 |
37 | http://www.apache.org/licenses/LICENSE-2.0
38 |
39 | Unless required by applicable law or agreed to in writing, software
40 | distributed under the License is distributed on an "AS IS" BASIS,
41 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 | See the License for the specific language governing permissions and
43 | limitations under the License.
44 | ```
45 |
46 |
--------------------------------------------------------------------------------
/out/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agarasul/Localized/e98d5fc030697a18174016f0ff82d95835d0b022/out/.DS_Store
--------------------------------------------------------------------------------
/out/artifacts/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agarasul/Localized/e98d5fc030697a18174016f0ff82d95835d0b022/out/artifacts/.DS_Store
--------------------------------------------------------------------------------
/out/production/Localized/META-INF/Localized.kotlin_module:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/out/production/Localized/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Main-Class: az.rasul.localized.Main
3 |
4 |
--------------------------------------------------------------------------------
/out/production/Localized/az/rasul/localized/fxml/main.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
21 |
22 |
23 |
28 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/screenshots/Screen Shot 2020-04-28 at 17.18.41.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agarasul/Localized/e98d5fc030697a18174016f0ff82d95835d0b022/screenshots/Screen Shot 2020-04-28 at 17.18.41.png
--------------------------------------------------------------------------------
/src/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Main-Class: az.rasul.localized.Main
3 |
4 |
--------------------------------------------------------------------------------
/src/az/rasul/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agarasul/Localized/e98d5fc030697a18174016f0ff82d95835d0b022/src/az/rasul/.DS_Store
--------------------------------------------------------------------------------
/src/az/rasul/localized/Main.java:
--------------------------------------------------------------------------------
1 | package az.rasul.localized;
2 |
3 | import javafx.application.Application;
4 | import javafx.application.Platform;
5 | import javafx.fxml.FXMLLoader;
6 | import javafx.scene.Parent;
7 | import javafx.scene.Scene;
8 | import javafx.stage.Stage;
9 |
10 | public class Main extends Application {
11 |
12 |
13 | @Override
14 | public void start(Stage primaryStage) throws Exception {
15 | Platform.setImplicitExit(true);
16 | Parent root = FXMLLoader.load(getClass().getResource("fxml/main.fxml"));
17 | primaryStage.setTitle("Localized");
18 | primaryStage.setScene(new Scene(root, 800, 530));
19 | primaryStage.setMinHeight(320f);
20 | primaryStage.setMinWidth(620f);
21 | primaryStage.show();
22 |
23 |
24 | primaryStage.setOnCloseRequest((ae) -> {
25 | Platform.exit();
26 | System.exit(0);
27 | });
28 |
29 |
30 | }
31 |
32 |
33 | public static void main(String[] args) {
34 | launch(args);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/az/rasul/localized/controller/MainController.kt:
--------------------------------------------------------------------------------
1 | package az.rasul.localized.controller
2 |
3 | import az.rasul.localized.model.Data
4 | import az.rasul.localized.localizers.AndroidStrings
5 | import az.rasul.localized.localizers.CSV
6 | import az.rasul.localized.localizers.LocalizableStrings
7 | import javafx.collections.FXCollections
8 | import javafx.event.ActionEvent
9 | import javafx.fxml.FXML
10 | import javafx.fxml.Initializable
11 | import javafx.scene.control.*
12 | import javafx.scene.control.cell.PropertyValueFactory
13 | import javafx.scene.layout.AnchorPane
14 | import javafx.stage.FileChooser
15 | import java.io.File
16 | import java.net.URL
17 | import java.util.*
18 |
19 |
20 | class MainController : Initializable {
21 |
22 | // private val parsedData = arrayListOf()
23 |
24 |
25 | private val parsedData = FXCollections.observableArrayList()
26 |
27 | private var localizableFile: File? = null
28 |
29 | @FXML
30 | private var mainPane: AnchorPane? = null
31 |
32 | @FXML
33 | private var filePathTextField: TextField? = null
34 |
35 | @FXML
36 | private var browseBtn: Button? = null
37 |
38 | @FXML
39 | private var exportBtn: Button? = null
40 |
41 | @FXML
42 | private var tableView: TableView? = null
43 |
44 | @FXML
45 | private var totalCountLabel: Label? = null
46 |
47 |
48 | @FXML
49 | private val androidRdb: RadioButton? = null
50 |
51 | @FXML
52 | private val iosRdb: RadioButton? = null
53 |
54 |
55 | @FXML
56 | private val csvRdb: RadioButton? = null
57 |
58 | @FXML
59 | private var radioGroup = ToggleGroup()
60 |
61 |
62 | private var type = ""
63 |
64 |
65 | override fun initialize(location: URL?, resources: ResourceBundle?) {
66 | this.browseBtn?.setOnAction(this::onBrowseBtnClicked)
67 | // this.convertBtn?.setOnAction(this::onConvertBtnClicked)
68 | this.exportBtn?.setOnAction(this::onExportBtnClicked)
69 | radioGroup.selectedToggleProperty().addListener { observable, oldValue, newValue ->
70 | type = (newValue as RadioButton).id
71 | }
72 | setupTableView()
73 | }
74 |
75 |
76 | private fun setupTableView() {
77 | val column1: TableColumn = TableColumn("Key")
78 | column1.cellValueFactory = PropertyValueFactory("key")
79 |
80 | val column2: TableColumn = TableColumn("Value")
81 | column2.cellValueFactory = PropertyValueFactory("value")
82 |
83 |
84 | tableView?.columns?.addAll(column1, column2)
85 |
86 | column1.prefWidthProperty().bind(tableView!!.widthProperty()!!.divide(3)) // w * 1/4
87 | column2.prefWidthProperty().bind(tableView!!.widthProperty()!!.divide(1.3)) // w * 1/4
88 |
89 | tableView?.items = parsedData
90 | }
91 |
92 |
93 | private fun onExportBtnClicked(event: ActionEvent) {
94 | if (parsedData.isEmpty()) {
95 | totalCountLabel?.text = ""
96 | filePathTextField?.text = ""
97 | parsedData.clear()
98 | tableView?.items?.setAll(parsedData)
99 | localizableFile = null
100 | showErrorAlert(message = "Please select the file that you want to convert")
101 | } else {
102 | val fileChooser = FileChooser()
103 | fileChooser.title = "Select place to save converted file"
104 | val fileToSave: File?
105 |
106 | val window = mainPane?.scene?.window
107 |
108 | when (type) {
109 | "androidRdb" -> {
110 | fileChooser.initialFileName = "strings.xml"
111 | fileToSave = fileChooser.showSaveDialog(window)
112 | }
113 | "iosRdb" -> {
114 | fileChooser.initialFileName = "Localizable.strings"
115 | fileToSave = fileChooser.showSaveDialog(window)
116 | }
117 | "csvRdb" -> {
118 | fileChooser.initialFileName = "Localized.csv"
119 | fileToSave = fileChooser.showSaveDialog(window)
120 | }
121 | else -> {
122 | fileToSave = null
123 | localizableFile = null
124 | showErrorAlert(message = "Please select the export format!")
125 | }
126 | }
127 | if (fileToSave != null) {
128 |
129 | var isSuccess = false
130 | try {
131 | when (type) {
132 | "androidRdb" -> {
133 | AndroidStrings.convertToXml(fileToSave, parsedData)
134 | }
135 | "iosRdb" -> {
136 | LocalizableStrings.convertToIOSFormat(fileToSave, parsedData)
137 | }
138 | "csvRdb" -> {
139 | CSV.convert(fileToSave, parsedData)
140 | }
141 | }
142 | } catch (e: Exception) {
143 | isSuccess = false
144 | }
145 |
146 | if (isSuccess) {
147 | showSuccessAlert(filePath = fileToSave.absolutePath)
148 | } else {
149 | showErrorAlert()
150 | }
151 |
152 | }
153 | }
154 |
155 | }
156 |
157 |
158 | private fun onBrowseBtnClicked(event: ActionEvent) {
159 | val fileChooser = FileChooser()
160 | val extFilter = FileChooser.ExtensionFilter("Apple (*.strings), Android (*.xml), .csv", "*.xml", "*.csv", "*.strings")
161 | fileChooser.extensionFilters.add(extFilter)
162 |
163 | localizableFile = fileChooser.showOpenDialog(mainPane?.scene?.window)
164 | filePathTextField?.text = localizableFile?.absolutePath
165 |
166 | if (localizableFile != null) {
167 | val path = localizableFile!!.absolutePath
168 |
169 | var extension = ""
170 | if (path.contains(".")) {
171 | extension = path.substring(path.lastIndexOf("."))
172 | }
173 |
174 | try {
175 | parsedData.clear()
176 |
177 | when (extension) {
178 | ".xml" -> {
179 | parsedData.addAll(AndroidStrings.parseFromFromXml(localizableFile!!))
180 | }
181 | ".strings" -> {
182 | parsedData.addAll(LocalizableStrings.parseFromString(localizableFile!!))
183 | }
184 | ".csv" -> {
185 | parsedData.addAll(CSV.parseFromFromCSV(localizableFile!!))
186 | }
187 | }
188 | totalCountLabel?.text = "Total count = ${parsedData.size}"
189 |
190 | } catch (e: Exception) {
191 | e.printStackTrace()
192 | totalCountLabel?.text = ""
193 | filePathTextField?.text = ""
194 | parsedData.clear()
195 | tableView?.items?.setAll(parsedData)
196 | localizableFile = null
197 | showErrorAlert(message = "Parse error. Please ensure that your file is correct and try again!")
198 | }
199 | }
200 | }
201 |
202 |
203 | private fun showSuccessAlert(filePath: String) {
204 | val alert = Alert(Alert.AlertType.INFORMATION)
205 | alert.title = "Success"
206 | alert.contentText = "Successfully converted. Converted file path:\n$filePath"
207 | alert.show()
208 | }
209 |
210 | private fun showErrorAlert(message: String = "Something went wrong. Please try again") {
211 | val alert = Alert(Alert.AlertType.ERROR)
212 | alert.title = "Error"
213 | alert.contentText = message
214 | alert.show()
215 | }
216 | }
--------------------------------------------------------------------------------
/src/az/rasul/localized/fxml/main.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
21 |
22 |
23 |
28 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/src/az/rasul/localized/localizers/AndroidStrings.kt:
--------------------------------------------------------------------------------
1 | package az.rasul.localized.localizers
2 |
3 | import az.rasul.localized.model.Data
4 | import org.w3c.dom.Element
5 | import org.w3c.dom.Node
6 | import java.io.File
7 | import javax.xml.parsers.DocumentBuilderFactory
8 | import javax.xml.transform.OutputKeys
9 | import javax.xml.transform.TransformerFactory
10 | import javax.xml.transform.dom.DOMSource
11 | import javax.xml.transform.stream.StreamResult
12 |
13 |
14 | object AndroidStrings {
15 | fun parseFromFromXml(file: File): List {
16 | val parsedData = arrayListOf()
17 | val dbFactory = DocumentBuilderFactory.newInstance()
18 | val dBuilder = dbFactory.newDocumentBuilder()
19 | val doc = dBuilder.parse(file)
20 |
21 | doc.documentElement.normalize()
22 |
23 |
24 | val nList = doc.getElementsByTagName("string")
25 |
26 |
27 | for (i in 0 until nList.length) {
28 | val nNode = nList.item(i)
29 | if (nNode.nodeType == Node.ELEMENT_NODE) {
30 |
31 | val element = nNode as Element
32 |
33 |
34 | val key = element.getAttribute("name")
35 | val value = element.firstChild.textContent
36 |
37 | parsedData.add(Data(key, value))
38 | }
39 |
40 | }
41 | return parsedData
42 | }
43 |
44 |
45 | /**
46 | * The method that parse strings.xml
47 | * @param file strings.xml file
48 | * @return List with parsed data
49 | */
50 |
51 | fun convertToXml(file: File, parsedData: List) {
52 | val docFactory = DocumentBuilderFactory.newInstance()
53 | val docBuilder = docFactory.newDocumentBuilder()
54 |
55 |
56 | val doc = docBuilder.newDocument()
57 |
58 |
59 | val resourcesElement = doc.createElement("resources")
60 | doc.appendChild(resourcesElement)
61 | parsedData.forEach {
62 | val element = doc.createElement("string")
63 | val nameAttr = doc.createAttribute("name")
64 | nameAttr.value = it.key
65 | element.setAttributeNode(nameAttr)
66 |
67 | if (it.value.contains("'")) {
68 | it.value = it.value.replace("'", "\\'")
69 | }
70 |
71 | element.appendChild(doc.createTextNode(replacePlaceHolders(it.value)))
72 | resourcesElement.appendChild(element)
73 | }
74 |
75 | val transformerFactory = TransformerFactory.newInstance()
76 | val transformer = transformerFactory.newTransformer()
77 | transformer.setOutputProperty(OutputKeys.METHOD, "xml")
78 | transformer.setOutputProperty(OutputKeys.INDENT, "yes")
79 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
80 |
81 | val source = DOMSource(doc)
82 | val result = StreamResult(file)
83 | transformer.transform(source, result)
84 | }
85 |
86 | private fun replacePlaceHolders(value: String): String {
87 | var newString = value
88 | if (value.contains("%d")) {
89 | newString = value.replace("%d", "%\$d")
90 | }
91 | if (value.contains("%@")) {
92 | newString = value.replace("%@", "%\$s")
93 | }
94 | return newString
95 | }
96 |
97 | }
--------------------------------------------------------------------------------
/src/az/rasul/localized/localizers/CSV.kt:
--------------------------------------------------------------------------------
1 | package az.rasul.localized.localizers
2 |
3 | import az.rasul.localized.model.Data
4 | import java.io.File
5 | import java.nio.charset.Charset
6 | import java.nio.charset.StandardCharsets
7 | import java.nio.file.Files
8 |
9 | object CSV {
10 |
11 |
12 |
13 | /**
14 | * The method that parse CSV file
15 | * @param file CSV file
16 | * @return List with parsed data
17 | */
18 | fun parseFromFromCSV(file: File): List {
19 | val parsedData = arrayListOf()
20 | val content = String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)
21 |
22 | content.lines().forEachIndexed { index, it ->
23 |
24 | if (!it.isNullOrBlank()) {
25 | val keyEndIndex = it.indexOfFirst { it == ',' }
26 |
27 | val key = it.substring(0, keyEndIndex)
28 | val value = it.substring(keyEndIndex + 1, it.count())
29 |
30 | parsedData.add(Data(key, value))
31 | }
32 | }
33 | return parsedData
34 | }
35 |
36 |
37 |
38 | fun convert(file: File, parsedData: List) {
39 | var str = ""
40 | parsedData.forEach {
41 | str += "\"${it.key}\",\"${it.value}\"" + System.lineSeparator()
42 | }
43 | file.writeText(str, Charset.defaultCharset())
44 | }
45 |
46 |
47 |
48 |
49 | }
--------------------------------------------------------------------------------
/src/az/rasul/localized/localizers/LocalizableStrings.kt:
--------------------------------------------------------------------------------
1 | package az.rasul.localized.localizers
2 |
3 | import az.rasul.localized.model.Data
4 | import java.io.File
5 | import java.nio.charset.Charset
6 | import java.nio.charset.StandardCharsets
7 | import java.nio.file.Files
8 | import java.util.regex.Pattern
9 |
10 |
11 | object LocalizableStrings {
12 |
13 |
14 | const val PATTERN = "[%][\\d]?[0-9]?[\$][a-z]"
15 |
16 | /**
17 | * The method that parse Localizable.strings file
18 | * @param file Localizable.strings file
19 | * @return List with parsed data
20 | */
21 | fun parseFromString(file: File): List {
22 | val parsedData = arrayListOf()
23 | val content = String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)
24 |
25 | val aaa = content.lines()
26 | aaa.forEachIndexed { index, it ->
27 | var newLine = it
28 |
29 | if (newLine.startsWith("//")) {
30 | } else {
31 |
32 | if (!newLine.isNullOrBlank()) {
33 | while (newLine.startsWith(" ")) {
34 | newLine = newLine.substring(1)
35 | }
36 |
37 | if (!newLine.endsWith(";")) {
38 | newLine += ";"
39 | }
40 |
41 | val equalSignIndex = newLine.indexOfFirst { it == '=' }
42 |
43 | val keyString = newLine.substring(0, equalSignIndex) // Обрезаем строку от начала до знака =
44 |
45 | val keyEndIndex = keyString.indexOfLast { it == '"' } // Находим последнюю ковычку в строке
46 |
47 | var key = newLine.substring(1, keyEndIndex) // Конечная строка (Ключ)
48 |
49 | // while (key.contains(".")) {
50 | // key = key.replace(".", "")
51 | // }
52 |
53 | val valueString = newLine.substring(equalSignIndex, newLine.count())
54 | val valueStartIndex = equalSignIndex + valueString.indexOfFirst { it == '"' } + 1 // Находим позицию начала значения
55 | val value = newLine.substring(valueStartIndex, newLine.count() - 2)
56 |
57 | parsedData.add(Data(
58 | key = key,
59 | value = value
60 | ))
61 | }
62 | }
63 |
64 | }
65 | return parsedData
66 | }
67 |
68 |
69 |
70 | fun convertToIOSFormat(file: File, parsedData: List) {
71 | var str = ""
72 | parsedData.forEach {
73 | str += "\"${it.key}\" = \"${replacePlaceHolders(it.value)}\";" + System.lineSeparator()
74 | }
75 | file.writeText(str, Charset.defaultCharset())
76 | }
77 |
78 |
79 |
80 |
81 |
82 | // [%][\d]?[0-9]?[$][a-z] IOS Pattern String interpolation
83 |
84 |
85 |
86 | private fun replacePlaceHolders(value: String): String {
87 | var newString = value
88 | if (value.contains("%\$d")) {
89 | newString = value.replace("%\$d", "%d")
90 | }
91 | if (value.contains("%\$s")) {
92 | newString = value.replace("%\$s", "%@")
93 | }
94 | return newString
95 | }
96 |
97 |
98 | /**
99 | * Converts placeholders from strings.xml to Localizable.strings format
100 | * @param value Value from strings.xml
101 | */
102 | private fun replacePlaceHolder(value : String) : String {
103 | val m2 = Pattern.compile(PATTERN).matcher(value)
104 | while (m2.find()) {
105 | val placeholder = m2.group()
106 |
107 |
108 |
109 | break
110 | }
111 | }
112 |
113 | }
114 |
115 |
116 |
--------------------------------------------------------------------------------
/src/az/rasul/localized/model/Data.kt:
--------------------------------------------------------------------------------
1 | package az.rasul.localized.model
2 |
3 | data class Data(
4 | var key: String,
5 | var value: String
6 | ) {
7 | override fun toString(): String {
8 |
9 | return "Key = $key Value = $value"
10 | }
11 | }
--------------------------------------------------------------------------------