Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Symfony translation component #1466

Merged
merged 10 commits into from
Aug 30, 2023
40 changes: 22 additions & 18 deletions src/controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Controller

protected $languages;

protected $translator;
Vainonen marked this conversation as resolved.
Show resolved Hide resolved

/**
* Initializes the Model object.
*/
Expand All @@ -30,37 +32,39 @@ public function __construct($model)
$this->negotiator = new \Negotiation\Negotiator();
$domain = 'skosmos';

// Specify the location of the translation tables
bindtextdomain($domain, 'resource/translations');
bind_textdomain_codeset($domain, 'UTF-8');

// Choose domain for translations
textdomain($domain);

// Build arrays of language information, with 'locale' and 'name' keys
$this->languages = array();
foreach ($this->model->getConfig()->getLanguages() as $langcode => $locale) {
$this->languages[$langcode] = array('locale' => $locale);
$this->setLanguageProperties($langcode);
$this->setLocale($langcode);
$this->translator = $this->model->getTranslator();
$this->translator->setlocale($langcode);
$this->languages[$langcode]['name'] = $this->translator->trans('in_this_language');
$this->languages[$langcode]['lemma'] = Punic\Language::getName($langcode, $langcode);
}
}

/**
* Sets the locale language properties from the parameter (used by gettext and some Model classes).
* @param string $lang language parameter eg. 'fi' for Finnish.
* Changes translation language for Symfony Translator.
* @param string $lang two character language code 'fi' or compound language (locale) name such as 'fi_FI'
*/
public function setLanguageProperties($lang)
public function setLocale($locale)
{
if (array_key_exists($lang, $this->languages)) {
$locale = $this->languages[$lang]['locale'];
putenv("LANGUAGE=$locale");
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
} else {
trigger_error("Unsupported language '$lang', not setting locale", E_USER_WARNING);
if (!is_null($this->translator)) {
$this->model->setlocale($locale);
}
}

/**
* Get text translated in language set by SetLocale function
*
* @param string $text text to be translated
*/
protected function getText($text)
{
return $this->model->getText($text);
}

/**
* Negotiate a MIME type according to the proposed format, the list of valid
* formats, and an optional proposed format.
Expand Down
22 changes: 11 additions & 11 deletions src/controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function vocabularies($request)
return $this->returnError(400, "Bad Request", "lang parameter missing");
}

$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());

$vocabs = array();
foreach ($this->model->getVocabularies() as $voc) {
Expand Down Expand Up @@ -259,7 +259,7 @@ public function vocabularyStatistics($request)
if ($this->notModified($request->getVocab())) {
return null;
}
$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());
$arrayClass = $request->getVocab()->getConfig()->getArrayClassURI();
$groupClass = $request->getVocab()->getConfig()->getGroupClassURI();
$queryLang = $request->getQueryParam('lang') ?? $request->getLang();
Expand Down Expand Up @@ -294,7 +294,7 @@ public function vocabularyStatistics($request)
'title' => $request->getVocab()->getConfig()->getTitle(),
'concepts' => array(
'class' => 'http://www.w3.org/2004/02/skos/core#Concept',
'label' => gettext('skos:Concept'),
'label' => $this->getText('skos:Concept'),
'count' => isset($vocabStats['http://www.w3.org/2004/02/skos/core#Concept']) ? $vocabStats['http://www.w3.org/2004/02/skos/core#Concept']['count'] : 0,
'deprecatedCount' => isset($vocabStats['http://www.w3.org/2004/02/skos/core#Concept']) ? $vocabStats['http://www.w3.org/2004/02/skos/core#Concept']['deprecatedCount'] : 0,
),
Expand All @@ -304,22 +304,22 @@ public function vocabularyStatistics($request)
if (isset($vocabStats['http://www.w3.org/2004/02/skos/core#Collection'])) {
$ret['conceptGroups'] = array(
'class' => 'http://www.w3.org/2004/02/skos/core#Collection',
'label' => gettext('skos:Collection'),
'label' => $this->getText('skos:Collection'),
'count' => $vocabStats['http://www.w3.org/2004/02/skos/core#Collection']['count'],
'deprecatedCount' => $vocabStats['http://www.w3.org/2004/02/skos/core#Collection']['deprecatedCount'],
);

} elseif (isset($vocabStats[$groupClass])) {
$ret['conceptGroups'] = array(
'class' => $groupClass,
'label' => isset($vocabStats[$groupClass]['label']) ? $vocabStats[$groupClass]['label'] : gettext(EasyRdf\RdfNamespace::shorten($groupClass)),
'label' => isset($vocabStats[$groupClass]['label']) ? $vocabStats[$groupClass]['label'] : $this->getText(EasyRdf\RdfNamespace::shorten($groupClass)),
'count' => $vocabStats[$groupClass]['count'],
'deprecatedCount' => $vocabStats[$groupClass]['deprecatedCount'],
);
} elseif (isset($vocabStats[$arrayClass])) {
$ret['arrays'] = array(
'class' => $arrayClass,
'label' => isset($vocabStats[$arrayClass]['label']) ? $vocabStats[$arrayClass]['label'] : gettext(EasyRdf\RdfNamespace::shorten($arrayClass)),
'label' => isset($vocabStats[$arrayClass]['label']) ? $vocabStats[$arrayClass]['label'] : $this->getText(EasyRdf\RdfNamespace::shorten($arrayClass)),
'count' => $vocabStats[$arrayClass]['count'],
'deprecatedCount' => $vocabStats[$arrayClass]['deprecatedCount'],
);
Expand All @@ -338,7 +338,7 @@ public function labelStatistics($request)
return null;
}
$lang = $request->getLang();
$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());
$vocabStats = $request->getVocab()->getLabelStatistics();

/* encode the results in a JSON-LD compatible array */
Expand Down Expand Up @@ -398,7 +398,7 @@ public function types($request)
return null;
}

$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());

$queriedtypes = $this->model->getTypes($vocid, $request->getLang());

Expand Down Expand Up @@ -687,7 +687,7 @@ public function data($request)
*/
public function mappings(Request $request)
{
$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());
$vocab = $request->getVocab();
if ($this->notModified($vocab)) {
return null;
Expand Down Expand Up @@ -774,7 +774,7 @@ public function label($request)

public function indexLetters($request)
{
$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());
$letters = $request->getVocab()->getAlphabet($request->getLang());

$ret = array_merge_recursive(
Expand Down Expand Up @@ -802,7 +802,7 @@ public function indexLetters($request)

public function indexConcepts($letter, $request)
{
$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());

$offset_param = $request->getQueryParam('offset');
$offset = (is_numeric($offset_param) && $offset_param >= 0) ? $offset_param : 0;
Expand Down
46 changes: 17 additions & 29 deletions src/controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* Importing the dependencies.
*/
use Punic\Language;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\PoFileLoader;
use Symfony\Bridge\Twig\Extension\TranslationExtension;

/**
Expand Down Expand Up @@ -68,16 +66,8 @@ public function __construct($model)
});
$this->twig->addFilter($langFilter);

foreach($this->languages as $langcode => $value) {
if (is_null($this->translator)) {
$this->translator = new Translator($langcode);
$this->twig->addExtension(new TranslationExtension($this->translator));
}
$this->translator->addLoader('po', new PoFileLoader());
$this->translator->addResource('po', __DIR__.'/../../resource/translations/skosmos_' . $langcode . '.po', $langcode);
$this->translator->setlocale($langcode);
$this->languages[$langcode]['name'] = $this->translator->trans('in_this_language');
}
$this->translator = $model->getTranslator();
$this->twig->addExtension(new TranslationExtension($this->translator));

// create the honeypot
$this->honeypot = new \Honeypot();
Expand All @@ -87,15 +77,6 @@ public function __construct($model)
$this->twig->addGlobal('honeypot', $this->honeypot);
}

/**
* Creates and registers the translation extension and loads translation file.
* @param string $lang two character language code 'fi' or compound language (locale) name such as 'fi_FI'
*/
public function setTranslationLocale($locale)
{
$this->translator->setlocale($locale);
}

/**
* Guess the language of the user. Return a language string that is one
* of the supported languages defined in the $LANGUAGES setting, e.g. "fi".
Expand Down Expand Up @@ -162,7 +143,8 @@ private function listStyle()
public function invokeLandingPage($request)
{
// set language parameters for gettext
$this->setLanguageProperties($request->getLang());
//$this->setLanguageProperties($request->getLang());
$this->setLocale($request->getLang());
// load template
$template = $this->twig->loadTemplate('landing.twig');
// set template variables
Expand Down Expand Up @@ -192,7 +174,7 @@ public function invokeLandingPage($request)
public function invokeVocabularyConcept(Request $request)
{
$lang = $request->getLang();
$this->setLanguageProperties($lang);
$this->setLocale($request->getLang());
$vocab = $request->getVocab();

$langcodes = $vocab->getConfig()->getShowLangCodes();
Expand Down Expand Up @@ -233,7 +215,8 @@ public function invokeVocabularyConcept(Request $request)
public function invokeFeedbackForm($request)
{
$template = $this->twig->loadTemplate('feedback.twig');
$this->setLanguageProperties($request->getLang());
//$this->setLanguageProperties($request->getLang());
Vainonen marked this conversation as resolved.
Show resolved Hide resolved
$this->setLocale($request->getLang());
$vocabList = $this->model->getVocabularyList(false);
$vocab = $request->getVocab();

Expand Down Expand Up @@ -341,7 +324,8 @@ public function sendFeedback($request, $message, $messageSubject, $fromName = nu
public function invokeAboutPage($request)
{
$template = $this->twig->loadTemplate('about.twig');
$this->setLanguageProperties($request->getLang());
//$this->setLanguageProperties($request->getLang());
Vainonen marked this conversation as resolved.
Show resolved Hide resolved
$this->setLocale($request->getLang());
$url = $request->getServerConstant('HTTP_HOST');

echo $template->render(
Expand All @@ -360,7 +344,8 @@ public function invokeGlobalSearch($request)
{
$lang = $request->getLang();
$template = $this->twig->loadTemplate('global-search.twig');
$this->setLanguageProperties($lang);
//$this->setLanguageProperties($lang);
Vainonen marked this conversation as resolved.
Show resolved Hide resolved
$this->setLocale($request->getLang());

$parameters = new ConceptSearchParameters($request, $this->model->getConfig());

Expand Down Expand Up @@ -429,7 +414,8 @@ public function invokeGlobalSearch($request)
public function invokeVocabularySearch($request)
{
$template = $this->twig->loadTemplate('vocab-search.twig');
$this->setLanguageProperties($request->getLang());
//$this->setLanguageProperties($request->getLang());
Vainonen marked this conversation as resolved.
Show resolved Hide resolved
$this->setLocale($request->getLang());
$vocab = $request->getVocab();
$searchResults = null;
try {
Expand Down Expand Up @@ -503,7 +489,8 @@ public function invokeVocabularyHome($request)
{
$lang = $request->getLang();
// set language parameters for gettext
$this->setLanguageProperties($lang);
//$this->setLanguageProperties($lang);
Vainonen marked this conversation as resolved.
Show resolved Hide resolved
$this->setLocale($request->getLang());
$vocab = $request->getVocab();

$defaultView = $vocab->getConfig()->getDefaultSidebarView();
Expand Down Expand Up @@ -535,7 +522,8 @@ public function invokeVocabularyHome($request)
*/
public function invokeGenericErrorPage($request, $message = null)
{
$this->setLanguageProperties($request->getLang());
//$this->setLanguageProperties($request->getLang());
osma marked this conversation as resolved.
Show resolved Hide resolved
$this->setLocale($request->getLang());
header("HTTP/1.0 404 Not Found");
$template = $this->twig->loadTemplate('error.twig');
echo $template->render(
Expand Down
2 changes: 1 addition & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
} else {
if (array_key_exists($parts[1], $config->getLanguages())) { // global pages
$request->setLang($parts[1]);
$controller->setTranslationLocale($parts[1]);
$controller->setLocale($parts[1]);
$content_lang = $request->getQueryParam('clang');
$request->setContentLang($content_lang);
($parts[2] == 'about' || $parts[2] == 'feedback' || $parts[2] == 'search') ? $request->setPage($parts[2]) : $request->setPage('');
Expand Down
2 changes: 1 addition & 1 deletion src/model/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function getResources($property)
protected function getLiteral($property, $default=null, $lang=null)
{
if (!isset($lang)) {
$lang = $this->getEnvLang();
$lang = $this->getLang();
}

$literal = $this->getResource()->getLiteral($property, $lang);
Expand Down
Loading