16 const ID =
'gk-translations';
53 if ( ! self::$_instance ) {
54 self::$_instance =
new self();
57 return self::$_instance;
81 if ( did_action(
'gk/foundation/translations/initialized' ) ) {
85 foreach ( FoundationCore::get_instance()->get_registered_plugins() as $plugin_file ) {
86 $plugin_data = CoreHelpers::get_plugin_data( $plugin_file );
88 if ( isset( $plugin_data[
'TextDomain'] ) ) {
89 $this->_text_domains[] = $plugin_data[
'TextDomain'];
93 if ( empty( $this->_text_domains ) ) {
97 add_action(
'update_option_WPLANG', [ $this,
'on_site_language_change' ], 10, 2 );
98 add_action(
'gk/foundation/plugin_activated', [ $this,
'on_plugin_activation' ] );
99 add_action(
'gk/foundation/plugin_deactivated', [ $this,
'on_plugin_deactivation' ] );
101 $this->_logger = LoggerFramework::get_instance();
110 do_action(
'gk/foundation/translations/initialized', $this );
128 return apply_filters(
'gk/foundation/translations/permissions/can-install-languages', current_user_can(
'install_languages' ) );
141 public function install( $text_domain, $new_language ) {
142 $current_user = wp_get_current_user();
145 $this->_logger->addError(
147 'User "%s" does not have permissions to install languages.',
148 $current_user->user_login
156 $T15s_updater->install( $new_language );
158 $translations = $T15s_updater->get_installed_translations(
true );
160 if ( isset( $translations[ $new_language ] ) ) {
164 $this->_logger->addError( $e->getMessage() );
195 $language = get_locale();
200 if ( ! $mo_translations ) {
201 $this->_logger->notice(
203 'No "%s" .mo translations found for "%s".',
212 load_textdomain( $text_domain, $mo_translations );
228 $language = get_locale();
237 if ( ! $json_translations ) {
238 $this->_logger->notice(
240 'No %s.json translations file found for "%s" text domain.',
241 $text_domain ?: $frontend_text_domain,
249 $json_translations = file_get_contents( $json_translations );
252 $text_domain = $frontend_text_domain ?: $text_domain;
254 add_filter(
'gk/foundation/inline-scripts',
function ( $scripts ) use ( $text_domain, $json_translations ) {
256 (
function( domain, translations ) {
257 var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
258 localeData[
""].domain = domain;
259 wp.i18n.setLocaleData( localeData, domain );
260 } )(
'${text_domain}', ${json_translations});
265 'dependencies' => [
'wp-i18n' ],
282 $filename = sprintf(
'%s/%s-%s.%s', self::WP_LANG_DIR, $text_domain, $language, $extension );
284 return ( file_exists( $filename ) ) ? $filename : null;
302 foreach ( $this->_text_domains as $text_domain ) {
303 $this->
install( $text_domain, $to_language );
317 if ( ! function_exists(
'get_plugin_data' ) ) {
318 require_once( ABSPATH .
'wp-admin/includes/plugin.php' );
321 $plugin_data = get_plugin_data( $plugin_file );
327 $this->
install( $plugin_data[
'TextDomain'], get_locale() );
340 if ( ! function_exists(
'get_plugin_data' ) ) {
341 require_once( ABSPATH .
'wp-admin/includes/plugin.php' );
344 $plugin_data = get_plugin_data( $plugin_file );
350 $files = glob( sprintf(
353 $plugin_data[
'TextDomain']
356 if ( empty( $files ) ) {
360 array_walk( $files,
'wp_delete_file' );
374 $locale = get_locale();
378 return in_array( $locale, [
'en_EN',
'en_US' ],
true );
install( $text_domain, $new_language)
Downloads and installs translations from TranslationsPress.
on_site_language_change( $from_language, $to_language)
Installs or updates translations for all plugins when the site's language setting is changed...
on_plugin_activation( $plugin_file)
Installs or updates translations for all plugins when a plugin is activated.
static get_translation_filename( $text_domain, $language, $extension='mo')
Returns the translation filename for a given language.
static get_instance( $slug, $translations_path='')
Returns an instance of this class for the given slug.
init()
Initializes Translations framework.
load_backend_translations( $text_domain, $language='')
Loads and sets backend translations.
static get_instance()
Returns class instance.
load_frontend_translations( $text_domain, $language='', $frontend_text_domain='')
Loads and sets frontend translations.
is_en_locale( $locale='')
Checks whether the current locale is set to English language.
on_plugin_deactivation( $plugin_file)
Deletes translations when the plugin is deactivated.
can_install_languages()
Checks of user has permissions to install languages.
get_T15s_updater( $text_domain)
Returns TranslationsPress updater instance.
Logging framework for GravityKit.
load_all_translations( $text_domain, $language='')
Loads and sets frontend and backend translations.