简体   繁体   English

用数组翻译数组

[英]Translate Array with Array

I search a way to translate a key from one array with other array.我搜索了一种将一个数组中的键转换为另一个数组的方法。

tmp_title=$3
title=$(echo ${tmp_title,,} | sed -e 's/\s/-/g')
tags=(computer media state society)
de=(computer medien staat gesellschaft)
fr=(ordinateur journalisme politique société)
ru=(Компьютер СМИ штат общество)

      file="./content/de/blog/$(date +"%Y")/$(date +"%m")/$title.md"
      if test -n "$2"; then

        # check tag is in tags array
        if [[ ${tags[*]} =~ $2 ]]; then

          # check the folder structure is right
          if [[ -d ./content/de/blog/$(date +"%Y")/$(date +"%m") ]]; then

            # create the content and fill up the file
            echo "---" >> "$file"
            echo "title: \"$3\"" >> "$file"
            echo "date: $(date +\"%Y-%m-%d\")" >> "$file"
            echo "draft: false" >> "$file"
            echo "tags: \"$2\"" >> "$file"
            echo "shorttext:" >> "$file"
            echo "cover: \"$2\"" >> "$file"
            echo "lang: $1" >> "$file"
            echo "---" >> "$file"
          fi
        else
          echo "Enter a valid tag name ..."
        fi
      fi

I search a way to translate "tags: \\"$2\\"" >> "$file" in the language array value.我搜索了一种在语言数组值中翻译 "tags: \\"$2\\"" >> "$file" 的方法。 When I append society to the script then should be tags: "gesellschaft".当我将社会附加到脚本时,应该是标签:“gesellschaft”。

Thank you for help.谢谢你的帮助。

Silvio西尔维奥

Consider this approach考虑这种方法

case $LANG in
    *en*) tags=(computer   media       state     society     );;
    *de*) tags=(computer   medien      staat     gesellschaft);;
    *fr*) tags=(ordinateur journalisme politique société     );;
    *ru*) tags=(Компьютер  СМИ         штат      общество    );;
esac

I have it realize other.我有它实现其他。 I ask for categories and translate then to the $lang value.我要求类别,然后转换为 $lang 值。

#!/usr/bin/env bash

# variables through user input
lang=$1
cover=$2
tmp_title=$3

# variables which we use in script
# create a title with small letters and remove whitespace
title=$(echo ${tmp_title,,} | sed -e 's/\s/-/g')

# categories
categories=(computer media repression society)

# date variables
date=$(date +"%Y-%m-%d")
year=$(date +"%Y")
month=$(date +"%m")

# content variables
content_dir="./content/$lang/blog/$year/$month"
file="$content_dir/$title.md"

# function
function create_file()
{
  {
    echo "---"
    echo "title: \"$tmp_title\""
    echo "date: $date"
    echo "draft: false"
    echo "tags: \"$tag\""
    echo "shorttext:"
    echo "cover: \"$cover\""
    echo "lang: $lang"
    echo "---"
  } >> "$file"
}

case $1 in
  de)
    if test -n "$lang"; then
      # check tag is in array
      if [[ ${categories[*]} =~ $cover ]]; then

        # translation tag > categories
        if [[ $cover =~ "computer" ]]; then
          tag="Computer"

        elif [[ $cover =~ "media" ]]; then
          tag="Medien"

        elif [[ $cover =~ "repression" ]]; then
          tag="Staat"


        elif [[ $cover =~ "society" ]]; then
          tag="Gesellschaft"
        fi

        # check the folder structure is right
        if [[ -d "$content_dir" ]]; then

          # create the content and fill up the file
          create_file

          if [[ -f "$file" ]]; then
            subl "$file"
          fi

        else

          # create the folder of content
          mkdir -p "$content_dir"

          # create the content and fill up the file
          create_file

          if [[ -f "$file" ]]; then
            subl "$file"
          fi

        fi
      else
        echo "Enter a valid tag name ..."
      fi
    fi
  ;;

  en)
    if test -n "$lang"; then
      # check tag is in array
      if [[ ${categories[*]} =~ $cover ]]; then

        # translation tag > categories
        if [[ $cover =~ "computer" ]]; then
          tag="Computer"

        elif [[ $cover =~ "media" ]]; then
          tag="Media"

        elif [[ $cover =~ "repression" ]]; then
          tag="State"

        elif [[ $cover =~ "society" ]]; then
          tag="Society"
        fi

        # check the folder structure is right
        if [[ -d "$content_dir" ]]; then

          # create the content and fill up the file
          create_file

          if [[ -f "$file" ]]; then
            subl "$file"
          fi

        else

          # create the folder of content
          mkdir -p "$content_dir"

          # create the content and fill up the file
          create_file

          if [[ -f "$file" ]]; then
            subl "$file"
          fi

        fi
      else
        echo "Enter a valid tag name ..."
      fi
    fi
  ;;

  info)
    echo "To work with this script you need append the follow stuff"
    echo "./bin/new.sh lang cover title"
    echo "./bin/news.sh en society 'This is a title'"
    echo "cover: computer, media, repression, society"
  ;;
esac

Silvio西尔维奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM