##### prepare strings from data files ###########

message( STATUS "Prepare to extract strings from tellico data files..." )

## core/tellico_config.kcfg
tde_l10n_prepare_xml(
    SOURCE "core/tellico_config.kcfg"
    TAGS "default"
    C_FORMAT
)
file( READ "${CMAKE_CURRENT_SOURCE_DIR}/core/tellico_config.kcfg.tde_l10n" _config_data )
string( REGEX REPLACE "[^\n]" "" _config_len ${_config_data} )
string( LENGTH "+${_config_len}" _config_len )
set( _config_pos 0 )
unset( _config_l10n )
while( _config_pos LESS ${_config_len} )
    # pick line
    string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\1" _config_line "${_config_data}" )
    string( REGEX REPLACE "^([^\n]*)\n(.*)" "\\2" _config_data "${_config_data}" )
    math( EXPR _config_pos "${_config_pos}+1" )

    # clean default config value without "code"
    if( "${_config_line}" MATCHES "i18n\\(\"i18n\\(" )
        string( REGEX REPLACE
            "i18n\\(\"i18n\\(\\\\\"(.*)\\\\\"\\)\"\\);"
            "i18n(\"\\1\");"
            _config_line "${_config_line}" )
    else( )
      set( _config_line "" )
    endif( )

    # write line
    set( _config_l10n "${_config_l10n}${_config_line}\n" )
endwhile( )
file( WRITE "${CMAKE_CURRENT_SOURCE_DIR}/core/tellico_config.kcfg.tde_l10n" "${_config_l10n}" )

## tips
tde_l10n_preparetips( "../tellico.tips" )

## xsl templates
file( GLOB_RECURSE _xsl_files
      RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/../xslt/*.xsl )
unset( _xsl_list_l10n )
set( _xsl_l10n "../xslt/.tde_l10n" )
foreach( _xsl_file ${_xsl_files} )
    if( "${_xsl_file}" MATCHES "/[^/]*-templates/" )
        get_filename_component( _xsl_name "${_xsl_file}" NAME_WE )
        string( REPLACE "_" " " _xsl_name "${_xsl_name}" )
        set( _xsl_list_l10n
            "${_xsl_list_l10n}i18n(\"${_xsl_name} XSL Template\", \"${_xsl_name}\");\n" )
    endif( )
    tde_l10n_prepare_xml( SOURCE ${_xsl_file} TAGS i18n )
    list( APPEND _xsl_l10n "${_xsl_file}.tde_l10n" )
endforeach( )
file( WRITE "${CMAKE_CURRENT_SOURCE_DIR}/../xslt/.tde_l10n" "${_xsl_list_l10n}" )

## z3950 servers
file( READ "${CMAKE_CURRENT_SOURCE_DIR}/fetch/z3950-servers.cfg" _z3950_servers )
string( REGEX REPLACE
    "(^|\n)Name=([^\n]+)"
    "\\1i18n(\"\\2\");"
    _z3950_l10n "${_z3950_servers}"
)
file( WRITE "${CMAKE_CURRENT_SOURCE_DIR}/fetch/z3950-server.cfg.tde_l10n" "${_z3950_l10n}" )

## scripts spec
file( GLOB_RECURSE _scripts_files
      RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/fetch/scripts/*.spec )
unset( _scripts_l10n )
foreach( _script_file ${_scripts_files} )
    file( READ "${CMAKE_CURRENT_SOURCE_DIR}/${_script_file}" _script_spec )
    string( REGEX REPLACE
        "(^|\n)Name=([^\n]+)"
        "\\1i18n(\"\\2\");"
        _script_l10n "${_script_spec}"
    )
    file( WRITE "${CMAKE_CURRENT_SOURCE_DIR}/${_script_file}.tde_l10n" "${_script_l10n}" )
    list( APPEND _scripts_l10n "${_script_file}.tde_l10n" )
endforeach( )


##### create translation templates ##############

tde_l10n_create_template(
    CATALOG "messages/tellico"
    SOURCES
        "."
        "core/tellico_config.kcfg.tde_l10n"
        "../tellico.tips.tde_l10n"
        ${_xsl_l10n}
        "fetch/z3950-server.cfg.tde_l10n"
        ${_scripts_l10n}
    EXCLUDES
        "^core/tellico_config\\\\.kcfg\\$"
    COMMENT "TRANSLATORS:"
)
