7

bashスクリプトでgettextを使用するには?

このページしか見つけられなかったのですが、よくわかりません。

ローカリゼーション

私のスクリプトは次のように書かれています:

 #!/bin/bash
 . lang_file.sh
 echo $LANG_HELLO_WORLD

lang_file.sh は次のようになります。

 #!/bin/bash
 LANG_HELLO_WORLD="Hello World"

次のように、gettext を使用して lang_file.sh を何かに変更したいと考えています。

 #!/bin/bash
 LANG_HELLO_WORLD=`some gettext command to get string in user language`

Launchpad でコードを使用して、他のユーザーが翻訳できるようにしたい (.po、.pot ファイル)

下手な英語で申し訳ありませんが、何か提案はありますか?

4

4 に答える 4

6

You need to preform following steps:

  1. Determine what's your project name, gettext calls it textdomain, you will need it to retrieve the translations for your project. Let's call it "PRJ".
  2. Mark the strings to be translated. Following snippet gives example:

    Let's call it PRJ.sh:

    #!/bin/sh
    alias GETTEXT='gettext "PRJ"'
    
    ## Use GETTEXT to mark the string you want to translate
    HELLO_WORLD=$(GETTEXT "Hello world") 
    
    echo "$HELLO_WORLD"
    
  3. Produce .pot file so translators can work on it.

    Run the following command, it only looks for GETTEXT, the ones you actually want to translate.

    xgettext -o PRJ.pot  -L Shell --keyword --keyword=GETTEXT  PRJ.sh
    
  4. Optional: Generate .po files.

    For each locale you want to cover.

    msginit -i PRJ.pot -l fr.UTF-8
    

    Note that "UTF-8" is sugggested, otherwise msginit may mistakenly choose some obsoleted encoding for you.

  5. Retrieve completed .po files, and convert them to .mo file (the file that machine can read it)

    msgfmt -v  fr.po -o fr.mo
    
  6. Install .mo files. Run:

    sudo install fr.mo /usr/share/locale/fr/LC_MESSAGES/PRJ.mo 
    

    Now you can try the result:

    LANGUAGE=fr  ./PRJ.sh
    

    and you should see French translation for Hello world.

于 2014-10-09T07:49:54.450 に答える
1

あなたがしようとしていることは、適切な言語でユーザーに尋ねることだと思いますか? おそらく、ユーザーに最初に言語を選択してもらいたいでしょう。あなたが求めていることの他の部分は、変数の中に $(get_some_str_func) のようなコマンドを埋め込むだけです。

私はこのコードを書きませんでしたが、あなたがやろうとしていることの線に沿っているのでしょうか? よくわかりません。あなたが何を求めているのか完全には理解できません。

    function _configure_locale() { # [profile]
        local profile=${1:-EN}
        case ${profile} in
          DE|DE_DE|de_DE)
              LC_ALL="de_DE.UTF-8"
              LANG="de_DE.UTF-8"
              LANGUAGE="de_DE:de:en_US:en"
              ;;
          EN|EN_US|en|en_US)
              LC_ALL="en_US.UTF-8"
              LANG="en_US.UTF-8"
              LANGUAGE="en_US:en"
              ;;
          *)
              echo "ALERT" "${FUNCNAME}: unknown profile '${profile}'"
              ;;
          esac
          LC_PAPER="de_DE.UTF-8"; # independent from locale
          LESSCHARSET="utf-8";    # independent from locale
          MM_CHARSET="utf-8"      # independent from locale
          echo "locale settings" "${LANG}";
          export LC_ALL LANG LANGUAGE LC_PAPER LESSCHARSET MM_CHARSET
    }
于 2014-06-06T14:42:34.667 に答える