1: <?php
2:
3: class Translator {
4:
5: public static function termsToTranslate() {
6: $terms = array();
7: foreach (self::_findModels() as $model) {
8: $terms[$model->plugin] = empty($terms[$model->plugin]) ?
9: self::_modelTerms($model) :
10: array_merge($terms[$model->plugin], self::_modelTerms($model));
11: }
12: $ret = array();
13: foreach ($terms as $plugin => $pluginTerms) {
14: $ret[$plugin] = self::_uniqueTerms($pluginTerms);
15: }
16: ksort($ret);
17: return $ret;
18: }
19:
20: private static function _uniqueTerms($terms) {
21: $ret = array_unique($terms);
22: sort($ret);
23: return $ret;
24: }
25:
26: 27: 28: 29: 30:
31: private static function _modelTerms(\Model $model) {
32: $terms = array(
33: $model->name,
34: preg_replace('/([a-z])([A-Z])/', '\1 \2', $model->name),
35: Inflector::pluralize($model->name),
36: Inflector::pluralize(
37: preg_replace('/([a-z])([A-Z])/', '\1 \2', $model->name)),
38: );
39: if (is_array($model->schema())) {
40: foreach (array_keys($model->schema()) as $field) {
41: $terms[] = Inflector::humanize(preg_replace('/_id$/', '', $field));
42: }
43: }
44: foreach (array_keys($model->virtualFields) as $field) {
45: $terms[] = Inflector::humanize(preg_replace('/_id$/', '', $field));
46: }
47: if (method_exists($model, 'toTranslation')) {
48: $terms = array_merge($terms, $model->toTranslation());
49: }
50: return $terms;
51: }
52:
53: 54: 55: 56:
57: private static function _findModels() {
58: $models = array();
59: foreach (App::objects('model') as $modelName) {
60: if ($modelName != 'AppModel') {
61: App::import('Model', $modelName);
62:
63: $class = new ReflectionClass($modelName);
64: if (!$class->isAbstract()) {
65: $models[] = $class->newInstance();
66: }
67: }
68: }
69: return $models;
70: }
71:
72: }
73: