From 6573149c2d6cc8160eb02aa46ccf99358b0b2669 Mon Sep 17 00:00:00 2001 From: Repflez Date: Mon, 13 Sep 2021 06:05:26 -0600 Subject: [PATCH] Finalize almost all the translation system. JS loading is next --- .../2021_09_13_045434_add_post_languages.php | 51 +++++++ resources/lang/en/settings.php | 45 +++++++ resources/views/3ds/settings/account.twig | 57 ++++++++ resources/views/3ds/settings/profile.twig | 29 ++++ routes/3ds.php | 4 +- src/Pages/CTR/Settings.php | 100 +++++++++++++- src/Translation.php | 124 ++++++++++-------- src/User.php | 14 +- 8 files changed, 362 insertions(+), 62 deletions(-) create mode 100644 database/2021_09_13_045434_add_post_languages.php create mode 100644 resources/lang/en/settings.php create mode 100644 resources/views/3ds/settings/account.twig create mode 100644 resources/views/3ds/settings/profile.twig diff --git a/database/2021_09_13_045434_add_post_languages.php b/database/2021_09_13_045434_add_post_languages.php new file mode 100644 index 0000000..a1ae42e --- /dev/null +++ b/database/2021_09_13_045434_add_post_languages.php @@ -0,0 +1,51 @@ +table('posts', function (Blueprint $table) { + $table->smallInteger('language')->default(1); + }); + + // Edit user language column + $schema->table('users', function (Blueprint $table) { + $table->dropColumn('lang'); + + $table->smallInteger('language')->default(1); + $table->smallInteger('post_language')->default(99); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $schema = DB::getSchemaBuilder(); + + $schema->table('posts', function (Blueprint $table) { + $table->dropColumn('language'); + }); + + $schema->table('users', function (Blueprint $table) { + $table->dropColumn('language'); + $table->dropColumn('post_language'); + + $table->string('lang', 2)->default('en'); + }); + } +} diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php new file mode 100644 index 0000000..1841388 --- /dev/null +++ b/resources/lang/en/settings.php @@ -0,0 +1,45 @@ + [ + 'title' => ':name Settings', + + 'display_language' => [ + 'title' => 'In what language you want :name to be displayed in?', + 'note' => 'Note: This setting does not affect what language posts are displayed in.', + ], + + 'post_language' => [ + 'title' => 'In what language do you want to see posts in :name?', + 'note' => 'Note: This setting does not affect what language :name is displayed in.', + + 'all_languages' => 'All Languages', + ], + + 'submit' => 'Save Settings', + ], + + // Profile Settings + 'profile' => [ + 'title' => 'Profile Settings', + + // Profile Comment + 'comment' => [ + 'section_name' => 'Profile Comment', + 'placeholder' => 'Write about yourself here.', + 'note' => 'Attention', + 'note_content' => 'Please refrain from including any of the following information:
+- Your address, telephone number, e-mail address, the name of your school, or any other information that could personally identify you.
+- Links to external websites which could be used to contact you directly.
+- Any other content prohibited by the :name Code of Conduct.
+Posting such information is against the :name Code of Conduct and may result in your profile being hidden from the public.', + ], + + // Submit button + 'submit' => 'Save Settings', + ], + ]; \ No newline at end of file diff --git a/resources/views/3ds/settings/account.twig b/resources/views/3ds/settings/account.twig new file mode 100644 index 0000000..fb80447 --- /dev/null +++ b/resources/views/3ds/settings/account.twig @@ -0,0 +1,57 @@ +{% set class = 'setting-account' %} +{% extends '@3ds/_master.twig' %} + +{% block content %} +{% set name = config('general.name') %} + + +
+
+
+
    +
  • +

    + +

    +

    {{ __('settings.account.display_language.note', { name: name }) }}

    +
    +
    + {{ selected_display_language }} + +
    +
    +
  • +
  • +

    + +

    +

    {{ __('settings.account.post_language.note', { name: name }) }}

    +
    +
    + {{ selected_post_language }} + +
    +
    +
  • +
    + +
    + +
+
+ +{% endblock %} diff --git a/resources/views/3ds/settings/profile.twig b/resources/views/3ds/settings/profile.twig new file mode 100644 index 0000000..5406473 --- /dev/null +++ b/resources/views/3ds/settings/profile.twig @@ -0,0 +1,29 @@ +{% set class = 'setting-profile' %} +{% extends '@3ds/_master.twig' %} + +{% block content %} +{% set name = config('general.name') %} + +
+
+
+
    +
      +
    • +

      {{ __('settings.profile.comment.section_name') }}

      + +

      {{ __('settings.profile.comment.note') }}

      +

      {{ __('settings.profile.comment.note_content', { name: config('general.name') })|raw }}

      +
    • +
    +
    + +
    +
    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/routes/3ds.php b/routes/3ds.php index 5bdaab9..e578853 100644 --- a/routes/3ds.php +++ b/routes/3ds.php @@ -128,8 +128,8 @@ Router::post('/played_title_ids', 'CTR.Dummy@dummy', 'settings.playedtitles'); // Account settings - Router::get('/aacount', 'CTR.Settings@account', 'settings.account'); - Router::post('/aacount', 'CTR.Settings@account_save', 'settings.accountsave'); + Router::get('/account', 'CTR.Settings@account', 'settings.account'); + Router::post('/account', 'CTR.Settings@account_save', 'settings.accountsave'); }); // Welcome diff --git a/src/Pages/CTR/Settings.php b/src/Pages/CTR/Settings.php index c19a400..14a4849 100644 --- a/src/Pages/CTR/Settings.php +++ b/src/Pages/CTR/Settings.php @@ -1,4 +1,5 @@ [ + 'title' => '日本語', + 'code' => Translation::LANGUAGE_JAPANESE, + 'selected' => $user->language === 0, + ], + 1 => [ + 'title' => 'English', + 'code' => Translation::LANGUAGE_ENGLISH, + 'selected' => $user->language === 1, + ], + 2 => [ + 'title' => 'Français', + 'code' => Translation::LANGUAGE_FRENCH, + 'selected' => $user->language === 2, + ], + 3 => [ + 'title' => 'Deutsch', + 'code' => Translation::LANGUAGE_GERMAN, + 'selected' => $user->language === 3, + ], + 4 => [ + 'title' => 'Italiano', + 'code' => Translation::LANGUAGE_ITALIAN, + 'selected' => $user->language === 4, + ], + 5 => [ + 'title' => 'Español', + 'code' => Translation::LANGUAGE_SPANISH, + 'selected' => $user->language === 5, + ], + 6 => [ + 'title' => '汉语', + 'code' => Translation::LANGUAGE_SIMPLIFIED_CHINESE, + 'selected' => $user->language === 6, + ], + 7 => [ + 'title' => '한국어', + 'code' => Translation::LANGUAGE_KOREAN, + 'selected' => $user->language === 7, + ], + 8 => [ + 'title' => 'Nederlands', + 'code' => Translation::LANGUAGE_DUTCH, + 'selected' => $user->language === 8, + ], + 9 => [ + 'title' => 'Português', + 'code' => Translation::LANGUAGE_PORTUGUESE, + 'selected' => $user->language === 9, + ], + 10 => [ + 'title' => 'Русский', + 'code' => Translation::LANGUAGE_RUSSIAN, + 'selected' => $user->language === 10, + ], + 11 => [ + 'title' => '漢語', + 'code' => Translation::LANGUAGE_TRADITIONAL_CHINESE, + 'selected' => $user->language === 11, + ], + ]; + $selected_display_language = $display_languages[$user->language]['title']; + $post_languages = [ + 99 => [ + 'title' => __('settings.account.post_language.all_languages'), + 'code' => Translation::LANGUAGE_SYSTEM, + 'selected' => $user->post_language === 99, + ], + ]; + $post_languages += $display_languages; + $selected_post_language = $post_languages[$user->post_language]['title']; + + return view('settings/account', compact( + 'display_languages', + 'selected_display_language', + 'post_languages', + 'selected_post_language' + )); + } + + public function profileView(): string + { + return view('settings/profile'); + } } diff --git a/src/Translation.php b/src/Translation.php index 38c1493..80cae4a 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -31,19 +31,19 @@ class Translation private static $engine; // Public language constants - public const LANGUAGE_SYSTEM = 'xx'; - public const LANGUAGE_JAPANESE = 'ja'; - public const LANGUAGE_ENGLISH = 'en'; - public const LANGUAGE_FRENCH = 'fr'; - public const LANGUAGE_GERMAN = 'de'; - public const LANGUAGE_ITALIAN = 'it'; - public const LANGUAGE_SPANISH = 'es'; - public const LANGUAGE_CHINESE = 'zh'; - public const LANGUAGE_KOREAN = 'ko'; - public const LANGUAGE_DUTCH = 'nl'; - public const LANGUAGE_PORTUGUESE = 'pt'; - public const LANGUAGE_RUSSIAN = 'ru'; - public const LANGUAGE_CHINESE_2 = 'zh'; + public const LANGUAGE_SYSTEM = 99; // Fallback variable meaning all languages + public const LANGUAGE_JAPANESE = 'ja'; + public const LANGUAGE_ENGLISH = 'en'; + public const LANGUAGE_FRENCH = 'fr'; + public const LANGUAGE_GERMAN = 'de'; + public const LANGUAGE_ITALIAN = 'it'; + public const LANGUAGE_SPANISH = 'es'; + public const LANGUAGE_SIMPLIFIED_CHINESE = 'chi'; + public const LANGUAGE_KOREAN = 'ko'; + public const LANGUAGE_DUTCH = 'nl'; + public const LANGUAGE_PORTUGUESE = 'pt'; + public const LANGUAGE_RUSSIAN = 'ru'; + public const LANGUAGE_TRADITIONAL_CHINESE = 'zho'; /** * Initialise the translation engine. @@ -56,52 +56,12 @@ public static function init(): void $language = ""; // Get the language to use - if (CurrentSession::$user->lang === self::LANGUAGE_SYSTEM) { + if (CurrentSession::$user->language === self::LANGUAGE_SYSTEM) { // Select the language based on their console's language - switch (intval(ConsoleAuth::$paramPack['language_id'])) { - case 0: - $language = self::LANGUAGE_JAPANESE; - break; - case 1: - $language = self::LANGUAGE_ENGLISH; - break; - case 2: - $language = self::LANGUAGE_FRENCH; - break; - case 3: - $language = self::LANGUAGE_GERMAN; - break; - case 4: - $language = self::LANGUAGE_ITALIAN; - break; - case 5: - $language = self::LANGUAGE_SPANISH; - break; - case 6: - $language = self::LANGUAGE_CHINESE; - break; - case 7: - $language = self::LANGUAGE_KOREAN; - break; - case 8: - $language = self::LANGUAGE_DUTCH; - break; - case 9: - $language = self::LANGUAGE_PORTUGUESE; - break; - case 10: - $language = self::LANGUAGE_RUSSIAN; - break; - case 11: - $language = self::LANGUAGE_CHINESE_2; - break; - default: - $language = self::LANGUAGE_ENGLISH; - break; - } + $language = self::getIsoLanguage(intval(ConsoleAuth::$paramPack['language_id'])); } else { // Use the language set on their account - $language = CurrentSession::$user->lang; + $language = self::getIsoLanguage(CurrentSession::$user->language); } // Register the translator @@ -123,4 +83,56 @@ public static function get(string $key, array $replace = []) { return self::$engine->get($key, $replace); } + + /** + * Converts Language ID to ISO 639 code. + * + * @param int $languageCode + * + * @return string + */ + public static function getIsoLanguage(int $languageCode): string + { + switch ($languageCode) { + case 0: return self::LANGUAGE_JAPANESE; + case 1: return self::LANGUAGE_ENGLISH; + case 2: return self::LANGUAGE_FRENCH; + case 3: return self::LANGUAGE_GERMAN; + case 4: return self::LANGUAGE_ITALIAN; + case 5: return self::LANGUAGE_SPANISH; + case 6: return self::LANGUAGE_SIMPLIFIED_CHINESE; + case 7: return self::LANGUAGE_KOREAN; + case 8: return self::LANGUAGE_DUTCH; + case 9: return self::LANGUAGE_PORTUGUESE; + case 10: return self::LANGUAGE_RUSSIAN; + case 11: return self::LANGUAGE_TRADITIONAL_CHINESE; + default: return self::LANGUAGE_ENGLISH; + } + } + + /** + * Converts ISO 639 code to Language ID + * + * @param string $languageCode + * + * @return int + */ + public static function getLanguageCode(string $languageCode) : int + { + switch ($languageCode) { + case self::LANGUAGE_JAPANESE: return 0; + case self::LANGUAGE_ENGLISH: return 1; + case self::LANGUAGE_FRENCH: return 2; + case self::LANGUAGE_GERMAN: return 3; + case self::LANGUAGE_ITALIAN: return 4; + case self::LANGUAGE_SPANISH: return 5; + case self::LANGUAGE_SIMPLIFIED_CHINESE: return 6; + case self::LANGUAGE_KOREAN: return 7; + case self::LANGUAGE_DUTCH: return 8; + case self::LANGUAGE_PORTUGUESE: return 9; + case self::LANGUAGE_RUSSIAN: return 10; + case self::LANGUAGE_TRADITIONAL_CHINESE: return 11; + default: return 1; // English Language ID + } + } } diff --git a/src/User.php b/src/User.php index 920db74..061ff19 100644 --- a/src/User.php +++ b/src/User.php @@ -206,9 +206,16 @@ class User /** * Current Language. * - * @var string + * @var int + */ + public $language = 1; + + /** + * Current post Language. + * + * @var int */ - public $lang = 'en'; + public $post_language = 90; /** * Mii holder for this user. @@ -322,7 +329,8 @@ private function __construct($userId) $this->follows = intval($userRow->follow_count); $this->followers = intval($userRow->follow_back_count); $this->news_dot = boolval($userRow->news_dot); - $this->lang = $userRow->lang; + $this->language = intval($userRow->language); + $this->post_language = intval($userRow->post_language); $this->registerIp = Net::ntop($userRow->register_ip); $this->lastIp = Net::ntop($userRow->last_ip);