A
A
alestro2016-01-15 14:01:54
PHP
alestro, 2016-01-15 14:01:54

How to localize a project using mo files?

Interested in how php projects are localized using mo files, for example, as in the same WP. Where can you read about this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Nemiro, 2016-01-15
@AlekseyNemiro

I didn’t have to localize PHP projects , and even more so WordPress , using gettext , but the other day I did it with some bash scripts. Perhaps my experience will be helpful.
1. You need to define the domain and scope of the message files search.
The domain is set by the textdomain function , and the path to the resources is bindtextdomain .

bindtextdomain('example', './local');
textdomain('example');

2. In code, all localizable text should be output via the gettext function . For example:
echo gettext("Hello world!");
echo gettext("Test123");

You can use phrases or keys.
If you use phrases, you won't need to create resources for the default language. Of the minuses of this approach, some phrases may have different meanings depending on the context of use, but this problem can in principle be solved by paraphrasing.
If you use keys (for example, MSG_ABOUT_TEXT1 ), then you will have to create resources for the default language. In the absence of resources, or if something breaks, the keys will be displayed to the user, which is not very good.
Which approach to choose depends on the complexity of the project and preferences. Phrases are simpler and there will definitely be no problems with them in small projects.
3. You need to extract lines from the code and create po files. This is done using the gettext toolkit . If you have not had to do this before, then most likely you will have to install the necessary components.
Under Debian/Ubuntu , you can install the gettext package with the following command:
Under other flavors of Linux systems, in general, about the same, only the package manager will be different.
Under Windows, I have not had a chance to do this. I think you can find something. For example, sourceforge.net/projects/gettext
I will only describe the procedure under Linux (specifically Debian ).
To create a po-file, run the following command:
Parameters:
--default-domain - domain name, must match the domain used in the PHP code ( textdomain ). Not to be confused with the site domain, although the name of the project is usually used;
--from-code=text encoding name . For example: utf-8 ;
--extract-all - indicates that all lines should be extracted.
Additionally, you can specify parameters:
--output=filename.po - by default, lines will be extracted to the messages.po file , this parameter allows you to specify any file name;
--no-wrap - prohibition of splitting long lines;
--join-existing- if there are previous extracted lines, then this option indicates the need to merge new found lines with existing ones (use only if the po file exists);
--copyright-holder="Vasya Pupkaidze" - who owns the copyright.
The --help option allows you to see a list of all options :-)
The program will create a po file with the following content:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <[email protected]>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-01-15 14:55+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "Hello world!"
msgstr ""

msgid "Test123"
msgstr ""

msgid is the message ID.
msgid - translation.
After specifying the translation and saving the changes in the po file :
msgid "Hello world!"
msgstr "Привет, мир!"

msgid "Test123"
msgstr "Тест123"

It needs to be compiled into a mo file .
This is done using the msgfmt command :
For myself, I wrote the following bash script, which compiles all po files in the directory (including all subdirectories) where the script was run:
#!/bin/bash

lang="$1"
path="$(cd "$(dirname "$0")" && pwd)"

if ; then
  path="$path/$lang"
  if ; then
    mkdir "$path"
  fi
fi

cd "$path"

find "$path" -name "*.po" | while read -r f; do
  po_dir="$(dirname $f)"
  po_file="$(basename $f)"
  po_name="$(echo $po_file | cut -d'.' -f1)"
  msgfmt --output-file="$po_dir/$po_name.mo" "$f" && \
  printf "Created: $po_dir/$po_name.mo\n" || \
  printf "ERROR: Could not create mo-file from: $f\n"
done

If everything goes without incident, the mo files should be placed in the LC_MESSAGES subfolders of the desired cultures, which should be located in the folder specified in the PHP code by the bindtextdomain function . It's confusing, it's easier to show :-)
If:
bindtextdomain('example', './local');
textdomain('example');

, then the mo file layout structure :
/local/ru/LC_MESSAGES/messages.mo
/local/en/LC_MESSAGES/messages.mo
/local/en_US/LC_MESSAGES/messages.mo
/local/ru_GB/LC_MESSAGES/messages.mo
/local/[Код культуры]/LC_MESSAGES/messages.mo

/local - part of the path specified in the PHP code : bindtextdomain('example', ' ./local ');
The culture code is a standard ( ISO 639 ) two-digit language code, or a language code with a region code ( ISO 639 + ISO 3166 ): ru (Russian language), ru_RU (Russian language, Russia), en (English language), en_US ( American English), etc.
To be honest, I don't see any real benefit to using gettext yet . Too complicated to generate all those po and mo. It is easier and more convenient to create a text file with the necessary resources, or use the database. Although work with gettext can be automated, it will take time.

W
wol_fi, 2016-01-15
@wol_fi

I don't see the point in these .po\.mo, especially for php.
store in a simple associative array - and easier (the file can be edited in any editor, without the need for compilation) and spends less resources.
For example, wordpress 5MWYccj.pngMO::import_from_reader and do not throw it away, and eats processor time.

A
Andrey Burov, 2016-01-15
@BuriK666

https://www.opennet.ru/base/dev/php_gettext.txt.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question