Skip to content

Commit

Permalink
Merge pull request #4 from iosphere/fix/csv-placeholder
Browse files Browse the repository at this point in the history
Fix/csv placeholder
  • Loading branch information
klaftertief authored Jul 26, 2017
2 parents 710d3aa + 9f39e6b commit 3ee76b8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
59 changes: 33 additions & 26 deletions src/CSV/Import.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Use Localized.Writer.write to create elm code from the list of localized
elements.
@docs generate
-}

import Csv
Expand Down Expand Up @@ -59,18 +60,18 @@ generateForCsv lines =
)
|> Dict.fromList
in
-- Generate the source code for each module based on the lines
-- grouped in the expression above.
List.map
(\name ->
let
linesForThisModule =
Dict.get name linesForModules
|> Maybe.withDefault []
in
( name, generateForModule linesForThisModule )
)
modules
-- Generate the source code for each module based on the lines
-- grouped in the expression above.
List.map
(\name ->
let
linesForThisModule =
Dict.get name linesForModules
|> Maybe.withDefault []
in
( name, generateForModule linesForThisModule )
)
modules


generateForModule : List (List String) -> List Localized.Element
Expand Down Expand Up @@ -119,10 +120,10 @@ code modulename key comment placeholderString value =
numPlaceholders =
List.length placeholders
in
if numPlaceholders == 0 then
staticElement modulename key comment value
else
formatElement modulename key comment placeholders value
if numPlaceholders == 0 then
staticElement modulename key comment value
else
formatElement modulename key comment placeholders value


formatElement : String -> String -> String -> List String -> String -> Localized.Element
Expand All @@ -131,34 +132,35 @@ formatElement modulename key comment placeholders value =
components =
-- "Hello {{p}} Goodbye {{q}}" -> ["Hello ", "p}} Goodbye ", "q }}"]
String.split "{{" value
|> withoutEmptyStrings
|> List.map
(\candidate ->
if String.contains "}}" candidate then
-- "p}} Goodbye " -> ["p", " Goodbye "]
String.split "}}" candidate
|> List.filter (String.isEmpty >> not)
|> withoutEmptyStrings
-- ["p", " Goodbye "] -> [FormatComponentPlaceholder "p", FormatComponentStatic " Goodbye "]
|> List.indexedMap
(\index submatch ->
if index % 2 == 0 then
Localized.FormatComponentPlaceholder (String.trim submatch)
else
Localized.FormatComponentStatic candidate
Localized.FormatComponentStatic submatch
)
else
[ Localized.FormatComponentStatic candidate ]
)
|> List.concat
in
Localized.ElementFormat
{ meta =
{ moduleName = modulename
, key = key
, comment = comment
}
, placeholders = placeholders
, components = components
Localized.ElementFormat
{ meta =
{ moduleName = modulename
, key = key
, comment = comment
}
, placeholders = placeholders
, components = components
}


staticElement : String -> String -> String -> String -> Localized.Element
Expand All @@ -171,3 +173,8 @@ staticElement modulename key comment value =
}
, value = value
}


withoutEmptyStrings : List String -> List String
withoutEmptyStrings =
List.filter (String.isEmpty >> not)
40 changes: 40 additions & 0 deletions tests/Tests/CSV/Import.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ inputCSV =
++ """
"Translation.Test","myString","My comment","","Value"
"Translation.Test","myFormat","","label","Prefix: {{label}}"
"Translation.Test","myFormatAtBeginning","","label","{{label}} suffix"
"Translation.Test","myFormatQuoted","","label","Prefix '{{label}}' suffix"
"""


Expand All @@ -65,6 +67,19 @@ myFormat : String -> String
myFormat label =
"Prefix: "
++ label
myFormatAtBeginning : String -> String
myFormatAtBeginning label =
label
++ " suffix"
myFormatQuoted : String -> String
myFormatQuoted label =
"Prefix '"
++ label
++ "' suffix"
"""


Expand All @@ -90,4 +105,29 @@ elements =
, Localized.FormatComponentPlaceholder "label"
]
}
, Localized.ElementFormat
{ meta =
{ moduleName = "Translation.Test"
, key = "myFormatAtBeginning"
, comment = ""
}
, placeholders = [ "label" ]
, components =
[ Localized.FormatComponentPlaceholder "label"
, Localized.FormatComponentStatic " suffix"
]
}
, Localized.ElementFormat
{ meta =
{ moduleName = "Translation.Test"
, key = "myFormatQuoted"
, comment = ""
}
, placeholders = [ "label" ]
, components =
[ Localized.FormatComponentStatic "Prefix '"
, Localized.FormatComponentPlaceholder "label"
, Localized.FormatComponentStatic "' suffix"
]
}
]

0 comments on commit 3ee76b8

Please sign in to comment.