diff --git a/system/Controllers/SetupController.php b/system/Controllers/SetupController.php index bd7f86d..e86ed78 100644 --- a/system/Controllers/SetupController.php +++ b/system/Controllers/SetupController.php @@ -52,31 +52,9 @@ class SetupController extends Controller $setuperrors = empty($systemcheck) ? false : 'Some system requirements for Typemill are missing.'; $systemcheck = empty($systemcheck) ? false : $systemcheck; - # Get the translated strings - $labels = $this->getSetupLabels(); - - return $this->render($response, 'auth/setup.twig', array( 'messages' => $setuperrors, 'systemcheck' => $systemcheck, 'labels' => $labels )); + return $this->render($response, 'auth/setup.twig', array( 'messages' => $setuperrors, 'systemcheck' => $systemcheck )); } - public function getSetupLabels() - { - # Check which languages are available - $langs = []; - $path = __DIR__ . '/../author/languages/*.yaml'; - foreach (glob($path) as $filename) { - $langs[] = basename($filename,'.yaml'); - } - - # Detect browser language - $accept_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); - $lang = in_array($accept_lang, $langs) ? $accept_lang : 'en'; - - # At least in the setup phase noon there should be no plugins and the theme should be typemill - $labels = \Typemill\Settings::getLanguageLabels($lang,'typemill',[]); - - return $labels; - } - public function create($request, $response, $args) { @@ -122,9 +100,6 @@ class SetupController extends Controller /* store updated settings */ \Typemill\Settings::updateSettings(array('welcome' => false)); - # Get the translated strings - $labels = $this->getSetupLabels(); - - return $this->render($response, 'auth/welcome.twig', array( 'labels' => $labels )); + return $this->render($response, 'auth/welcome.twig', array()); } } \ No newline at end of file diff --git a/system/Extensions/TwigLanguageExtension.php b/system/Extensions/TwigLanguageExtension.php index 3cd6eb2..c3f79d6 100644 --- a/system/Extensions/TwigLanguageExtension.php +++ b/system/Extensions/TwigLanguageExtension.php @@ -17,59 +17,17 @@ class TwigLanguageExtension extends \Twig_Extension { return [ new \Twig_SimpleFilter('__', [$this,'translate'] ), - new \Twig_SimpleFilter('ta', [$this,'translate_array'] ) ]; } public function getFunctions() { return [ - new \Twig_SimpleFunction('__', array($this, 'translate' )), - new \Twig_SimpleFunction('ta', [$this,'translate_array'] ) + new \Twig_SimpleFunction('__', array($this, 'translate' )) ]; } - public function translate_array( $label ) - { - /* In reality the function does not Translate an Array but a string, temporarily transformed into an array. - * I saw a filter/function with this name in Grav. - * Example: - - $label -> placeholder="Add Label for Start-Button" value="Start" - - after explode: - { - [0]=> string(13) " placeholder=" - [1]=> string(26) "Add Label for Start-Button" - [2]=> string(7) " value=" - [3]=> string(5) "Start" - [4]=> string(0) "" - } - - */ - $translated_label = ''; - $items = explode('"',$label); - foreach($items as $item){ - // skip empty string - if(!empty($item)){ - $pos = strpos($item, '='); - //skip string containing equal sign - if ($pos === false) { - // translate with previous function in this class - $translated = $this->translate($item); - // add the translated string - $translated_label .= '"'.$translated.'"'; - } else { - // adds the string containing the equal sign - $translated_label .= $item; - } - } - } - return $translated_label; - } - - public function translate( $label ) { // replaces spaces, dots, comma and dash with underscores diff --git a/system/Settings.php b/system/Settings.php index 9e4fe83..37b632c 100644 --- a/system/Settings.php +++ b/system/Settings.php @@ -38,22 +38,6 @@ class Settings } } - # let us load translations only for admin area to improve performance for frontend - $uri = $_SERVER['REQUEST_URI']; - if(isset($uri) && (strpos($uri,'/tm/') !== false OR strpos($uri,'/setup') !== false)) - { - # i18n - # load the strings of the set language - $language = $settings['language']; - $theme = $settings['theme']; - $plugins = []; - if(isset($settings['plugins'])) - { - $plugins = $settings['plugins']; - } - $settings['labels'] = self::getLanguageLabels($language, $theme, $plugins); - } - # We know the used theme now so create the theme path $settings['themePath'] = $settings['rootPath'] . $settings['themeFolder'] . DIRECTORY_SEPARATOR . $settings['theme']; @@ -109,65 +93,6 @@ class Settings } - # i18n - public static function getLanguageLabels($language, $theme, $plugins) - { - # if not present, set the English language - if( empty($language) ) - { - $language = 'en'; - } - - # loads the system strings of the set language - $yaml = new Models\WriteYaml(); - $system_labels = $yaml->getYaml('system' . DIRECTORY_SEPARATOR . 'author' . DIRECTORY_SEPARATOR . 'languages', $language . '.yaml'); - - # loads the theme strings of the set language - $theme_labels = []; - $theme_language_folder = 'themes' . DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR; - $theme_language_file = $language . '.yaml'; - if (file_exists($theme_language_folder . $theme_language_file)) - { - $this_theme_labels = $yaml->getYaml($theme_language_folder, $theme_language_file); - if(is_array($this_theme_labels)) - { - $theme_labels = $this_theme_labels; - } - } - - # loads the plugins strings of the set language - $plugins_labels = []; - if(!empty($plugins)) - { - $plugin_labels = []; - foreach($plugins as $name => $value) - { - $plugin_language_folder = 'plugins' . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR; - $plugin_language_file = $language . '.yaml'; - - if (file_exists($plugin_language_folder . $plugin_language_file)) - { - $this_plugin_labels = $yaml->getYaml($plugin_language_folder, $plugin_language_file); - if(is_array($this_plugin_labels)) - { - $plugin_labels[$name] = $this_plugin_labels; - } - } - } - - foreach($plugin_labels as $key => $value) - { - $plugins_labels = array_merge($plugins_labels, $value); - } - } - - # Combines arrays of system languages, themes and plugins - $labels = array_merge($system_labels, $theme_labels, $plugins_labels); - - return $labels; - - } - public function whichLanguage() { # Check which languages are available diff --git a/system/Translations.php b/system/Translations.php new file mode 100644 index 0000000..f8d00ba --- /dev/null +++ b/system/Translations.php @@ -0,0 +1,89 @@ +getYaml('settings', 'settings.yaml'); + + if($settings === FALSE){ + $language = \Typemill\Settings::whichLanguage(); + } else { + $language = $settings['language']; + } + + $theme = 'typemill'; + if($settings !== NULL){ + if(is_array($settings)){ + if (array_key_exists('theme', $settings)) { + $theme = $settings['theme']; + } + } + } + + // theme labels selected according to the environment: admin or user + $theme_labels = []; + $theme_language_folder = 'themes' . DS . $theme . DS . 'languages' . DS . $environment . DS; + $theme_language_file = $language . '.yaml'; + if (file_exists($theme_language_folder . $theme_language_file)) + { + $theme_labels = $yaml->getYaml($theme_language_folder, $theme_language_file); + } + + $system_labels = []; + $plugins_labels = []; + if($environment=='admin'){ + // system labels + $system_language_folder ='system' . DS . 'author' . DS . 'languages' . DS; + $system_language_file = $language . '.yaml'; + if (file_exists($system_language_folder . $system_language_file)) + { + $system_labels = $yaml->getYaml($system_language_folder, $system_language_file); + } + + // Next change, to provide labels for the admin and user environments. + // There may be plugins that only work in the user environment, + // only in the admin environment, or in both environments. + $plugin_labels = []; + if($settings !== NULL){ + if(is_array($settings)){ + if (array_key_exists('plugins', $settings)) { + if($settings['plugins'] !== NULL) { + foreach($settings['plugins'] as $plugin => $config){ + if($config['active']=='on'){ + $plugin_language_folder = 'plugins' . DS . $plugin . DS . 'languages' . DS; + $plugin_language_file = $language . '.yaml'; + if (file_exists($plugin_language_folder . $plugin_language_file)){ + $plugin_labels[$plugin] = $yaml->getYaml($plugin_language_folder, $plugin_language_file); + } + } + } + foreach($plugin_labels as $key => $value) { + $plugins_labels = array_merge($plugins_labels, $value); + } + } + } + } + } + } + + $labels = []; + if(is_array($plugins_labels)){ + $labels = array_merge($labels, $plugins_labels); + } + if(is_array($system_labels)){ + $labels = array_merge($labels, $system_labels); + } + if(is_array($theme_labels)){ + $labels = array_merge($labels, $theme_labels); + } + + return $labels; + } + +} diff --git a/system/author/auth/setup.twig b/system/author/auth/setup.twig index 5c897c1..5c1a156 100644 --- a/system/author/auth/setup.twig +++ b/system/author/auth/setup.twig @@ -1,5 +1,5 @@ {% extends 'layouts/layoutAuth.twig' %} -{% block title %}{{ labels['SETUP'] ? labels['SETUP'] : 'Setup' }}{% endblock %} +{% block title %}{{ __('Setup') }}{% endblock %} {% block content %} @@ -19,21 +19,21 @@
- + {% if errors.username %} {{ errors.username | first }} {% endif %}
- + {% if errors.email %} {{ errors.email | first }} {% endif %}
- + {% if errors.password %} {{ errors.password | first }} @@ -41,7 +41,7 @@
- + {{ csrf_field() | raw }} diff --git a/system/author/auth/welcome.twig b/system/author/auth/welcome.twig index 7644ac7..da0ed84 100644 --- a/system/author/auth/welcome.twig +++ b/system/author/auth/welcome.twig @@ -1,6 +1,6 @@ {% extends 'layouts/layoutAuth.twig' %} -{% block title %}{{ labels['SW_SETUP_WELCOME'] ? labels['SW_SETUP_WELCOME'] : 'Setup Welcome' }}{% endblock %} +{% block title %}{{ __('Setup Welcome') }}{% endblock %} {% block content %} @@ -8,37 +8,37 @@
-

{{ labels['SW_HURRA'] ? labels['SW_HURRA'] : 'Hurra' }}!

-

{{ labels['SW_YOUR_ACCOUNT'] ? labels['SW_YOUR_ACCOUNT'] : 'Your account has been created and you are logged in now.'}}

-

{{ labels['SW_NEXT_STOP'] ? labels['SW_NEXT_STOP'] : 'Next step:' }} {{ labels['SW_VISIT'] ? labels['SW_VISIT'] : 'Visit the author panel and setup your new website. You can configure the system, choose themes and add plugins.' }}

-

{{ labels['SW_GET_HELP'] ? labels['SW_GET_HELP'] : 'Get help:' }} {{ labels['SW_IF_YOU_HAVE'] ? labels['SW_IF_YOU_HAVE'] : 'If you have any questions, please read the' }} {{ labels['SW_DOCS'] ? labels['SW_DOCS'] : 'docs' }} {{ labels['SW_OR_OPEN'] ? labels['SW_OR_OPEN'] : 'or open a new issue on' }} {{ labels['GITHUB'] ? labels['GITHUB'] : 'github' }}.

-

{{ labels['SW_CODED'] ? labels['SW_CODED'] : 'Coded with' }} {{ labels['SW_BY'] ? labels['SW_BY'] : 'by the' }} {{ labels['SW_COMMUNITY'] ? labels['SW_COMMUNITY'] : 'community' }} & {{ labels['SW_TRENDSCHAU'] ? labels['SW_TRENDSCHAU'] : 'Trendschau Digital' }}.

+

{{ __('Hurra') }}!

+

{{ __('Your account has been created and you are logged in now.') }}

+

{{ __('Next step') }}: {{ __('Visit the author panel and setup your new website. You can configure the system, choose themes and add plugins.') }}

+

{{ __('Get help') }}: {{ __('If you have any questions, please read the') }} {{ __('docs') }} {{ __('or open a new issue on') }} {{ __('github') }}.

+

{{ __('Coded with') }} {{ __('by the') }} {{ __('community') }} & {{ __('Trendschau Digital') }}.

- {{ labels['SW_CONFIGURE'] ? labels['SW_CONFIGURE'] : 'Configure your website' }} + {{ __('Configure your website') }}
-

{{ labels['SYSTEM'] ? labels['SYSTEM'] : 'System' }}

-

{{ labels['SW_GIVE_YOUR_NEW'] ? labels['SW_GIVE_YOUR_NEW'] : 'Give your new website a name, add the author and choose a copyright.' }}

+

{{ __('System') }}

+

{{ __('Give your new website a name, add the author and choose a copyright.') }}

-

{{ labels['THEMES'] ? labels['THEMES'] : 'Themes' }}

-

{{ labels['SW_CHOOSE_A_THEME'] ? labels['SW_CHOOSE_A_THEME'] : 'Choose a theme for your website and configure the theme details.' }}

+

{{ __('Themes') }}

+

{{ __('Choose a theme for your website and configure the theme details.') }}

-

{{ labels['PLUGINS'] ? labels['PLUGINS'] : 'Plugins' }}

-

{{ labels['SW_ADD_NEW_FEATURE'] ? labels['SW_ADD_NEW_FEATURE'] : 'Add new features to your website with plugins and configure them.' }}

+

{{ __('Plugins') }}

+

{{ __('Add new features to your website with plugins and configure them.') }}

diff --git a/system/author/js/vue-shared.js b/system/author/js/vue-shared.js index 7d09441..b63a96f 100644 --- a/system/author/js/vue-shared.js +++ b/system/author/js/vue-shared.js @@ -11,13 +11,13 @@ Vue.component('component-image', { '
' + '
' + ' ' + - '

upload an image

'+ + '

{{ \'upload an image\'|translate }}

'+ '
' + '
' + - '' + + '' + '
' + '
' + - '' + + '' + '
' + '' + ' let formatConfig = {{ settings.formats|json_encode() }}; let language = {{ settings.language|json_encode() }}; - let labels = {{ settings.labels|json_encode() }}; + let labels = {{ translations|json_encode() }}; let navigation = {{ navigation|json_encode() }}; diff --git a/system/author/layouts/layoutEditor.twig b/system/author/layouts/layoutEditor.twig index 0f04772..6ba924e 100644 --- a/system/author/layouts/layoutEditor.twig +++ b/system/author/layouts/layoutEditor.twig @@ -96,7 +96,7 @@ diff --git a/system/system.php b/system/system.php index da86c2d..742ee6e 100644 --- a/system/system.php +++ b/system/system.php @@ -203,9 +203,6 @@ $container['view'] = function ($container) $view->addExtension(new Typemill\Extensions\TwigMetaExtension()); $view->addExtension(new Typemill\Extensions\TwigPagelistExtension()); - // i18n - $view->addExtension(new Typemill\Extensions\TwigLanguageExtension( $container->get('settings')['labels'] )); - /* use {{ base_url() }} in twig templates */ $view['base_url'] = $container['request']->getUri()->getBaseUrl(); $view['current_url'] = $container['request']->getUri()->getPath(); @@ -220,6 +217,24 @@ $container['view'] = function ($container) /* add asset-function to all views */ $view->getEnvironment()->addGlobal('assets', $container->assets); + +/****************************** +* LOAD TRANSLATIONS * +******************************/ + $uri = $_SERVER['REQUEST_URI']; + if(isset($uri) && (strpos($uri,'/tm/') !== false OR strpos($uri,'/setup') !== false)) + { + // Admin environment labels + $labels = Typemill\Translations::loadTranslations('admin'); + } else { + // User environment labels + // For now it is useless, but it will prove useful in the future + $labels = Typemill\Translations::loadTranslations('user'); + } + $container['translations'] = $labels; + $view['translations'] = $labels; + $view->addExtension(new Typemill\Extensions\TwigLanguageExtension( $labels )); + return $view; }; diff --git a/themes/typemill/cover.twig b/themes/typemill/cover.twig index 356644c..bc7954d 100644 --- a/themes/typemill/cover.twig +++ b/themes/typemill/cover.twig @@ -19,10 +19,10 @@ {{ content }}