File: translateAttributes.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (123 lines) | stat: -rwxr-xr-x 4,486 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env php
<?php

/**
 * This script loads attribute names from the attributemap/ directory, and dumps them into an attributes.po
 * translation file for each supported language.
 */

$base = __DIR__ . '/../';

include_once($base . 'vendor/autoload.php');

include_once($base . 'attributemap/name2urn.php');
$names = $attributemap;

include_once($base . 'attributemap/urn2oid.php');
$urns = $attributemap;

include_once($base . 'attributemap/newSchacNS.php');
$schac = $attributemap;

/*
 * We are still using the old JSON dictionaries as authoritative source here. It is actually convenient to keep
 * something equivalent to this, in order to automate propagation of attribute translations to the PO files. We should
 * probably consider moving the "dictionaries/attributes.definition.json" file somewhere else, and keep using it as
 * the authoritative source of known attributes for this script.
 */
$defs = json_decode(file_get_contents($base . 'dictionaries/attributes.definition.json'), true);
$trans = json_decode(file_get_contents($base . 'dictionaries/attributes.translation.json'), true);

$attributes = [];

$languages = SimpleSAML\Locale\Language::$language_names;
$languages['nb'] = $languages['no'];
unset($languages['no']);


// build the list of attributes with their corresponding aliases
foreach ($names as $name => $urn) {
    $lower = str_replace([':', '-'], '_', strtolower($name));
    if (!array_key_exists('attribute_'  . $lower, $defs)) {
        $defs['attribute_' . $lower] = [];
    }
    if (!array_key_exists('attribute_' . $lower, $trans)) {
        $trans['attribute_' . $lower] = [];
    }
    if (array_key_exists('no', $trans['attribute_' . $lower])) {
        // fix the locale code
        $trans['attribute_' . $lower]['nb'] = $trans['attribute_' . $lower]['no'];
        unset($trans['attribute_' . $lower]['no']);
    }
    $names = [$name, $urn, $urns[$urn]];
    if (array_key_exists($urn, $schac)) {
        $names[] = $schac[$urn];
    }
    $attributes[$name] = [
        'names' => $names,
        'translations' => array_merge(
            [
                'en' => $defs['attribute_' . $lower]['en'],
            ],
            $trans['attribute_' . $lower]
        ),
    ];
}

// process other sets of attributes
foreach (['facebook', 'linkedin', 'openid', 'twitter', 'windowslive'] as $set) {
    include_once($base . 'attributemap/' . $set . '2name.php');
    foreach ($attributemap as $alias => $attr) {
        if (array_key_exists($attr, $attributes)) {
            $attributes[$attr]['names'][] = $alias;
        }
    }
}

// build the dictionaries per language
foreach (array_keys($languages) as $language) {
    $strings = new Gettext\Translations();

    // load existing translations in the PO files
    $strings->addFromPoFile($base . 'locales/' . $language . "/LC_MESSAGES/attributes.po");

    foreach ($attributes as $attribute) {
        foreach ($attribute['names'] as $name) {
            if (empty($name)) {
                continue;
            }
            $translation = new Gettext\Translation('', $name);
            if (
                array_key_exists($language, $attribute['translations'])
                && !is_null($attribute['translations'][$language])
            ) {
                $t = $strings->find($translation);
                if ($t) {
                    if ($t->getOriginal() === $t->getTranslation()) {
                        $t->setTranslation($attribute['translations'][$language]);
                        $translation = $t;
                    }
                }
            }
            if (!is_null($attribute['translations']['en']) && $language !== 'en') {
                $translation->addComment('English string: ' . $attribute['translations']['en']);
            }
            $strings[] = $translation;
        }
    }

    foreach ($strings as $entry) {
        if ($entry->getTranslation() === '') {
            // ensure that all entries contain a translation string
            $entry->setTranslation($entry->getOriginal());
        }
    }

    // remove headers that only cause unnecessary changes in our commits
    $strings->deleteHeader('POT-Creation-Date');
    $strings->deleteHeader('PO-Revision-Date');

    $strings->setLanguage($language);
    echo "Saving translations to " . $base . "locales/" . $language . "/LC_MESSAGES/attributes.po\n";
    Gettext\Generators\Po::toFile($strings, $base . 'locales/' . $language . '/LC_MESSAGES/attributes.po');
}