This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

PO.Import

Generates a list of localized elements for multiple modules from a PO string.

This matches the export format generated by PO.Export.generate.

Use Localized.Writer.write to create elm code from the list of localized elements.

generate : String -> List ( String, List Localized.Element )

Generates localized elements for multiple modules from a PO string. The msgids in the PO file are expected to include the module name and key. For example msgid "Translation.Main.title" will generate a Localized.Element with key title and module Translation.Main.

You will usually use this output to create elm code:

PO.Import.generate poString
|> Localized.Writer.write
module PO.Import exposing (generate)

{-| Generates a list of localized elements for multiple modules from a PO
string.

This matches the export format generated by PO.Export.generate.

Use Localized.Writer.write to create elm code from the list of localized
elements.

@docs generate
-}

import Dict exposing (Dict)
import Localized exposing (FormatComponent)
import PO.Import.Internal exposing (..)


{-| Generates localized elements for multiple modules from a PO
string. The msgids in the PO file are expected to include the module name and
key. For example `msgid "Translation.Main.title"` will generate a
`Localized.Element` with key `title` and module `Translation.Main`.

You will usually use this output to create elm code:

    PO.Import.generate poString
    |> Localized.Writer.write

-}
generate : String -> List ( String, List Localized.Element )
generate poString =
    let
        keysInModules =
            keys poString
    in
        List.map
            (\( moduleName, keysInModule ) ->
                generateModule poString moduleName keysInModule
                    |> (,) moduleName
            )
            keysInModules


generateModule : String -> String -> List String -> List Localized.Element
generateModule poString moduleName allKeys =
    let
        fullComments =
            poComments poString moduleName allKeys

        fullCommentForKey key =
            Dict.get key fullComments |> Maybe.withDefault ""

        allValues =
            values poString moduleName allKeys

        valueForKey key =
            Dict.get key allValues |> Maybe.withDefault ""
    in
        List.map
            (\key ->
                element moduleName key (valueForKey key) (fullCommentForKey key)
            )
            allKeys