class-i18n.php
5.48 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* i18n class
*
* @author Tijmen Smit
* @since 2.0.0
*/
if ( !defined( 'ABSPATH' ) ) exit;
if ( !class_exists( 'WPSL_i18n' ) ) {
class WPSL_i18n {
private $wpml_active = null;
private $qtrans_active = null;
/**
* Class constructor
*/
function __construct() {
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
}
/**
* Load the translations from the language folder
*
* @since 2.0.0
* @return void
*/
public function load_plugin_textdomain() {
$domain = 'wpsl';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
// Load the language file from the /wp-content/languages/wp-store-locator folder, custom + update proof translations.
load_textdomain( $domain, WP_LANG_DIR . '/wp-store-locator/' . $domain . '-' . $locale . '.mo' );
// Load the language file from the /wp-content/plugins/wp-store-locator/languages/ folder.
load_plugin_textdomain( $domain, false, dirname( WPSL_BASENAME ) . '/languages/' );
}
/**
* Check if WPML is active
*
* @since 2.0.0
* @return boolean|null
*/
public function wpml_exists() {
if ( $this->wpml_active == null ) {
$this->wpml_active = function_exists( 'icl_register_string' );
}
return $this->wpml_active;
}
/**
* Check if a qTranslate compatible plugin is active.
*
* @since 2.0.0
* @return boolean|null
*/
public function qtrans_exists() {
if ( $this->qtrans_active == null ) {
$this->qtrans_active = ( function_exists( 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage' ) || function_exists( 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' ) );
}
return $this->qtrans_active;
}
/**
* See if there is a translated page available for the provided store ID.
*
* @since 2.0.0
* @see https://wpml.org/documentation/support/creating-multilingual-wordpress-themes/language-dependent-ids/#2
* @param string $store_id
* @return string empty or the id of the translated store
*/
public function maybe_get_wpml_id( $store_id ) {
$return_original_id = apply_filters( 'wpsl_return_original_wpml_id', true );
// icl_object_id is deprecated as of 3.2
if ( defined( 'ICL_SITEPRESS_VERSION' ) && version_compare( ICL_SITEPRESS_VERSION, 3.2, '>=' ) ) {
$translated_id = apply_filters( 'wpml_object_id', $store_id, 'wpsl_stores', $return_original_id, ICL_LANGUAGE_CODE );
} else {
$translated_id = icl_object_id( $store_id, 'wpsl_stores', $return_original_id, ICL_LANGUAGE_CODE );
}
// If '$return_original_id' is set to false, NULL is returned if no translation exists.
if ( is_null( $translated_id ) ) {
$translated_id = '';
}
return $translated_id;
}
/**
* Get the correct translation.
*
* Return the translated text from WPML or the translation
* that was set on the settings page.
*
* @since 2.0.0
* @param string $name The name of the translated string
* @param string $text The text of the translated string
* @return string $translation The translation
*/
public function get_translation( $name, $text ) {
global $wpsl_settings;
if ( defined( 'WPML_ST_VERSION' ) ) {
$translation = $text;
} elseif ( defined( 'POLYLANG_VERSION' ) && defined( 'POLYLANG_DIR' ) ) {
if ( ! function_exists( 'pll__' ) ) {
require_once POLYLANG_DIR . '/include/api.php';
}
$translation = pll__( $text );
} else {
$translation = stripslashes( $wpsl_settings[$name] );
}
return $translation;
}
/**
* If a multilingual plugin like WPML or qTranslate X is active
* we return the active language code.
*
* @since 2.0.0
* @return string Empty or the current language code
*/
public function check_multilingual_code() {
$language_code = '';
if ( $this->wpml_exists() && defined( 'ICL_LANGUAGE_CODE' ) ) {
$language_code = ICL_LANGUAGE_CODE;
} else if ( $this->qtrans_exists() ) {
if ( function_exists( 'qtranxf_getLanguage' ) ) {
$language_code = qtranxf_getLanguage();
} else if ( function_exists( 'qtrans_getLanguage' ) ) {
$language_code = qtrans_getLanguage();
}
}
return $language_code;
}
}
new WPSL_i18n();
}