|
| 1 | +--[[ |
| 2 | + gen_18n_mo.lua - generate .mo files from .po files and put them in the correct place |
| 3 | +
|
| 4 | + Copyright (C) 2016,2018 Bill Ferguson <[email protected]> |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation; either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +]] |
| 19 | +--[[ |
| 20 | + gen_i18n_mo - generate translation files from the source and place them in the appropriate locale directory |
| 21 | + |
| 22 | + gen_i18n_mo finds all the .po files scattered throughout the script tree, compiles them into |
| 23 | + .mo files and places them in the correct locale directory for use by the gettext tools. |
| 24 | +
|
| 25 | +]] |
| 26 | + |
| 27 | +local dt = require "darktable" |
| 28 | +local du = require "lib/dtutils" |
| 29 | +local df = require "lib/dtutils.file" |
| 30 | +local log = require "lib/dtutils.log" |
| 31 | +local dtsys = require "lib/dtutils.system" |
| 32 | + |
| 33 | +-- figure out the path separator |
| 34 | + |
| 35 | +local PS = dt.configuration.running_os == "windows" and "\\" or "/" |
| 36 | + |
| 37 | +local LUA_DIR = dt.configuration.config_dir .. PS .. "lua" .. PS |
| 38 | +local LOCALE_DIR = dt.configuration.config_dir .. PS .. "lua" .. PS .. "locale" .. PS |
| 39 | + |
| 40 | +-- check if we have msgfmt |
| 41 | + |
| 42 | +local msgfmt_executable = df.check_if_bin_exists("msgfmt") |
| 43 | + |
| 44 | +if msgfmt_executable then |
| 45 | + |
| 46 | + -- find the .po files |
| 47 | + |
| 48 | + local find_cmd = "find -L " .. LUA_DIR .. " -name \\*.po -print" |
| 49 | + if dt.configuration.running_os == "windows" then |
| 50 | + find_cmd = "dir /b/s " .. LUA_DIR .. "\\*.po" |
| 51 | + end |
| 52 | + |
| 53 | + local output = io.popen(find_cmd) |
| 54 | + |
| 55 | + -- for each .po file.... |
| 56 | + |
| 57 | + for line in output:lines() do |
| 58 | + local fname = df.get_filename(line) |
| 59 | + |
| 60 | + -- get the language used... this depends on the file being named using |
| 61 | + -- the convention /..../lang/LC_MESSAGES/file.po where lang is de_DE, fr_FR, etc. |
| 62 | + |
| 63 | + local path_parts = du.split(line, PS) |
| 64 | + local lang = path_parts[#path_parts - 2] |
| 65 | + |
| 66 | + -- ensure there is a destination directory for them |
| 67 | + |
| 68 | + local mkdir_cmd = "mkdir -p " |
| 69 | + if dt.configuration.running_os == "windows" then |
| 70 | + mkdir_cmd = "mkdir " |
| 71 | + end |
| 72 | + |
| 73 | + if not df.check_if_file_exists(LOCALE_DIR .. lang .. PS .. "LC_MESSAGES") then |
| 74 | + log.msg(log.info, "Creating locale", lang) |
| 75 | + os.execute(mkdir_cmd .. LOCALE_DIR .. lang .. PS .. "LC_MESSAGES") |
| 76 | + end |
| 77 | + |
| 78 | + -- generate the mo file |
| 79 | + |
| 80 | + fname = string.gsub(fname, ".po$", ".mo") |
| 81 | + log.msg(log.info, "Compiling translation to", fname) |
| 82 | + local result = os.execute(msgfmt_executable .. " -o " .. LOCALE_DIR .. lang .. PS .. "LC_MESSAGES" .. PS .. fname .. " " .. line) |
| 83 | + end |
| 84 | +else |
| 85 | + log.msg(log.screen, "ERROR: msgfmt executable not found. Please install or specifiy location in preferences.") |
| 86 | +end |
| 87 | +dt.preferences.register("executable_paths", "msgfmt", -- name |
| 88 | + "file", -- type |
| 89 | + 'gen_i18n_mo: msgfmt location', -- label |
| 90 | + 'Install location of msgfmt. Requires restart to take effect.', -- tooltip |
| 91 | + "msgfmt", -- default |
| 92 | + dt.new_widget("file_chooser_button"){ |
| 93 | + title = "Select msgfmt[.exe] file", |
| 94 | + value = "", |
| 95 | + is_directory = false, |
| 96 | + } |
| 97 | +) |
0 commit comments