Help us understand the problem. What is going on with this article?

Redmineのメッセージ種別ごとに語尾を付加する(Postgres・プラグイン対応)

前提条件は
https://qiita.com/ryouma_nagare/items/121da3dba644b5f7368c
を参照ください。

スクリプト

redmine_add_message_suffix.sh
#!/bin/bash
#####################################################
# 環境依存値
DB_NAME=db_redmine
PG_USER=postgres
# プラグイン含めて
REDMINE_DIR=/opt/redmine
# 単独のYMLを指定する場合
LOCALE_FILE=/opt/redmine/config/locales/ja.yml
# メッセージの種別(ラベル名の先頭)ごとに語尾を定義
declare -A SUFFIX
SUFFIX["label"]="にょ"
SUFFIX["notice"]="みたいよ"
SUFFIX["error"]="らしいよ"
SUFFIX["field"]="にゅ"
SUFFIX["setting"]="ぴょ"
SUFFIX["permission"]="ゲマ"
SUFFIX["button"]="ナリ"
# 固定置換用
declare -A FIX_REP
FIX_REP["button_submit"]="目からビーム"
#####################################################
PID=$$

if [ "$1" == "RESET" ]; then

cat << EOS > /tmp/${PID}.sql
UPDATE settings
   SET value=E'--- \n:custom_messages: {} \n'
 WHERE name='plugin_redmine_message_customize'
EOS

else
  IFS_BACKUP=$IFS
  IFS=$'\n'
## 単一のファイルを書き替えたい場合はこっち
#  declare -a LABELS=( $(grep -E '^  (label|notice|error|field|setting|permission|button)' ${LOCALE_FILE} | grep -v '|' | grep -v ':\s*$') )
## プラグインを含めて書き替えたい場合はこっち
  declare -a LABELS=( $(grep -E '^  (label|notice|error|field|setting|permission|button)' $(find ${REDMINE_DIR} -name 'ja.yml') | grep -v '|' | grep -v ':\s*$'  | awk -F': ' '{c="";for(i=2;i<=NF;i++) c=c $i": "; print c}' | sed -e 's/: $//g' ) )

  for LABEL in ${LABELS[@]}
  do
    MSG_KEY=$(echo ${LABEL} | awk -F': ' '{print $1}' | sed -e 's/ //g' )
    MSG_VALUE=$(echo ${LABEL} | awk -F': ' '{c="";for(i=2;i<=NF;i++) c=c $i": "; print c}' | sed -e 's/: $//g' | sed -e 's/\r//g' )

    # 下のいずれでもない
    REP_PTN=0
    # 固定置換あり
    if [ "${FIX_REP[${MSG_KEY}]}" != "" ]; then
      REP_PTN=1
    fi
    # 句点、コロン、?のいずれかを含む
    echo ${MSG_VALUE} | grep -E '(。|:|:|?)' > /dev/null
    if [ $? -eq 0 -a "${REP_PTN}" == "0" ]; then
      REP_PTN=2
    fi
    # ダブルクォートで終わる
    echo ${MSG_VALUE} | grep -E '"$' > /dev/null
    if [ $? -eq 0 -a "${REP_PTN}" == "0" ]; then
      REP_PTN=3
    fi
    # シングルクォートで終わる
    echo ${MSG_VALUE} | grep -E "'$" > /dev/null
    if [ $? -eq 0 -a "${REP_PTN}" == "0" ]; then
      REP_PTN=4
    fi

    PREFIX=$(echo ${MSG_KEY} | awk -F'_' '{print $1}' )
    REPSTR=${SUFFIX[${PREFIX}]}

    case "${REP_PTN}" in
    "0" ) MSG_VALUE=$(echo ${MSG_VALUE} | sed -e "s/$/${REPSTR}/g") ;;
    "1" ) MSG_VALUE=${FIX_REP[${MSG_KEY}]} ;;
    "2" ) MSG_VALUE=$(echo ${MSG_VALUE} | sed -e "s/。/${REPSTR}。/g" | sed -e "s/:/${REPSTR}:/g" | sed -e "s/:/${REPSTR}:/g" | sed -e "s/?/${REPSTR}?/g") ;;
    "3" ) MSG_VALUE=$(echo ${MSG_VALUE} | sed -e "s/\"$/${REPSTR}\"/g") ;;
    "4" ) MSG_VALUE=$(echo ${MSG_VALUE} | sed -e "s/'$/${REPSTR}'/g") ;;
    esac

    MSG_VALUE=$(echo ${MSG_VALUE} | sed -e "s/'/''/g")

    MSG+="    ${MSG_KEY}: ${MSG_VALUE}\n"
  done

cat << EOS > /tmp/${PID}.sql
UPDATE settings
   SET value=E'--- \n:custom_messages: \n  ja:\n${MSG}'
 WHERE name='plugin_redmine_message_customize'
EOS
fi

IFS=$IFS_BACKUP

sudo su - ${PG_USER} -c "psql ${DB_NAME} -f /tmp/${PID}.sql"
rm -f /tmp/${PID}.sql

使い方

  • メッセージ設定
./redmine_add_message_suffix.sh

語尾をつけるのではなく、固定で置換したい場合はFIX_REP の行をコピーして増やしてください。

  • メッセージ設定取消
./redmine_add_message_suffix.sh RESET

結果

image.png

注意

  • プラグインによってはエラーになるものもあるかもしれません。
    • 以下のプラグインをインストールしている環境ではエラーにならずにすんでいます。
Redmine plugins:
  alm                            1.5.3
  full_text_search               1.0.4
  group_watchers                 0.0.1
  lac                            1.5.4
  lad                            1.5.5
  levm                           2.8.0
  lgc                            4.1.5
  lgc_pro                        4.1.5
  lpt                            1.2.12
  lychee_easy_assigned_user      1.0.9
  lychee_issue_board             3.7.5
  lychee_issue_set               1.7.3
  lychee_issue_spread_sheet      1.0.7
  lychee_issues_evm              2.8.0
  lychee_profile_icon            1.1.1
  lychee_project_view            1.2.12
  lychee_status_color            1.0.5
  lychee_version_start_date      1.0.9
  lychee_workdays                1.0.6
  redmine_absolute_dates         0.0.4
  redmine_banner                 0.2.1
  redmine_code_review            1.0.0
  redmine_issue_templates        0.3.5
  redmine_jstoolbar_ext          0.2.1
  redmine_jstoolbar_ext_buttons  0.2.1
  redmine_message_customize      
  redmine_persist_wfmt           2.0.1
  redmine_vividtone_my_page_blocks 2017/07/05
  redmine_wiki_extensions        0.9.0
  redmine_wiki_index_tree_view   0.0.1
  redmine_wiki_lists             0.0.9
  redmine_wiki_page_tree         0.0.2
  redmine_work_time              0.4.0
  redmine_xls_export             0.2.1.t11
  view_customize                 2.3.0
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away