Unit.pm: add UTF8 support

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@12671 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
jpawlowski 2016-11-27 12:06:33 +00:00
parent 5c7afd4e0b
commit d46de12c87

View File

@ -5,6 +5,8 @@ use warnings;
use Scalar::Util qw(looks_like_number); use Scalar::Util qw(looks_like_number);
use FHEM::UConv; use FHEM::UConv;
use Data::Dumper; use Data::Dumper;
use utf8;
use Encode qw(encode_utf8 decode_utf8);
sub Unit_Initialize() { sub Unit_Initialize() {
} }
@ -432,7 +434,7 @@ my $rtype_base = {
}, },
format => '%.2f', format => '%.2f',
scope => '^[0-9]*(?:\.[0-9]*)?$', scope => '^[0-9]*(?:\.[0-9]*)?$',
tmpl => '%value% %symbol%', tmpl => '%value%' . chr(0x00A0) . '%symbol%',
}, },
25 => { 25 => {
@ -789,7 +791,7 @@ my $rtypes = {
en => 'time hh:mm', en => 'time hh:mm',
}, },
tmpl_long => { tmpl_long => {
de => '%value% Uhr', de => '%value%' . chr(0x00A0) . 'Uhr',
en => '%value%', en => '%value%',
} }
}, },
@ -1237,7 +1239,7 @@ my $rtypes = {
de => 'Prozent', de => 'Prozent',
en => 'percent', en => 'percent',
}, },
tmpl => '%value% %symbol%', tmpl => '%value%' . chr(0x00A0) . '%symbol%',
scope => { min => 0, max => 100 }, scope => { min => 0, max => 100 },
}, },
@ -1283,7 +1285,7 @@ my $rtypes = {
# plane angular # plane angular
gon => { gon => {
ref_base => 14, ref_base => 14,
symbol => '°', symbol => chr(0x00B0),
suffix => 'gon', suffix => 'gon',
txt => { txt => {
de => 'Grad', de => 'Grad',
@ -1306,7 +1308,7 @@ my $rtypes = {
# temperature # temperature
c => { c => {
ref_base => 4, ref_base => 4,
symbol => chr(0xC2) . chr(0xB0) . 'C', symbol => chr(0x00B0) . 'C',
suffix => 'C', suffix => 'C',
txt => { txt => {
de => 'Grad Celsius', de => 'Grad Celsius',
@ -1316,13 +1318,14 @@ my $rtypes = {
de => 'Grad Celsius', de => 'Grad Celsius',
en => 'Degrees Celsius', en => 'Degrees Celsius',
}, },
tmpl => '%value%%symbol%',
tmpl => '%value%' . chr(0x202F) . '%symbol%',
scope => { min => -273.15 }, scope => { min => -273.15 },
}, },
f => { f => {
ref_base => 4, ref_base => 4,
symbol => chr(0xC2) . chr(0xB0) . 'F', symbol => chr(0x00B0) . 'F',
suffix => 'F', suffix => 'F',
txt => { txt => {
de => 'Grad Fahrenheit', de => 'Grad Fahrenheit',
@ -1332,7 +1335,7 @@ my $rtypes = {
de => 'Grad Fahrenheit', de => 'Grad Fahrenheit',
en => 'Degrees Fahrenheit', en => 'Degrees Fahrenheit',
}, },
tmpl => '%value% %symbol%', tmpl => '%value%' . chr(0x202F) . '%symbol%',
scope => { min => -459.67 }, scope => { min => -459.67 },
}, },
@ -1483,8 +1486,8 @@ my $rtypes = {
en => 'inches', en => 'inches',
}, },
tmpl => '%value%%symbol%', tmpl => '%value%%symbol%',
tmpl_long => '%value% %txt%', tmpl_long => '%value%' . chr(0x00A0) . '%txt%',
tmpl_long_pl => '%value% %txt_pl%', tmpl_long_pl => '%value%' . chr(0x00A0) . '%txt_pl%',
}, },
ft => { ft => {
@ -1500,8 +1503,8 @@ my $rtypes = {
en => 'feet', en => 'feet',
}, },
tmpl => '%value%%symbol%', tmpl => '%value%%symbol%',
tmpl_long => '%value% %txt%', tmpl_long => '%value%' . chr(0x00A0) . '%txt%',
tmpl_long_pl => '%value% %txt_pl%', tmpl_long_pl => '%value%' . chr(0x00A0) . '%txt_pl%',
}, },
yd => { yd => {
@ -1667,14 +1670,14 @@ my $rtypes = {
ref_base => 15, ref_base => 15,
ref => 'ft', ref => 'ft',
ref_t => 'sec', ref_t => 'sec',
tmpl => '%value% %suffix%/%suffix_t%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_t%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_t%',
en => '%value% %txt% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_t%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_t%',
en => '%value% %txt_pl% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_t%',
}, },
}, },
@ -1682,14 +1685,14 @@ my $rtypes = {
ref_base => 15, ref_base => 15,
ref => 'mi', ref => 'mi',
ref_t => 'hr', ref_t => 'hr',
tmpl => '%value% mph', tmpl => '%value%' . chr(0x00A0) . 'mph',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_t%',
en => '%value% %txt% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_t%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_t%',
en => '%value% %txt_pl% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_t%',
}, },
}, },
@ -1698,14 +1701,14 @@ my $rtypes = {
ref => 'm', ref => 'm',
ref_t => 'hr', ref_t => 'hr',
scale_m => '1.0e3', scale_m => '1.0e3',
tmpl => '%value% %suffix%/%suffix_t%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_t%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_t%',
en => '%value% %txt% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_t%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_t%',
en => '%value% %txt_pl% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_t%',
}, },
}, },
@ -1714,14 +1717,14 @@ my $rtypes = {
ref => 'm', ref => 'm',
ref_t => 'sec', ref_t => 'sec',
scale_m => '1.0e0', scale_m => '1.0e0',
tmpl => '%value% %suffix%/%suffix_t%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_t%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_t%',
en => '%value% %txt% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_t%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_t%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_t%',
en => '%value% %txt_pl% per %txt_t%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_t%',
}, },
}, },
@ -2008,14 +2011,14 @@ my $rtypes = {
ref_sq => 'm', ref_sq => 'm',
scale_sq => '1.0e-2', scale_sq => '1.0e-2',
format => '%.0f', format => '%.0f',
tmpl => '%value% %suffix%/%suffix_sq%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_sq%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_sq%',
en => '%value% %txt% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_sq%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_sq%',
en => '%value% %txt_pl% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_sq%',
}, },
}, },
@ -2025,14 +2028,14 @@ my $rtypes = {
ref_sq => 'm', ref_sq => 'm',
scale_sq => '1.0e0', scale_sq => '1.0e0',
format => '%.0f', format => '%.0f',
tmpl => '%value% %suffix%/%suffix_sq%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_sq%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_sq%',
en => '%value% %txt% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_sq%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_sq%',
en => '%value% %txt_pl% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_sq%',
}, },
}, },
@ -2042,14 +2045,14 @@ my $rtypes = {
ref_sq => 'm', ref_sq => 'm',
scale_sq => '1.0e-2', scale_sq => '1.0e-2',
format => '%.0f', format => '%.0f',
tmpl => '%value% %suffix%/%suffix_sq%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_sq%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_sq%',
en => '%value% %txt% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_sq%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_sq%',
en => '%value% %txt_pl% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_sq%',
}, },
}, },
@ -2059,14 +2062,14 @@ my $rtypes = {
ref_sq => 'm', ref_sq => 'm',
scale_sq => '1.0e0', scale_sq => '1.0e0',
format => '%.0f', format => '%.0f',
tmpl => '%value% %suffix%/%suffix_sq%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_sq%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_sq%',
en => '%value% %txt% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_sq%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_sq%',
en => '%value% %txt_pl% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_sq%',
}, },
}, },
@ -2076,14 +2079,14 @@ my $rtypes = {
ref_sq => 'm', ref_sq => 'm',
scale_sq => '1.0e-2', scale_sq => '1.0e-2',
format => '%.0f', format => '%.0f',
tmpl => '%value% %suffix%/%suffix_sq%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_sq%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_sq%',
en => '%value% %txt% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_sq%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_sq%',
en => '%value% %txt_pl% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_sq%',
}, },
}, },
@ -2093,14 +2096,14 @@ my $rtypes = {
ref_sq => 'm', ref_sq => 'm',
scale_sq => '1.0e0', scale_sq => '1.0e0',
format => '%.0f', format => '%.0f',
tmpl => '%value% %suffix%/%suffix_sq%', tmpl => '%value%' . chr(0x00A0) . '%suffix%/%suffix_sq%',
tmpl_long => { tmpl_long => {
de => '%value% %txt% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt% pro %txt_sq%',
en => '%value% %txt% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt% per %txt_sq%',
}, },
tmpl_long_pl => { tmpl_long_pl => {
de => '%value% %txt_pl% pro %txt_sq%', de => '%value%' . chr(0x00A0) . '%txt_pl% pro %txt_sq%',
en => '%value% %txt_pl% per %txt_sq%', en => '%value%' . chr(0x00A0) . '%txt_pl% per %txt_sq%',
}, },
}, },
@ -2455,6 +2458,7 @@ sub rname2rtype ($$@) {
# package main # package main
# #
package main; package main;
use utf8;
# Get value + rtype combined string # Get value + rtype combined string
sub replaceTemplate ($$$$;$) { sub replaceTemplate ($$$$;$) {
@ -2595,7 +2599,7 @@ sub replaceTemplate ($$$$;$) {
&& $desc->{scale_txt_long_cu} ); && $desc->{scale_txt_long_cu} );
# short # short
$txt = '%value% %suffix%'; $txt = '%value%' . chr(0x00A0) . '%suffix%';
$txt = $desc->{tmpl} if ( $desc->{tmpl} ); $txt = $desc->{tmpl} if ( $desc->{tmpl} );
if ( $r && $reading && $r->{$reading} ) { if ( $r && $reading && $r->{$reading} ) {
foreach my $k ( keys %{ $r->{$reading} } ) { foreach my $k ( keys %{ $r->{$reading} } ) {
@ -2613,7 +2617,7 @@ sub replaceTemplate ($$$$;$) {
&& ( $value eq "0" || $value > 1 ) && ( $value eq "0" || $value > 1 )
&& $desc->{txt_long_pl} ) && $desc->{txt_long_pl} )
{ {
$txt_long = '%value% %txt_long_pl%'; $txt_long = '%value%' . chr(0x00A0) . '%txt_long_pl%';
$txt_long = $desc->{tmpl_long_pl} $txt_long = $desc->{tmpl_long_pl}
if ( $desc->{tmpl_long_pl} ); if ( $desc->{tmpl_long_pl} );
} }
@ -2621,19 +2625,19 @@ sub replaceTemplate ($$$$;$) {
&& ( $value eq "0" || $value > 1 ) && ( $value eq "0" || $value > 1 )
&& $desc->{txt_pl} ) && $desc->{txt_pl} )
{ {
$txt_long = '%value% %txt_pl%'; $txt_long = '%value%' . chr(0x00A0) . '%txt_pl%';
$txt_long = $desc->{tmpl_long_pl} $txt_long = $desc->{tmpl_long_pl}
if ( $desc->{tmpl_long_pl} ); if ( $desc->{tmpl_long_pl} );
} }
# long singular # long singular
elsif ( $desc->{txt_long} ) { elsif ( $desc->{txt_long} ) {
$txt_long = '%value% %txt_long%'; $txt_long = '%value%' . chr(0x00A0) . '%txt_long%';
$txt_long = $desc->{tmpl_long} $txt_long = $desc->{tmpl_long}
if ( $desc->{tmpl_long} ); if ( $desc->{tmpl_long} );
} }
elsif ( $desc->{txt} ) { elsif ( $desc->{txt} ) {
$txt_long = '%value% %txt%'; $txt_long = '%value%' . chr(0x00A0) . '%txt%';
$txt_long = $desc->{tmpl_long} $txt_long = $desc->{tmpl_long}
if ( $desc->{tmpl_long} ); if ( $desc->{tmpl_long} );
} }
@ -2649,7 +2653,7 @@ sub replaceTemplate ($$$$;$) {
} }
} }
return ( $txt, $txt_long ); return ( Encode::encode_utf8($txt), Encode::encode_utf8($txt_long) );
} }
# format a number according to desc and optional format. # format a number according to desc and optional format.