TYPO3: Get language code - typo3

As far as I know, there are two ways to retrieve the current (front-end) language:
$GLOBALS['TSFE']->config['config']['language']
$GLOBALS['TSFE']->sys_language_uid
If the latter is 2, then the config value is, say, 'dk'.
However, if the language UID is 0, then the first variable returns '' (empty string).
Is there a mapping between sys_language_uid and the language code, or in other words, where/how can I find the default language (code)?

Just to go a bit deeper into this: There is a mapping between sys_language_id and the language code.
You're setting up languages available for your website through sys_language records on your instance root (via list module on "page" uid 0). These records contain a select field static_lang_isocode where you choose the corresponding ISO 2-char code for the language record you're creating/editing. This allows fetching the 2-char code for any language uid given.
As long as you've inserted the page languages correctly, you'll have a mapping, so to say. Except for uid 0, as konsolenfreddy wrote.

You might have to initialize the language in TypoScript:
config {
sys_language_uid = 0
language = de
locale_all = de_DE.UTF-8
htmlTag_langKey = de_DE
}
[globalVar = GP:L = 1]
config {
sys_language_uid = 1
language = en
locale_all = en_EN.UTF-8
htmlTag_langKey = en_EN
}
[end]
This would default to german and return 'de' in your case

Related

Select statement to return one value or another one if the first one is null

I have a Book's table as follows:
id locale name book_id ...
1 "en-GB" The book 421
2 "fr-FR" Le livre 421
...
I need to SELECT the book's name for a given locale (for instance 'fr-FR') but then, if the first one is not set, then retrieve the name with the locale by default ('en-GB') which will be always set.
How could I do that?
I'm using a DB2.
I've tried the following statement:
SELECT CASE WHEN B.NAME IS NOT NULL THEN B.NAME ELSE A.NAME END AS NAME FROM BOOK A JOIN BOOK B ON B.BOOK_ID = A.BOOK_ID WHERE A.LOCALE = "en-GB" AND B.LOCALE = "fr-FR"
However, this is only working when both locales are defined, but not in case that I need (when the second locale is not set).
Thanks in advance.
EDIT WITH MY OWN ANSWER:
I have finally used an UNION to achieve that:
SELECT NAME FROM BOOK WHERE BOOK_ID = 421 AND LOCALE = "en-GB"
UNION ALL
SELECT NAME FROM BOOK WHERE BOOK_ID = 421 AND LOCALE = "fr-FR"
FETCH FIRST 1 ROW ONLY
Try this:
select *
from mytable
where book_id=421 and locale in
--('*', 'en-GB')
('fr-FR', 'en-GB')
order by case when locale='en-GB' then 1 else 0 end
fetch first 1 row only;
I said View in my previous answer I meant Stored Procedure.
You might want to take a look to creating a Stored Procedure. In a SP you can add if else statements.
CREATE PROCEDURE SomeProcedure
AS
BEGIN
IF Exists(SELECT 1 FROM Book WHERE locale = "en-GB")
SELECT * FROM Book WHERE locale = "fr-FR"
ELSE
SELECT * FROM Book WHERE locale = "en-GB"
END
Something like this.

query error: no field 'face' found in schema

i have bigint column named as face in mysql. and this is my sphinx.conf
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = pass
sql_db = nums
sql_port = 3306 # optional, default is 3306
sql_query = SELECT id,id AS id_attr,tel,name,sex,face from tel
sql_attr_uint = id_attr
sql_attr_bigint = face
}
index num
{
rt_attr_bigint = face
rt_field = face
source = src1
path = C:/sphinx/bin/data/numaralar
}
i can make search by name and tel but not with face.
Fatal error: Uncaught exception 'Foolz\SphinxQL\Exception\DatabaseException' with message '[1064] index nums: query error: no field 'face' found in schema [ SELECT * FROM nums WHERE MATCH('(#face 123456)') LIMIT 0, 10 OPTION max_matches = 5000;SHOW META]' in ..
why may it be?
You are trying to use the value as an field. The # fulltext operator (and indeed the whole of MATCH() full text query, operates on fields ONLY.
You've instead defined face as an atribute. Attributes don't work in full-text queries.
Can
Make face a field instead (remove the sql_attr_bigint) or make it both an attribute and field. (to do that, would have to duplicate it like you've duplicated the id, one for field, one for attribute. or use sql_field_string, but that makes a string attribute)
or
Use filter by the attribute instead. Dont really know how to do that in Foolz. But the SphinxQL query would be something like
SELECT * FROM nums WHERE `face` = 123456 LIMIT 0, 10

Search with Turkish characters

I have problem on db search with like and elastic search in Turkish upper and lower case.
For example I have posts table which contains post titled 'DENEME YAZI'.
If I run this query:
select * from posts where title like '%deneme%';
or:
select * from posts where title like '%YAZI%';
I get correct result but if I run:
select * from posts where title like '%yazı%';
it doesn't return any record. My database encoding is tr_TR.UTF-8.
How can I get correct results without entering exact word?
You must use ILIKE for case insensitive matches:
select * from posts where title ilike '%yazı%';
However, there is the additional complication of peculiar rules in the Turkish locale. Upper case of 'ı' is 'I'. But not the other way round. Lower case of 'I' is 'i':
db=# SELECT lower(upper('ı'));
lower
-------
i
You could solve that by applying upper() on either side of the LIKE expression:
select upper('DENEME YAZI') like ('%' || upper('yazı') || '%');
Applying just a single UPPER (or LOWER) on either side of the expression is not a solution. You should handle problematic Turkish characters (ıI-iİ) by yourself.
İ and i are the same letters in Turkish alphabet.
I and ı are the same letters in Turkish alphabet.
But even using UTF-8, Latin5, Windows 1254 Encoding and collation settings in postgre
UPPER('İ') returns 'İ' OK
UPPER('i') return 'I' Not OK
UPPER('I') returns 'I' OK
UPPER('ı') return 'İ' Not OK
so
SELECT ... FROM ... WHERE ... UPPER('İZMİR') like UPPER('izmir') return false
SELECT ... FROM ... WHERE ... UPPER('ISPARTA') like UPPER('ısparta') return false.
Here's some more precise but not perfect solution because of performance issues
SELECT ... FROM ... WHERE ...
UPPER(REPLACE(REPLACE(COLUMNX, 'i', 'İ'), 'ı', 'I')) = UPPER(REPLACE(REPLACE(myvalue,
'i', 'İ'), 'ı', 'I'))
or
SELECT ... FROM ... WHERE ...
UPPER(TRANSLATE('COLUMNX','ıi','Iİ')) = UPPER(TRANSLATE(myvalue,'ıi','Iİ'))

Check if a full text catalog exists in 2000

I'm trying to detect whether a database has a specific full text catalog so that I can either use it or avoid executing part of a script that would create errors without the catalog. I know in sql server 2005 you can use:
IF EXISTS(SELECT 1 FROM sys.fulltext_catalogs WHERE name = 'catalog_name')
But we have to support sql server 2000 still and I can't use that. Is there another way to check for the catalog?
The ##version check might need altering, but this should work:
declare #catalogExists tinyint
set #catalogExists = 0
if (##version like ('%SQL%Server%2000%')) begin
if exists(SELECT 1 FROM [master].[dbo].[sysfulltextcatalogs] WHERE name = 'catalog_name')
set #catalogExists = 1
end
else begin
IF EXISTS(SELECT 1 FROM sys.fulltext_catalogs WHERE name = 'catalog_name')
set #catalogExists = 1
end
print #catalogExists

How to return text for default language

I have so tables:
and so data at Language table:
and so data at Text table:
I have to return text for requested language if it exists and text for default language if it does not exist. Is it possible to do that in one query (no while, please)?
Code:
DECLARE #CommentId bigint = 1
--DECLARE #LanguageCode nvarchar(2) = 'en' -- "english text" returns
DECLARE #LanguageCode nvarchar(2) = 'ua' -- nothing at this moment
SELECT
t.CommentId
,t.TextId
,t.[Text]
,t.LanguageId
,RequestedLanguageId = #LanguageCode
FROM dbo.common_Text t
INNER JOIN dbo.common_LanguageType l
ON t.LanguageId = l.LanguageId
WHERE l.Code = #LanguageCode
AND t.CommentId = #CommentId
Thank you.
I assume that something is messed up in the data you supplied. Didn't you mean to show a row in the text table with LanguageId = 2? Without using a recursive query or loop, you can't keep following the DefaultId of the language until you end up at English. Assuming there is a row in the text table for ukrainian's backup (2 = russian):
DECLARE
#CommentId BIGINT = 1,
#LanguageCode NVARCHAR(2) = 'ua';
SELECT
CommentId = COALESCE(t.CommentId, a.CommentId),
TextId = COALESCE(t.TextId, a.TextId),
[Text] = COALESCE(t.[Text], a.[Text]),
LanguageId = COALESCE(t.LanguageId, a.LanguageId),
RequestedLanguageId = #LanguageCode
FROM
dbo.common_LanguageType AS l
LEFT OUTER JOIN
dbo.common_Text AS t
ON l.LanguageId = t.LanguageId
AND t.CommentID = #CommentId
LEFT OUTER JOIN
dbo.common_Text AS a -- a for "alternate"
ON l.DefaultId = a.LanguageId
WHERE
l.Code = #LanguageCode
AND a.CommentID = #CommentId;
If this is not the case, you need to make the question more clear. If you have LanguageId 4, 'central ukrainian' with a DefualtId = 3, when that language is requested is the query supposed to check the text table for 4, when it's not found, it checks 4's default (3), when that's not found, it checks 3's default (2), when that's not found, it checks 2's default (1) and finally returns the row for 1? If this is the case you will certainly need a more complicated query (using either a recursive CTE or a loop).
Also for the language code you should probably use NCHAR(2) as opposed to NVARCHAR(2). I hope the column is not nullable and unique.
Solution was found on Database Administrators site