patch 9.1.0509: not possible to translate Vim script messages

Problem:  not possible to translate Vim script messages
          (RestorerZ)
Solution: implement bindtextdomain() and gettext() to support Vim script
          message translations (Christ van Willegen)

fixes: #11637
closes: #12447

Signed-off-by: Christ van Willegen <cvwillegen@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak
index e31d2b5..a49f158 100644
--- a/src/testdir/Make_all.mak
+++ b/src/testdir/Make_all.mak
@@ -161,6 +161,9 @@
 	test_function_lists \
 	test_ga \
 	test_getcwd \
+	test_gettext \
+	test_gettext_cp1251 \
+	test_gettext_utf8 \
 	test_getvar \
 	test_gf \
 	test_glob2regpat \
@@ -420,6 +423,9 @@
 	test_functions.res \
 	test_function_lists.res \
 	test_getcwd.res \
+	test_gettext.res \
+	test_gettext_cp1251.res \
+	test_gettext_utf8.res \
 	test_getvar.res \
 	test_gf.res \
 	test_gn.res \
diff --git a/src/testdir/ru_RU/LC_MESSAGES/__PACKAGE__.mo b/src/testdir/ru_RU/LC_MESSAGES/__PACKAGE__.mo
new file mode 100644
index 0000000..300eba2
--- /dev/null
+++ b/src/testdir/ru_RU/LC_MESSAGES/__PACKAGE__.mo
Binary files differ
diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim
index ba8f18f..8e973f6 100644
--- a/src/testdir/test_functions.vim
+++ b/src/testdir/test_functions.vim
@@ -3865,11 +3865,6 @@
   call assert_equal('msg', HasDefault())
 endfunc
 
-" Test for gettext()
-func Test_gettext()
-  call assert_fails('call gettext(1)', 'E1174:')
-endfunc
-
 func Test_builtin_check()
   call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:')
   call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
diff --git a/src/testdir/test_gettext.vim b/src/testdir/test_gettext.vim
new file mode 100644
index 0000000..6a5aafd
--- /dev/null
+++ b/src/testdir/test_gettext.vim
@@ -0,0 +1,16 @@
+source check.vim
+
+" Test for gettext()
+func Test_gettext()
+  call assert_fails('call bindtextdomain("test")', 'E119:')
+  call assert_fails('call bindtextdomain("vim", "test")', 'E475:')
+
+  call assert_fails('call gettext(1)', 'E1174:')
+  call assert_equal('xxxTESTxxx', gettext("xxxTESTxxx"))
+
+  call assert_equal('xxxTESTxxx', gettext("xxxTESTxxx", "vim"))
+  call assert_equal('xxxTESTxxx', gettext("xxxTESTxxx", "__PACKAGE__"))
+  call assert_equal('ERROR: ', gettext("ERROR: ", "__PACKAGE__"))
+endfunc
+
+" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_gettext_cp1251.vim b/src/testdir/test_gettext_cp1251.vim
new file mode 100644
index 0000000..fe02a03
--- /dev/null
+++ b/src/testdir/test_gettext_cp1251.vim
@@ -0,0 +1,22 @@
+source check.vim
+
+" Test for gettext()
+func Test_gettext()
+  set encoding=cp1251
+  call bindtextdomain("__PACKAGE__", getcwd())
+  try
+    language ru_RU
+    call assert_equal('ÎØÈÁÊÀ: ', gettext("ERROR: ", "__PACKAGE__"))
+  catch /^Vim\%((\a\+)\)\=:E197:/
+    throw "Skipped: not possible to set locale to ru (missing?)"
+  endtry
+  try
+    language en_GB.UTF-8
+    call assert_equal('ERROR: ', gettext("ERROR: ", "__PACKAGE__"))
+  catch /^Vim\%((\a\+)\)\=:E197:/
+    throw "Skipped: not possible to set locale to en (missing?)"
+  endtry
+  set encoding&
+endfunc
+
+" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_gettext_utf8.vim b/src/testdir/test_gettext_utf8.vim
new file mode 100644
index 0000000..277710e
--- /dev/null
+++ b/src/testdir/test_gettext_utf8.vim
@@ -0,0 +1,22 @@
+source check.vim
+
+" Test for gettext()
+func Test_gettext()
+  set encoding=utf-8
+  call bindtextdomain("__PACKAGE__", getcwd())
+  try
+    language ru_RU
+    call assert_equal('ОШИБКА: ', gettext("ERROR: ", "__PACKAGE__"))
+  catch /^Vim\%((\a\+)\)\=:E197:/
+    throw "Skipped: not possible to set locale to ru (missing?)"
+  endtry
+  try
+    language en_GB.UTF-8
+    call assert_equal('ERROR: ', gettext("ERROR: ", "__PACKAGE__"))
+  catch /^Vim\%((\a\+)\)\=:E197:/
+    throw "Skipped: not possible to set locale to en (missing?)"
+  endtry
+  set encoding&
+endfunc
+
+" vim: shiftwidth=2 sts=2 expandtab