diff --git a/src/CSV/Import.elm b/src/CSV/Import.elm index a1d09f4..47c1d3f 100644 --- a/src/CSV/Import.elm +++ b/src/CSV/Import.elm @@ -10,6 +10,7 @@ Use Localized.Writer.write to create elm code from the list of localized elements. @docs generate + -} import Csv @@ -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 @@ -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 @@ -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 @@ -171,3 +173,8 @@ staticElement modulename key comment value = } , value = value } + + +withoutEmptyStrings : List String -> List String +withoutEmptyStrings = + List.filter (String.isEmpty >> not) diff --git a/tests/Tests/CSV/Import.elm b/tests/Tests/CSV/Import.elm index 1d776e2..00a5e9f 100644 --- a/tests/Tests/CSV/Import.elm +++ b/tests/Tests/CSV/Import.elm @@ -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" """ @@ -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" """ @@ -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" + ] + } ]