@@ -151,8 +151,8 @@ private static void AddPrefixUnits(Quantity quantity)
151
151
152
152
unitsToAdd . Add ( new Unit
153
153
{
154
- SingularName = $ "{ prefix } { StringExtensions . ToCamelCase ( unit . SingularName ) } ", // "Kilo" + "NewtonPerMeter" => "KilonewtonPerMeter"
155
- PluralName = $ "{ prefix } { StringExtensions . ToCamelCase ( unit . PluralName ) } ", // "Kilo" + "NewtonsPerMeter" => "KilonewtonsPerMeter"
154
+ SingularName = $ "{ prefix } { unit . SingularName . ToCamelCase ( ) } ", // "Kilo" + "NewtonPerMeter" => "KilonewtonPerMeter"
155
+ PluralName = $ "{ prefix } { unit . PluralName . ToCamelCase ( ) } ", // "Kilo" + "NewtonsPerMeter" => "KilonewtonsPerMeter"
156
156
BaseUnits = null , // Can we determine this somehow?
157
157
FromBaseToUnitFunc = $ "({ unit . FromBaseToUnitFunc } ) / { prefixInfo . Factor } ",
158
158
FromUnitToBaseFunc = $ "({ unit . FromUnitToBaseFunc } ) * { prefixInfo . Factor } ",
@@ -166,60 +166,95 @@ private static void AddPrefixUnits(Quantity quantity)
166
166
167
167
private static Localization [ ] GetLocalizationForPrefixUnit ( Unit unit , int prefixIndex , PrefixInfo prefixInfo , string quantityName )
168
168
{
169
- return unit . Localization . Select ( loc =>
169
+ string [ ] GetUnitAbbreviationsForPrefix ( Localization loc )
170
170
{
171
- EnsureValidAbbreviationsWithPrefixes ( quantityName , loc , unit ) ;
172
-
173
- // "k" + "m" => "km"
174
- // Correct count is ensured earlier
171
+ // If no custom abbreviations are specified, prepend the default prefix to each unit abbreviation: kilo ("k") + meter ("m") => kilometer ("km")
175
172
if ( loc . AbbreviationsWithPrefixes == null || ! loc . AbbreviationsWithPrefixes . Any ( ) )
176
173
{
177
- var prefixedAbbreviation = $ "{ prefixInfo . Abbreviation } { loc . Abbreviations . First ( ) } ";
178
- return new Localization
179
- {
180
- Culture = loc . Culture ,
181
- Abbreviations = new [ ] { prefixedAbbreviation } ,
182
- } ;
174
+ string prefix = prefixInfo . Abbreviation ;
175
+ return loc . Abbreviations . Select ( unitAbbreviation => $ "{ prefix } { unitAbbreviation } ") . ToArray ( ) ;
183
176
}
184
177
185
178
/*
186
- Example: For languages where you can't simply prepend "k" for kilo prefix, the prefix abbreviations must be explicitly defined
187
- with AbbreviationsWithPrefixes. This is an array of string|string[] so if there are two items in `Abbreviations` then
188
- there should be sub-arrays each of length 2.
189
- "Prefixes": [ "Nano", "Micro", "Milli" ],
190
- "Localization": [
191
- {
192
- "Culture": "en-US",
193
- "Abbreviations": [ "s", "sec", "secs", "second", "seconds" ]
194
- },
195
- {
196
- "Culture": "ru-RU",
197
- "Abbreviations": [ "с", "сек" ],
198
- "AbbreviationsWithPrefixes": [ ["нс", "нсек"], ["мкс", "мксек"], ["мс", "мсек"] ]
199
- }
179
+ Prepend prefix to all abbreviations of a unit.
180
+ Some languages, like Russian, you can't simply prepend "k" for kilo prefix, so the prefix abbreviations must be explicitly defined
181
+ with AbbreviationsWithPrefixes.
182
+
183
+ Example 1 - Torque.Newtonmeter has only a single abbreviation in Russian, so AbbreviationsWithPrefixes is an array of strings mapped to each prefix
184
+
185
+ {
186
+ "SingularName": "NewtonMeter",
187
+ "PluralName": "NewtonMeters",
188
+ "FromUnitToBaseFunc": "x",
189
+ "FromBaseToUnitFunc": "x",
190
+ "Prefixes": [ "Kilo", "Mega" ],
191
+ "Localization": [
192
+ {
193
+ "Culture": "en-US",
194
+ "Abbreviations": [ "N·m" ]
195
+ },
196
+ {
197
+ "Culture": "ru-RU",
198
+ "Abbreviations": [ "Н·м" ],
199
+ "AbbreviationsWithPrefixes": [ "кН·м", "МН·м" ]
200
+ }
201
+ ]
202
+ },
203
+
204
+ Example 2 - Duration.Second has 3 prefixes and 2 abbreviations in Russian, so AbbreviationsWithPrefixes is an array of 3 items where each
205
+ represents the unit abbreviations for that prefix - typically a variant of those in "Abbreviations", but the counts don't have to match.
206
+
207
+ {
208
+ "SingularName": "Second",
209
+ "PluralName": "Seconds",
210
+ "BaseUnits": {
211
+ "T": "Second"
212
+ },
213
+ "FromUnitToBaseFunc": "x",
214
+ "FromBaseToUnitFunc": "x",
215
+ "Prefixes": [ "Nano", "Micro", "Milli" ],
216
+ "Localization": [
217
+ {
218
+ "Culture": "en-US",
219
+ "Abbreviations": [ "s", "sec", "secs", "second", "seconds" ]
220
+ },
221
+ {
222
+ "Culture": "ru-RU",
223
+ "Abbreviations": [ "с", "сек" ],
224
+ "AbbreviationsWithPrefixes": [ ["нс", "нсек"], ["мкс", "мксек"], ["мс", "мсек"] ]
225
+ }
226
+ ]
227
+ }
200
228
*/
201
- string [ ] abbreviationsWithPrefixes ;
202
- switch ( loc . AbbreviationsWithPrefixes [ prefixIndex ] . Type )
229
+
230
+ EnsureValidAbbreviationsWithPrefixes ( loc , unit , quantityName ) ;
231
+ JToken abbreviationsForPrefix = loc . AbbreviationsWithPrefixes [ prefixIndex ] ;
232
+ switch ( abbreviationsForPrefix . Type )
203
233
{
204
234
case JTokenType . Array :
205
- abbreviationsWithPrefixes = loc . AbbreviationsWithPrefixes [ prefixIndex ] . ToObject < string [ ] > ( ) ;
206
- break ;
235
+ return abbreviationsForPrefix . ToObject < string [ ] > ( ) ;
207
236
case JTokenType . String :
208
- abbreviationsWithPrefixes = new [ ] { loc . AbbreviationsWithPrefixes [ prefixIndex ] . ToObject < string > ( ) } ;
209
- break ;
237
+ return new [ ] { abbreviationsForPrefix . ToObject < string > ( ) } ;
210
238
default :
211
239
throw new NotSupportedException ( "Expect AbbreviationsWithPrefixes to be an array of strings or string arrays." ) ;
212
240
}
241
+ }
242
+
243
+ Localization WithPrefixes ( Localization loc )
244
+ {
245
+ string [ ] unitAbbreviationsForPrefix = GetUnitAbbreviationsForPrefix ( loc ) ;
213
246
214
247
return new Localization
215
248
{
216
249
Culture = loc . Culture ,
217
- Abbreviations = abbreviationsWithPrefixes
250
+ Abbreviations = unitAbbreviationsForPrefix ,
218
251
} ;
219
- } ) . ToArray ( ) ;
252
+ }
253
+
254
+ return unit . Localization . Select ( WithPrefixes ) . ToArray ( ) ;
220
255
}
221
256
222
- private static void EnsureValidAbbreviationsWithPrefixes ( string quantityName , Localization localization , Unit unit )
257
+ private static void EnsureValidAbbreviationsWithPrefixes ( Localization localization , Unit unit , string quantityName )
223
258
{
224
259
if ( localization . AbbreviationsWithPrefixes . Length > 0 &&
225
260
localization . AbbreviationsWithPrefixes . Length != unit . Prefixes . Length )
0 commit comments