twine-flutter 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 586677a3df54ba426027489178ca89aaf7afb56237e679c85a9ca22685d5a08c
4
- data.tar.gz: 8fc4f010b07820cd403f27a716e54f83c134178e10fd8ed3e99090bb2fb9278f
3
+ metadata.gz: 15a3ba2b0b13dd5f82be010a87d3bed56fefbfb493268aba7d17ee50e6d955cb
4
+ data.tar.gz: 812a54e3480ab12975c5082055960549552fe080a44803a568ba06698f2f17dc
5
5
  SHA512:
6
- metadata.gz: 346c7568b35680f03335f050a9618d569d1e0b1a2705b333ce2778be9374ad6ffc46800bed4df08fef8bb1167463d83206016736be1780da8edbdae83b423803
7
- data.tar.gz: 502b267c3bc039c7435504efa81ef6c0f2d6ad444295e69b6691253dc24f911046c028eb034709426ba6d298135bb2db3ce87c900de21e5db0d47496050f0f70
6
+ metadata.gz: 261c6208aae7b9d0e609931adb369dd7adac7eeb2332bea965775e408df61a2300adcb4728ea445f59db12a41e97ee1671d536dbda8464a1f69a94695dc7efaa
7
+ data.tar.gz: 1bb62f33803a6ac2beb6103f51baaf2020a3a7d52eff7a7234f94a74bf1b37b6b0423df09e240d40b383cc7b377fc1640ece9662e8682fd88e5e7799dce7fe0a
data/README.md CHANGED
@@ -1,11 +1,141 @@
1
1
  # Twine Flutter formatter plugin
2
2
  This [twine](https://github.com/scelis/twine) plugin provide a formatter for `.arb` files, used by Flutter projects.
3
3
 
4
+ ## Features
5
+
6
+ * Generation of arb files from twine
7
+ * Consumation of localized arb files into twine
8
+ * Support of arb comments
9
+ * Basic support of arb placeholders
10
+
11
+ ## Generation of localizations
12
+ To generate a localizations `.arb` file use twine `generate-localization-file` command.
13
+
14
+ Example:
15
+ `twine generate-localization-file twine.txt app_en.arb`
16
+
17
+ _Input twine file:_
18
+ ```
19
+ [[Section A]]
20
+ [key1]
21
+ en = Text with comment for translators
22
+ comment = comment key1
23
+ [key2]
24
+ en = Simple text
25
+ ```
26
+
27
+ _Output arb file:_
28
+ ```json
29
+ {
30
+ "@@locale": "en",
31
+
32
+ "key1": "Text with comment for translators",
33
+ "@key1": {
34
+ "description": "comment key1"
35
+ },
36
+ "key2": "Simple text"
37
+ }
38
+ ```
39
+
40
+ ## Consumation of localizations
41
+ To consume a twin localizations file to an `.arb` file use twine `consume-localization-file` command.
42
+ If you want to consume comments add `-c` option to the twine command.
43
+
44
+ Example:
45
+ `twine consume-localization-file twine.txt app_en.arb -c`
46
+
47
+ _Input arb file:_
48
+ ```json
49
+ {
50
+ "@@locale": "en",
51
+
52
+ "key1": "Text with comment for translators",
53
+ "@key1": {
54
+ "description": "comment key1"
55
+ },
56
+ "key2": "Simple text"
57
+ }
58
+ ```
59
+
60
+ _Output twine file:_
61
+ ```
62
+ [[Uncategorized]]
63
+ [key1]
64
+ en = Text with comment for translators
65
+ comment = comment key1
66
+ [key2]
67
+ en = Simple text
68
+ ```
69
+
70
+ **Warning!** The arb file doesn't contain any reference to sections so all the keys are added to the "Uncategorized" section, when the `-a` option of `consume-localization-file` is used.
71
+
72
+ ## Placeholders
73
+ Flutter provides [extended support](https://localizely.com/flutter-arb/) to placeholders in the localization files.
74
+
75
+ Currently this plugin support a basic use of placeholders automatically creating an untyped placeholder for every string wrapped with curlty braces.
76
+ Are only supported placeholders with lowercase chars, uppercase chars, digits, `.`, `_` and `-`.
77
+
78
+ Example:
79
+
80
+ _Input twine file:_
81
+ ```
82
+ [[Section B]]
83
+ [key3]
84
+ en = Text with {name} placeholder
85
+ [key4]
86
+ en = Text with double placeholders {1} and {2} and a comment
87
+ comment = comment key4
88
+
89
+ [[Section C]]
90
+ [key5]
91
+ en = Placeholders with punctuation: {p_1}, {p-1}, {p.1}
92
+ ```
93
+
94
+ _Output arb file:_
95
+ ```json
96
+ {
97
+ "@@locale": "en",
98
+
99
+ "key3": "Text with {name} placeholder",
100
+ "@key3": {
101
+ "placeholders": {
102
+ "name": {}
103
+ }
104
+ },
105
+ "key4": "Text with double placeholders {1} and {2} and a comment",
106
+ "@key4": {
107
+ "description": "comment key4",
108
+ "placeholders": {
109
+ "1": {},
110
+ "2": {}
111
+ }
112
+ },
113
+
114
+ "key5": "Placeholders with punctuation: {p_1}, {p-1}, {p.1}",
115
+ "@key5": {
116
+ "placeholders": {
117
+ "p_1": {},
118
+ "p-1": {},
119
+ "p.1": {}
120
+ }
121
+ }
122
+ }
123
+ ```
124
+
4
125
  ## Installation
5
126
  Install this gem:
6
127
 
7
- ```gem install twine-flutter```
128
+ ```ruby
129
+ gem install twine-flutter
130
+ ```
131
+
132
+ Then setup twine to use this plugin as described [here](https://github.com/scelis/twine/blob/main/documentation/formatters.md#plugins) adding the following in a twine configuration file.
133
+
134
+ ```ruby
135
+ gems: twine-flutter
136
+ ```
8
137
 
9
- Then setup twine to use it as described [here](https://github.com/scelis/twine/blob/main/documentation/formatters.md#plugins).
138
+ ## TODO
10
139
 
11
- ```gems: twine-flutter```
140
+ * Automatic testing
141
+ * Extended placeholder support
data/lib/formatter.rb CHANGED
@@ -21,18 +21,13 @@ module Twine
21
21
  end
22
22
 
23
23
  def read(io, lang)
24
- begin
25
- require "json"
26
- rescue LoadError
27
- raise Twine::Error.new "You must run `gem install json` in order to read or write flutter files."
28
- end
24
+ require "json"
29
25
 
30
26
  json = JSON.load(io)
31
27
  json.each do |key, value|
32
28
  if key == "@@locale"
33
29
  # Ignore because it represents the file lang
34
30
  elsif key[0,1] == "@"
35
- description_value = "{\n \"description\":\"#{value}\"\n }"
36
31
  if value["description"]
37
32
  set_comment_for_key(key[1..-1], value["description"])
38
33
  end
@@ -67,16 +62,22 @@ module Twine
67
62
  end
68
63
 
69
64
  def format_definition(definition, lang)
70
- [format_key_value(definition, lang), format_comment(definition, lang)].compact.join(",\n")
65
+ [format_key_value(definition, lang), format_comment_and_placeholders(definition, lang)].compact.join(",\n")
71
66
  end
72
67
 
73
68
  def key_value_pattern
74
69
  " \"%{key}\": \"%{value}\""
75
70
  end
76
71
 
77
- def format_comment(definition, lang)
78
- " \"@#{definition.key}\": {\n \"description\": \"#{definition.comment}\"\n }" if definition.comment
79
- end
72
+ def format_comment_and_placeholders(definition, lang)
73
+ placeholdersScan = definition.translation_for_lang(lang).scan(/{[a-zA-Z0-9\-\_\.]+}/m)
74
+ if definition.comment || !placeholdersScan.empty?
75
+ comment = " \"description\": \"#{definition.comment}\"" if definition.comment
76
+ placeholders = placeholdersScan.map! { |placeholder| " \"#{placeholder.tr('{}','')}\": {}" }.join(",\n") if !placeholdersScan.empty?
77
+ placeholdersBlock = " \"placeholders\": {\n#{placeholders}\n }" if placeholders
78
+ return " \"@#{definition.key}\": {\n#{[comment, placeholdersBlock].compact.join(",\n")}\n }"
79
+ end
80
+ end
80
81
 
81
82
  def format_key(key)
82
83
  escape_quotes(key)
@@ -0,0 +1,33 @@
1
+ {
2
+ "@@locale": "en",
3
+
4
+ "key1": "Text with comment for translators",
5
+ "@key1": {
6
+ "description": "comment key1"
7
+ },
8
+ "key2": "Simple text",
9
+
10
+ "key3": "Text with {name} placeholder",
11
+ "@key3": {
12
+ "placeholders": {
13
+ "name": {}
14
+ }
15
+ },
16
+ "key4": "Text with double placeholders {1} and {2} and a comment",
17
+ "@key4": {
18
+ "description": "comment key4",
19
+ "placeholders": {
20
+ "1": {},
21
+ "2": {}
22
+ }
23
+ },
24
+
25
+ "key5": "Placeholders with punctuation: {p_1}, {p-1}, {p.1}",
26
+ "@key5": {
27
+ "placeholders": {
28
+ "p_1": {},
29
+ "p-1": {},
30
+ "p.1": {}
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "@@locale": "en",
3
+
4
+ "key1": "Text with comment for translators",
5
+ "@key1": {
6
+ "description": "comment key1"
7
+ },
8
+ "key2": "Simple text",
9
+
10
+ "key3": "Text with {name} placeholder",
11
+ "@key3": {
12
+ "placeholders": {
13
+ "name": {}
14
+ }
15
+ },
16
+ "key4": "Text with double placeholders {1} and {2} and a comment",
17
+ "@key4": {
18
+ "description": "comment key4",
19
+ "placeholders": {
20
+ "1": {},
21
+ "2": {}
22
+ }
23
+ },
24
+
25
+ "key5": "Placeholders with punctuation: {p_1}, {p-1}, {p.1}",
26
+ "@key5": {
27
+ "placeholders": {
28
+ "p_1": {},
29
+ "p-1": {},
30
+ "p.1": {}
31
+ }
32
+ }
33
+ }
@@ -1,13 +1,17 @@
1
1
  [[Section A]]
2
2
  [key1]
3
- en = value1-english
3
+ en = Text with comment for translators
4
4
  comment = comment key1
5
5
  [key2]
6
- en = value2-english
6
+ en = Simple text
7
7
 
8
8
  [[Section B]]
9
9
  [key3]
10
- en = value3-english
10
+ en = Text with {name} placeholder
11
11
  [key4]
12
- en = value4-english
12
+ en = Text with double placeholders {1} and {2} and a comment
13
13
  comment = comment key4
14
+
15
+ [[Section C]]
16
+ [key5]
17
+ en = Placeholders with punctuation: {p_1}, {p-1}, {p.1}
@@ -0,0 +1,13 @@
1
+ [[Uncategorized]]
2
+ [key1]
3
+ en = Text with comment for translators
4
+ comment = comment key1
5
+ [key2]
6
+ en = Simple text
7
+ [key3]
8
+ en = Text with {name} placeholder
9
+ [key4]
10
+ en = Text with double placeholders {1} and {2} and a comment
11
+ comment = comment key4
12
+ [key5]
13
+ en = Placeholders with punctuation: {p_1}, {p-1}, {p.1}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twine-flutter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Butti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-24 00:00:00.000000000 Z
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twine
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.6.0
27
41
  description: Plugin for twine that enables .arb generation for use with Flutter.
28
42
  email:
29
43
  - butti.fabio@tiknil.com
@@ -35,8 +49,10 @@ files:
35
49
  - README.md
36
50
  - lib/formatter.rb
37
51
  - lib/twine-flutter.rb
38
- - test/fixtures/flutter_format.arb
52
+ - test/fixtures/app_en.arb
53
+ - test/fixtures/test.arb
39
54
  - test/fixtures/twine.txt
55
+ - test/fixtures/uncategorized_twine.txt
40
56
  - test/test_flutter_formatter.rb
41
57
  homepage: https://github.com/tiknil/twine-flutter
42
58
  licenses:
@@ -1,15 +0,0 @@
1
- {
2
- "@@locale": "en",
3
-
4
- "key1": "value1-english",
5
- "@key1": {
6
- "description": "comment key1"
7
- },
8
- "key2": "value2-english",
9
-
10
- "key3": "value3-english",
11
- "key4": "value4-english",
12
- "@key4": {
13
- "description": "comment key4"
14
- }
15
- }